Coverage Report

Created: 2026-07-23 15:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/coding.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style license that can be
7
// found in the LICENSE file. See the AUTHORS file for names of contributors.
8
9
#pragma once
10
11
#include <bit>
12
13
#ifndef __APPLE__
14
#include <endian.h>
15
#endif
16
#include <stdint.h>
17
#include <string.h>
18
19
#include "exec/common/endian.h"
20
#include "storage/olap_common.h"
21
#include "util/slice.h"
22
23
namespace doris {
24
// TODO(zc): add encode big endian later when we need it
25
// use big endian when we have order requirement.
26
// little endian is more efficient when we use X86 CPU, so
27
// when we have no order needs, we prefer little endian encoding
28
inline void encode_fixed8(uint8_t* buf, uint8_t val) {
29
    *buf = val;
30
}
31
32
10.1k
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
33
10.1k
    val = to_endian<std::endian::little>(val);
34
10.1k
    memcpy(buf, &val, sizeof(val));
35
10.1k
}
36
37
78.9M
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
38
78.9M
    val = to_endian<std::endian::little>(val);
39
78.9M
    memcpy(buf, &val, sizeof(val));
40
78.9M
}
41
42
136k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
43
136k
    val = to_endian<std::endian::little>(val);
44
136k
    memcpy(buf, &val, sizeof(val));
45
136k
}
46
47
24.4k
inline void encode_fixed128_le(uint8_t* buf, uint128_t val) {
48
24.4k
    val = to_endian<std::endian::little>(val);
49
24.4k
    memcpy(buf, &val, sizeof(val));
50
24.4k
}
51
52
4.54M
inline uint8_t decode_fixed8(const uint8_t* buf) {
53
4.54M
    return *buf;
54
4.54M
}
55
56
57.4k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
57
57.4k
    uint16_t res;
58
57.4k
    memcpy(&res, buf, sizeof(res));
59
57.4k
    return to_endian<std::endian::little>(res);
60
57.4k
}
61
62
241M
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
63
241M
    uint32_t res;
64
241M
    memcpy(&res, buf, sizeof(res));
65
241M
    return to_endian<std::endian::little>(res);
66
241M
}
67
68
54.4k
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
69
54.4k
    uint64_t res;
70
54.4k
    memcpy(&res, buf, sizeof(res));
71
54.4k
    return to_endian<std::endian::little>(res);
