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 | 2.96k | inline void encode_fixed16_le(uint8_t* buf, uint16_t val) { |
33 | 2.96k | val = to_endian<std::endian::little>(val); |
34 | 2.96k | memcpy(buf, &val, sizeof(val)); |
35 | 2.96k | } |
36 | | |
37 | 34.4M | inline void encode_fixed32_le(uint8_t* buf, uint32_t val) { |
38 | 34.4M | val = to_endian<std::endian::little>(val); |
39 | 34.4M | memcpy(buf, &val, sizeof(val)); |
40 | 34.4M | } |
41 | | |
42 | 233k | inline void encode_fixed64_le(uint8_t* buf, uint64_t val) { |
43 | 233k | val = to_endian<std::endian::little>(val); |
44 | 233k | memcpy(buf, &val, sizeof(val)); |
45 | 233k | } |
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 | 1.08M | inline uint8_t decode_fixed8(const uint8_t* buf) { |
53 | 1.08M | return *buf; |
54 | 1.08M | } |
55 | | |
56 | 49.1k | inline uint16_t decode_fixed16_le(const uint8_t* buf) { |
57 | 49.1k | uint16_t res; |
58 | 49.1k | memcpy(&res, buf, sizeof(res)); |
59 | 49.1k | return to_endian<std::endian::little>(res); |
60 | 49.1k | } |
61 | | |
62 | 81.7M | inline uint32_t decode_fixed32_le(const uint8_t* buf) { |
63 | 81.7M | uint32_t res; |
64 | 81.7M | memcpy(&res, buf, sizeof(res)); |
65 | 81.7M | return to_endian<std::endian::little>(res); |
66 | 81.7M | } |
67 | | |
68 | 44.1k | inline uint64_t decode_fixed64_le(const uint8_t* buf) { |
69 | 44.1k | uint64_t res; |
70 | 44.1k | memcpy(&res, buf, sizeof(res)); |
71 | 44.1k | return to_endian<std::endian::little>(res); |
72 | 44.1k | } |
73 | | |
74 | 66.8k | inline void decode_fixed64_le_array(uint64_t* dst, const void* src, size_t n) { |
75 | 66.8k | if (n == 0) { |
76 | 0 | return; |
77 | 0 | } |
78 | 66.8k | if constexpr (std::endian::native == std::endian::little) { |
79 | 66.8k | 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 | 66.8k | } |
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 | 24.3M | void put_fixed32_le(T* dst, uint32_t val) { |
97 | 24.3M | uint8_t buf[sizeof(val)]; |
98 | 24.3M | encode_fixed32_le(buf, val); |
99 | 24.3M | dst->append((char*)buf, sizeof(buf)); |
100 | 24.3M | } _ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j Line | Count | Source | 96 | 1.26M | void put_fixed32_le(T* dst, uint32_t val) { | 97 | 1.26M | uint8_t buf[sizeof(val)]; | 98 | 1.26M | encode_fixed32_le(buf, val); | 99 | 1.26M | dst->append((char*)buf, sizeof(buf)); | 100 | 1.26M | } |
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j Line | Count | Source | 96 | 23.1M | void put_fixed32_le(T* dst, uint32_t val) { | 97 | 23.1M | uint8_t buf[sizeof(val)]; | 98 | 23.1M | encode_fixed32_le(buf, val); | 99 | 23.1M | dst->append((char*)buf, sizeof(buf)); | 100 | 23.1M | } |
|
101 | | |
102 | | template <typename T> |
103 | 71.7k | void put_fixed64_le(T* dst, uint64_t val) { |
104 | 71.7k | uint8_t buf[sizeof(val)]; |
105 | 71.7k | encode_fixed64_le(buf, val); |
106 | 71.7k | dst->append((char*)buf, sizeof(buf)); |
107 | 71.7k | } _ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m Line | Count | Source | 103 | 47.3k | void put_fixed64_le(T* dst, uint64_t val) { | 104 | 47.3k | uint8_t buf[sizeof(val)]; | 105 | 47.3k | encode_fixed64_le(buf, val); | 106 | 47.3k | dst->append((char*)buf, sizeof(buf)); | 107 | 47.3k | } |
_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 | 36 | inline int varint_length(uint64_t v) { |
111 | 36 | int len = 1; |
112 | 36 | while (v >= 128) { |
113 | 0 | v >>= 7; |
114 | 0 | len++; |
115 | 0 | } |
116 | 36 | return len; |
117 | 36 | } |
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 | 1.10M | inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) { |
130 | 1.10M | static const unsigned int B = 128; |
131 | 2.49M | 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 | 1.39M | *(dst++) = uint8_t(v | B); |
136 | 1.39M | v >>= 7; |
137 | 1.39M | } |
138 | 1.10M | *(dst++) = static_cast<unsigned char>(v); |
139 | 1.10M | return dst; |
140 | 1.10M | } |
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 | 36.8M | uint32_t* value) { |
147 | 36.8M | if (ptr < limit) { |
148 | 36.8M | uint32_t result = *ptr; |
149 | 36.8M | if ((result & 128) == 0) { |
150 | 34.2M | *value = result; |
151 | 34.2M | return ptr + 1; |
152 | 34.2M | } |
153 | 36.8M | } |
154 | 2.56M | return decode_varint32_ptr_fallback(ptr, limit, value); |
155 | 36.8M | } |
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 | 8.55M | void put_varint32(T* dst, uint32_t v) { |
161 | 8.55M | uint8_t buf[16]; |
162 | 8.55M | uint8_t* ptr = encode_varint32(buf, v); |
163 | 8.55M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
164 | 8.55M | } |
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 | 1.10M | void put_length_prefixed_slice(T* dst, const Slice& value) { |
175 | 1.10M | put_varint32(dst, uint32_t(value.get_size())); |
176 | 1.10M | dst->append(value.get_data(), value.get_size()); |
177 | 1.10M | } |
178 | | |
179 | | template <typename T> |
180 | 1.10M | void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) { |
181 | 1.10M | uint8_t buf[16]; |
182 | 1.10M | uint8_t* ptr = encode_varint64(buf, v1); |
183 | 1.10M | ptr = encode_varint32(ptr, v2); |
184 | 1.10M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
185 | 1.10M | } |
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 | 4.76M | inline bool get_varint32(Slice* input, uint32_t* val) { |
191 | 4.76M | const auto* p = (const uint8_t*)input->data; |
192 | 4.76M | const uint8_t* limit = p + input->size; |
193 | 4.76M | const uint8_t* q = decode_varint32_ptr(p, limit, val); |
194 | 4.76M | if (q == nullptr) { |
195 | 0 | return false; |
196 | 4.76M | } else { |
197 | 4.76M | *input = Slice(q, limit - q); |
198 | 4.76M | return true; |
199 | 4.76M | } |
200 | 4.76M | } |
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 | 2.14M | inline bool get_varint64(Slice* input, uint64_t* val) { |
206 | 2.14M | const auto* p = (const uint8_t*)input->data; |
207 | 2.14M | const uint8_t* limit = p + input->size; |
208 | 2.14M | const uint8_t* q = decode_varint64_ptr(p, limit, val); |
209 | 2.14M | if (q == nullptr) { |
210 | 0 | return false; |
211 | 2.14M | } else { |
212 | 2.14M | *input = Slice(q, limit - q); |
213 | 2.14M | return true; |
214 | 2.14M | } |
215 | 2.14M | } |
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 | 2.14M | inline bool get_length_prefixed_slice(Slice* input, Slice* val) { |
221 | 2.14M | uint32_t len; |
222 | 2.14M | if (get_varint32(input, &len) && input->get_size() >= len) { |
223 | 2.14M | *val = Slice(input->get_data(), len); |
224 | 2.14M | input->remove_prefix(len); |
225 | 2.14M | return true; |
226 | 18.4E | } else { |
227 | 18.4E | return false; |
228 | 18.4E | } |
229 | 2.14M | } |
230 | | } // namespace doris |