Coverage Report

Created: 2025-06-11 05:33

/root/doris/be/src/util/coding.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef __APPLE__
12
#include <endian.h>
13
#endif
14
#include <stdint.h>
15
#include <string.h>
16
17
#include "olap/olap_common.h"
18
#include "util/slice.h"
19
20
namespace doris {
21
22
// TODO(zc): add encode big endian later when we need it
23
// use big endian when we have order requirement.
24
// little endian is more efficient when we use X86 CPU, so
25
// when we have no order needs, we prefer little endian encoding
26
inline void encode_fixed8(uint8_t* buf, uint8_t val) {
27
    *buf = val;
28
}
29
30
214k
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
31
214k
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
32
214k
    memcpy(buf, &val, sizeof(val));
33
#else
34
    uint16_t res = bswap_16(val);
35
    memcpy(buf, &res, sizeof(res));
36
#endif
37
214k
}
38
39
50.0M
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
40
50.0M
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
41
50.0M
    memcpy(buf, &val, sizeof(val));
42
#else
43
    uint32_t res = bswap_32(val);
44
    memcpy(buf, &res, sizeof(res));
45
#endif
46
50.0M
}
47
48
277k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
49
277k
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
50
277k
    memcpy(buf, &val, sizeof(val));
51
#else
52
    uint64_t res = gbswap_64(val);
53
    memcpy(buf, &res, sizeof(res));
54
#endif
55
277k
}
56
57
0
inline void encode_fixed128_le(uint8_t* buf, uint128_t val) {
58
0
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
59
0
    memcpy(buf, &val, sizeof(val));
60
#else
61
    uint128_t res = gbswap_128(val);
62
    memcpy(buf, &res, sizeof(res));
63
#endif
64
0
}
65
66
4.32M
inline uint8_t decode_fixed8(const uint8_t* buf) {
67
4.32M
    return *buf;
68
4.32M
}
69
70
261k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
71
261k
    uint16_t res;
72
261k
    memcpy(&res, buf, sizeof(res));
73
261k
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
74
261k
    return res;
75
#else
76
    return bswap_16(res);
77
#endif
78
261k
}
79
80
267M
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
81
267M
    uint32_t res;
82
267M
    memcpy(&res, buf, sizeof(res));
83
267M
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
84
267M
    return res;
85
#else
86
    return bswap_32(res);
87
#endif
88
267M
}
89
90
1.79G
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
91
1.79G
    uint64_t res;
92
1.79G
    memcpy(&res, buf, sizeof(res));
93
1.79G
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
94
1.79G
    return res;
95
#else
96
    return gbswap_64(res);