72
54.4k
}
73
74
415
inline void decode_fixed64_le_array(uint64_t* dst, const void* src, size_t n) {
75
415
    if (n == 0) {
76
0
        return;
77
0
    }
78
415
    if constexpr (std::endian::native == std::endian::little) {
79
415
        memcpy(dst, src, sizeof(uint64_t) * n);
80
    } else {
81
        const auto* ptr = reinterpret_cast<const uint8_t*>(src);
82
        for (size_t i = 0; i < n; ++i) {
83
            dst[i] = decode_fixed64_le(ptr);
84
            ptr += sizeof(uint64_t);
85
        }
86
    }
87
415
}
88
89
24.4k
inline uint128_t decode_fixed128_le(const uint8_t* buf) {
90
24.4k
    uint128_t res;
91
24.4k
    memcpy(&res, buf, sizeof(res));
92
24.4k
    return to_endian<std::endian::little>(res);
93
24.4k
}
94
95
template <typename T>
96
34.8M
void put_fixed32_le(T* dst, uint32_t val) {
97
34.8M
    uint8_t buf[sizeof(val)];
98
34.8M
    encode_fixed32_le(buf, val);
99
34.8M
    dst->append((char*)buf, sizeof(buf));
100
34.8M
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
96
2.52M
void put_fixed32_le(T* dst, uint32_t val) {
97
2.52M
    uint8_t buf[sizeof(val)];
98
2.52M
    encode_fixed32_le(buf, val);
99
2.52M
    dst->append((char*)buf, sizeof(buf));
100
2.52M
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
96
32.3M
void put_fixed32_le(T* dst, uint32_t val) {
97
32.3M
    uint8_t buf[sizeof(val)];
98
32.3M
    encode_fixed32_le(buf, val);
99
32.3M
    dst->append((char*)buf, sizeof(buf));
100
32.3M
}
101
102
template <typename T>
103
74.2k
void put_fixed64_le(T* dst, uint64_t val) {
104
74.2k
    uint8_t buf[sizeof(val)];
105
74.2k
    encode_fixed64_le(buf, val);
106
74.2k
    dst->append((char*)buf, sizeof(buf));
107
74.2k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
103
49.8k
void put_fixed64_le(T* dst, uint64_t val) {
104
49.8k
    uint8_t buf[sizeof(val)];
105
49.8k
    encode_fixed64_le(buf, val);
106
49.8k
    dst->append((char*)buf, sizeof(buf));
107
49.8k
}
_ZN5doris14put_fixed64_leINS_10faststringEEEvPT_m
Line
Count
Source
103
24.4k
void put_fixed64_le(T* dst, uint64_t val) {
104
24.4k
    uint8_t buf[sizeof(val)];
105
24.4k
    encode_fixed64_le(buf, val);
106
24.4k
    dst->append((char*)buf, sizeof(buf));
107
24.4k
}
108
109
// Returns the length of the varint32 or varint64 encoding of "v"
110
58
inline int varint_length(uint64_t v) {
111
58
    int len = 1;
112
58
    while (v >= 128) {
113
0
        v >>= 7;
114
0
        len++;
115
0
    }
116
58
    return len;
117
58
}
118
119
template <typename T>
120
24.4k
void put_fixed128_le(T* dst, uint128_t val) {
121
24.4k
    uint8_t buf[sizeof(val)];
122
24.4k
    encode_fixed128_le(buf, val);
123
24.4k
    dst->append((char*)buf, sizeof(buf));
124
24.4k
}
125
126
extern uint8_t* encode_varint32(uint8_t* dst, uint32_t value);
127
extern uint8_t* encode_varint64(uint8_t* dst, uint64_t value);
128
129
2.16M
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
130
2.16M
    static const unsigned int B = 128;
131
4.66M
    while (v >= B) {
132
        // Fetch low seven bits from current v, and the eight bit is marked as compression mark.
133
        // v | B is optimised from (v & (B-1)) | B, because result is assigned to uint8_t and other bits
134
        // is cleared by implicit conversion.
135
2.50M
        *(dst++) = uint8_t(v | B);
136
2.50M
        v >>= 7;
137
2.50M
    }
138
2.16M
    *(dst++) = static_cast<unsigned char>(v);
139
2.16M
    return dst;
140
2.16M
}
141
142
extern const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit,
143
                                                   uint32_t* value);
144
145
inline const uint8_t* decode_varint32_ptr(const uint8_t* ptr, const uint8_t* limit,
146
188M
                                          uint32_t* value) {
147
188M
    if (ptr < limit) {
148
188M
        uint32_t result = *ptr;
149
188M
        if ((result & 128) == 0) {
150
188M
            *value = result;
151
188M
            return ptr + 1;
152
188M
        }
153
188M
    }
154
409k
    return decode_varint32_ptr_fallback(ptr, limit, value);
155
188M
}
156
157
extern const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value);
158
159
template <typename T>
160
22.3M
void put_varint32(T* dst, uint32_t v) {
161
22.3M
    uint8_t buf[16];
162
22.3M
    uint8_t* ptr = encode_varint32(buf, v);
163
22.3M
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
164
22.3M
}
165
166
template <typename T>
167
void put_varint64(T* dst, uint64_t v) {
168
    uint8_t buf[16];
169
    uint8_t* ptr = encode_varint64(buf, v);
170
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
171
}
172
173
template <typename T>
174
2.16M
void put_length_prefixed_slice(T* dst, const Slice& value) {
175
2.16M
    put_varint32(dst, uint32_t(value.get_size()));
176
2.16M
    dst->append(value.get_data(), value.get_size());
177
2.16M
}
178
179
template <typename T>
180
2.16M
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
181
2.16M
    uint8_t buf[16];
182
2.16M
    uint8_t* ptr = encode_varint64(buf, v1);
183
2.16M
    ptr = encode_varint32(ptr, v2);
184
2.16M
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
185
2.16M
}
186
187
// parse a varint32 from the start of `input` into `val`.
188
// on success, return true and advance `input` past the parsed value.
189
// on failure, return false and `input` is not modified.
190
931k
inline bool get_varint32(Slice* input, uint32_t* val) {
191
931k
    const auto* p = (const uint8_t*)input->data;
192
931k
    const uint8_t* limit = p + input->size;
193
931k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
194
931k
    if (q == nullptr) {
195
0
        return false;
196
931k
    } else {
197
931k
        *input = Slice(q, limit - q);
198
931k
        return true;
199
931k
    }
200
931k
}
201
202
// parse a varint64 from the start of `input` into `val`.
203
// on success, return true and advance `input` past the parsed value.
204
// on failure, return false and `input` is not modified.
205
398k
inline bool get_varint64(Slice* input, uint64_t* val) {
206
398k
    const auto* p = (const uint8_t*)input->data;
207
398k
    const uint8_t* limit = p + input->size;
208
398k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
209
398k
    if (q == nullptr) {
210
0
        return false;
211
398k
    } else {
212
398k
        *input = Slice(q, limit - q);
213
398k
        return true;
214
398k
    }
215
398k
}
216
217
// parse a length-prefixed-slice from the start of `input` into `val`.
218
// on success, return true and advance `input` past the parsed value.
219
// on failure, return false and `input` may or may not be modified.
220
398k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
221
398k
    uint32_t len;
222
399k
    if (get_varint32(input, &len) && input->get_size() >= len) {
223
398k
        *val = Slice(input->get_data(), len);
224
398k
        input->remove_prefix(len);
225
398k
        return true;
226
18.4E
    } else {
227
18.4E
        return false;
228
18.4E
    }
229
398k
}
230
} // namespace doris