Coverage Report

Created: 2025-07-06 19:09

/root/doris/be/src/util/faststring.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/faststring.h"
19
20
#include <glog/logging.h>
21
22
#include <memory>
23
24
namespace doris {
25
26
9.88k
void faststring::GrowToAtLeast(size_t newcapacity) {
27
    // Not enough space, need to reserve more.
28
    // Don't reserve exactly enough space for the new string -- that makes it
29
    // too easy to write perf bugs where you get O(n^2) append.
30
    // Instead, always expand by at least 50%.
31
32
9.88k
    if (newcapacity < capacity_ * 3 / 2) {
  Branch (32:9): [True: 9.87k, False: 11]
33
9.87k
        newcapacity = capacity_ * 3 / 2;
34
9.87k
    }
35
9.88k
    GrowArray(newcapacity);
36
9.88k
}
37
38
149k
void faststring::GrowArray(size_t newcapacity) {
39
149k
    DCHECK_GE(newcapacity, capacity_);
40
149k
    std::unique_ptr<uint8_t[]> newdata(reinterpret_cast<uint8_t*>(Allocator::alloc(newcapacity)));
41
149k
    if (len_ > 0) {
  Branch (41:9): [True: 68.0k, False: 81.1k]
42
68.0k
        memcpy(&newdata[0], &data_[0], len_);
43
68.0k
    }
44
45
149k
    if (data_ != initial_data_) {
  Branch (45:9): [True: 51.6k, False: 97.5k]
46
51.6k
        Allocator::free(data_, capacity_);
47
97.5k
    } else {
48
97.5k
        ASAN_POISON_MEMORY_REGION(initial_data_, arraysize(initial_data_));
Line
Count
Source
507
97.5k
#define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size))
49
97.5k
    }
50
51
149k
    data_ = newdata.release();
52
149k
    capacity_ = newcapacity;
53
149k
    ASAN_POISON_MEMORY_REGION(data_ + len_, capacity_ - len_);
Line
Count
Source
507
149k
#define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size))
54
149k
}
55
56
34
void faststring::ShrinkToFitInternal() {
57
34
    DCHECK_NE(data_, initial_data_);
58
34
    if (len_ <= kInitialCapacity) {
  Branch (58:9): [True: 29, False: 5]
59
29
        ASAN_UNPOISON_MEMORY_REGION(initial_data_, len_);
Line
Count
Source
508
29
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region((addr), (size))
60
29
        memcpy(initial_data_, &data_[0], len_);
61
29
        Allocator::free(data_, capacity_);
62
29
        data_ = initial_data_;
63
29
        capacity_ = kInitialCapacity;
64
29
    } else {
65
5
        std::unique_ptr<uint8_t[]> newdata(reinterpret_cast<uint8_t*>(Allocator::alloc(len_)));
66
5
        memcpy(&newdata[0], &data_[0], len_);
67
5
        Allocator::free(data_, capacity_);
68
5
        data_ = newdata.release();
69
5
        capacity_ = len_;
70
5
    }
71
34
}
72
73
} // namespace doris