97
#endif
98
1.79G
}
99
100
0
inline uint128_t decode_fixed128_le(const uint8_t* buf) {
101
0
    uint128_t res;
102
0
    memcpy(&res, buf, sizeof(res));
103
0
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
104
0
    return res;
105
#else
106
    return gbswap_128(res);
107
#endif
108
0
}
109
110
template <typename T>
111
43.3M
void put_fixed32_le(T* dst, uint32_t val) {
112
43.3M
    uint8_t buf[sizeof(val)];
113
43.3M
    encode_fixed32_le(buf, val);
114
43.3M
    dst->append((char*)buf, sizeof(buf));
115
43.3M
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
111
41.0M
void put_fixed32_le(T* dst, uint32_t val) {
112
41.0M
    uint8_t buf[sizeof(val)];
113
41.0M
    encode_fixed32_le(buf, val);
114
41.0M
    dst->append((char*)buf, sizeof(buf));
115
41.0M
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
111
2.26M
void put_fixed32_le(T* dst, uint32_t val) {
112
2.26M
    uint8_t buf[sizeof(val)];
113
2.26M
    encode_fixed32_le(buf, val);
114
2.26M
    dst->append((char*)buf, sizeof(buf));
115
2.26M
}
116
117
template <typename T>
118
166k
void put_fixed64_le(T* dst, uint64_t val) {
119
166k
    uint8_t buf[sizeof(val)];
120
166k
    encode_fixed64_le(buf, val);
121
166k
    dst->append((char*)buf, sizeof(buf));
122
166k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
118
166k
void put_fixed64_le(T* dst, uint64_t val) {
119
166k
    uint8_t buf[sizeof(val)];
120
166k
    encode_fixed64_le(buf, val);
121
166k
    dst->append((char*)buf, sizeof(buf));
122
166k
}
_ZN5doris14put_fixed64_leINS_10faststringEEEvPT_m
Line
Count
Source
118
25
void put_fixed64_le(T* dst, uint64_t val) {
119
25
    uint8_t buf[sizeof(val)];
120
25
    encode_fixed64_le(buf, val);
121
25
    dst->append((char*)buf, sizeof(buf));
122
25
}
123
124
// Returns the length of the varint32 or varint64 encoding of "v"
125
49
inline int varint_length(uint64_t v) {
126
49
    int len = 1;
127
49
    while (v >= 128) {
128
0
        v >>= 7;
129
0
        len++;
130
0
    }
131
49
    return len;
132
49
}
133
134
template <typename T>
135
0
void put_fixed128_le(T* dst, uint128_t val) {
136
0
    uint8_t buf[sizeof(val)];
137
0
    encode_fixed128_le(buf, val);
138
0
    dst->append((char*)buf, sizeof(buf));
139
0
}
140
141
extern uint8_t* encode_varint32(uint8_t* dst, uint32_t value);
142
extern uint8_t* encode_varint64(uint8_t* dst, uint64_t value);
143
144
1.94M
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
145
1.94M
    static const unsigned int B = 128;
146
4.20M
    while (v >= B) {
147
        // Fetch low seven bits from current v, and the eight bit is marked as compression mark.
148
        // v | B is optimised from (v & (B-1)) | B, because result is assigned to uint8_t and other bits
149
        // is cleared by implicit conversion.
150
2.26M
        *(dst++) = v | B;
151
2.26M
        v >>= 7;
152
2.26M
    }
153
1.94M
    *(dst++) = static_cast<unsigned char>(v);
154
1.94M
    return dst;
155
1.94M
}
156
157
extern const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit,
158
                                                   uint32_t* value);
159
160
inline const uint8_t* decode_varint32_ptr(const uint8_t* ptr, const uint8_t* limit,
161
160M
                                          uint32_t* value) {
162
161M
    if (ptr < limit) {
163
161M
        uint32_t result = *ptr;
164
161M
        if ((result & 128) == 0) {
165
161M
            *value = result;
166
161M
            return ptr + 1;
167
161M
        }
168
161M
    }
169
18.4E
    return decode_varint32_ptr_fallback(ptr, limit, value);
170
160M
}
171
172
extern const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value);
173
174
template <typename T>
175
34.7M
void put_varint32(T* dst, uint32_t v) {
176
34.7M
    uint8_t buf[16];
177
34.7M
    uint8_t* ptr = encode_varint32(buf, v);
178
34.7M
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
179
34.7M
}
180
181
template <typename T>
182
void put_varint64(T* dst, uint64_t v) {
183
    uint8_t buf[16];
184
    uint8_t* ptr = encode_varint64(buf, v);
185
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
186
}
187
188
template <typename T>
189
1.94M
void put_length_prefixed_slice(T* dst, const Slice& value) {
190
1.94M
    put_varint32(dst, value.get_size());
191
1.94M
    dst->append(value.get_data(), value.get_size());
192
1.94M
}
193
194
template <typename T>
195
1.94M
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
196
1.94M
    uint8_t buf[16];
197
1.94M
    uint8_t* ptr = encode_varint64(buf, v1);
198
1.94M
    ptr = encode_varint32(ptr, v2);
199
1.94M
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
200
1.94M
}
201
202
// parse a varint32 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
484k
inline bool get_varint32(Slice* input, uint32_t* val) {
206
484k
    const uint8_t* p = (const uint8_t*)input->data;
207
484k
    const uint8_t* limit = p + input->size;
208
484k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
209
484k
    if (q == nullptr) {
210
0
        return false;
211
484k
    } else {
212
484k
        *input = Slice(q, limit - q);
213
484k
        return true;
214
484k
    }
215
484k
}
216
217
// parse a varint64 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` is not modified.
220
180k
inline bool get_varint64(Slice* input, uint64_t* val) {
221
180k
    const uint8_t* p = (const uint8_t*)input->data;
222
180k
    const uint8_t* limit = p + input->size;
223
180k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
224
180k
    if (q == nullptr) {
225
0
        return false;
226
180k
    } else {
227
180k
        *input = Slice(q, limit - q);
228
180k
        return true;
229
180k
    }
230
180k
}
231
232
// parse a length-prefixed-slice from the start of `input` into `val`.
233
// on success, return true and advance `input` past the parsed value.
234
// on failure, return false and `input` may or may not be modified.
235
181k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
236
181k
    uint32_t len;
237
181k
    if (get_varint32(input, &len) && input->get_size() >= len) {
238
180k
        *val = Slice(input->get_data(), len);
239
180k
        input->remove_prefix(len);
240
180k
        return true;
241
180k
    } else {
242
741
        return false;
243
741
    }
244
181k
}
245
246
} // namespace doris