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