Coverage Report

Created: 2025-04-30 15:44

/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
391k
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
31
391k
#if __BYTE_ORDER == __LITTLE_ENDIAN
32
391k
    memcpy(buf, &val, sizeof(val));
33
#else
34
    uint16_t res = bswap_16(val);
35
    memcpy(buf, &res, sizeof(res));
36
#endif
37
391k
}
38
39
50.7M
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
40
50.7M
#if __BYTE_ORDER == __LITTLE_ENDIAN
41
50.7M
    memcpy(buf, &val, sizeof(val));
42
#else
43
    uint32_t res = bswap_32(val);
44
    memcpy(buf, &res, sizeof(res));
45
#endif
46
50.7M
}
47
48
298k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
49
298k
#if __BYTE_ORDER == __LITTLE_ENDIAN
50
298k
    memcpy(buf, &val, sizeof(val));
51
#else
52
    uint64_t res = gbswap_64(val);
53
    memcpy(buf, &res, sizeof(res));
54
#endif
55
298k
}
56
57
0
inline void encode_fixed128_le(uint8_t* buf, uint128_t val) {
58
0
#if __BYTE_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.76M
inline uint8_t decode_fixed8(const uint8_t* buf) {
67
4.76M
    return *buf;
68
4.76M
}
69
70
438k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
71
438k
    uint16_t res;
72
438k
    memcpy(&res, buf, sizeof(res));
73
438k
#if __BYTE_ORDER == __LITTLE_ENDIAN
74
438k
    return res;
75
#else
76
    return bswap_16(res);
77
#endif
78
438k
}
79
80
453M
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
81
453M
    uint32_t res;
82
453M
    memcpy(&res, buf, sizeof(res));
83
453M
#if __BYTE_ORDER == __LITTLE_ENDIAN
84
453M
    return res;
85
#else
86
    return bswap_32(res);
87
#endif
88
453M
}
89
90
1.92G
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
91
1.92G
    uint64_t res;
92
1.92G
    memcpy(&res, buf, sizeof(res));
93
1.92G
#if __BYTE_ORDER == __LITTLE_ENDIAN
94
1.92G
    return res;
95
#else
96
    return gbswap_64(res);
97
#endif
98
1.92G
}
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 == __LITTLE_ENDIAN
104
0
    return res;
105
#else
106
    return gbswap_128(res);
107
#endif
108
0
}
109
110
template <typename T>
111
43.8M
void put_fixed32_le(T* dst, uint32_t val) {
112
43.8M
    uint8_t buf[sizeof(val)];
113
43.8M
    encode_fixed32_le(buf, val);
114
43.8M
    dst->append((char*)buf, sizeof(buf));
115
43.8M
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
111
41.5M
void put_fixed32_le(T* dst, uint32_t val) {
112
41.5M
    uint8_t buf[sizeof(val)];
113
41.5M
    encode_fixed32_le(buf, val);
114
41.5M
    dst->append((char*)buf, sizeof(buf));
115
41.5M
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
111
2.32M
void put_fixed32_le(T* dst, uint32_t val) {
112
2.32M
    uint8_t buf[sizeof(val)];
113
2.32M
    encode_fixed32_le(buf, val);
114
2.32M
    dst->append((char*)buf, sizeof(buf));
115
2.32M
}
116
117
template <typename T>
118
168k
void put_fixed64_le(T* dst, uint64_t val) {
119
168k
    uint8_t buf[sizeof(val)];
120
168k
    encode_fixed64_le(buf, val);
121
168k
    dst->append((char*)buf, sizeof(buf));
122
168k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
118
168k
void put_fixed64_le(T* dst, uint64_t val) {
119
168k
    uint8_t buf[sizeof(val)];
120
168k
    encode_fixed64_le(buf, val);
121
168k
    dst->append((char*)buf, sizeof(buf));
122
168k
}
_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
45
inline int varint_length(uint64_t v) {
126
45
    int len = 1;
127
45
    while (v >= 128) {
128
0
        v >>= 7;
129
0
        len++;
130
0
    }
131
45
    return len;
132
45
}
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.99M
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
145
1.99M
    static const unsigned int B = 128;
146
4.32M
    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.33M
        *(dst++) = v | B;
151
2.33M
        v >>= 7;
152
2.33M
    }
153
1.99M
    *(dst++) = static_cast<unsigned char>(v);
154
1.99M
    return dst;
155
1.99M
}
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
173M
                                          uint32_t* value) {
162
173M
    if (ptr < limit) {
163
172M
        uint32_t result = *ptr;
164
173M
        if ((result & 128) == 0) {
165
173M
            *value = result;
166
173M
            return ptr + 1;
167
173M
        }
168
172M
    }
169
18.4E
    return decode_varint32_ptr_fallback(ptr, limit, value);
170
173M
}
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.99M
void put_length_prefixed_slice(T* dst, const Slice& value) {
190
1.99M
    put_varint32(dst, value.get_size());
191
1.99M
    dst->append(value.get_data(), value.get_size());
192
1.99M
}
193
194
template <typename T>
195
1.99M
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
196
1.99M
    uint8_t buf[16];
197
1.99M
    uint8_t* ptr = encode_varint64(buf, v1);
198
1.99M
    ptr = encode_varint32(ptr, v2);
199
1.99M
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
200
1.99M
}
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
426k
inline bool get_varint32(Slice* input, uint32_t* val) {
206
426k
    const uint8_t* p = (const uint8_t*)input->data;
207
426k
    const uint8_t* limit = p + input->size;
208
426k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
209
426k
    if (q == nullptr) {
210
0
        return false;
211
426k
    } else {
212
426k
        *input = Slice(q, limit - q);
213
426k
        return true;
214
426k
    }
215
426k
}
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
154k
inline bool get_varint64(Slice* input, uint64_t* val) {
221
154k
    const uint8_t* p = (const uint8_t*)input->data;
222
154k
    const uint8_t* limit = p + input->size;
223
154k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
224
154k
    if (q == nullptr) {
225
0
        return false;
226
154k
    } else {
227
154k
        *input = Slice(q, limit - q);
228
154k
        return true;
229
154k
    }
230
154k
}
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
154k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
236
154k
    uint32_t len;
237
154k
    if (get_varint32(input, &len) && input->get_size() >= len) {
238
154k
        *val = Slice(input->get_data(), len);
239
154k
        input->remove_prefix(len);
240
154k
        return true;
241
154k
    } else {
242
298
        return false;
243
298
    }
244
154k
}
245
246
} // namespace doris