Coverage Report

Created: 2025-09-15 16:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 "vec/common/custom_allocator.h"
23
24
namespace doris {
25
26
1.14M
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
1.14M
    if (newcapacity < capacity_ * 3 / 2) {
33
883k
        newcapacity = capacity_ * 3 / 2;
34
883k
    }
35
1.14M
    GrowArray(newcapacity);
36
1.14M
}
37
38
6.49M
void faststring::GrowArray(size_t newcapacity) {
39
6.49M
    DCHECK_GE(newcapacity, capacity_);
40
6.49M
    DorisUniqueBufferPtr<uint8_t> newdata(newcapacity);
41
6.49M
    if (len_ > 0) {
42
1.59M
        memcpy(&newdata[0], &data_[0], len_);
43
1.59M
    }
44
45
6.49M
    if (data_ != initial_data_) {
46
701k
        Allocator::free(data_, capacity_);
47
5.79M
    } else {
48
5.79M
        ASAN_POISON_MEMORY_REGION(initial_data_, arraysize(initial_data_));
49
5.79M
    }
50
51
6.49M
    data_ = newdata.release();
52
6.49M
    capacity_ = newcapacity;
53
6.49M
    ASAN_POISON_MEMORY_REGION(data_ + len_, capacity_ - len_);
54
6.49M
}
55
56
29
void faststring::ShrinkToFitInternal() {
57
29
    DCHECK_NE(data_, initial_data_);
58
29
    if (len_ <= kInitialCapacity) {
59
25
        ASAN_UNPOISON_MEMORY_REGION(initial_data_, len_);
60
25
        memcpy(initial_data_, &data_[0], len_);
61
25
        Allocator::free(data_, capacity_);
62
25
        data_ = initial_data_;
63
25
        capacity_ = kInitialCapacity;
64
25
    } else {
65
4
        DorisUniqueBufferPtr<uint8_t> newdata(len_);
66
4
        memcpy(&newdata[0], &data_[0], len_);
67
4
        Allocator::free(data_, capacity_);
68
4
        data_ = newdata.release();
69
4
        capacity_ = len_;
70
4
    }
71
29
}
72
73
} // namespace doris