be/src/util/rle_encoding.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | #pragma once |
18 | | |
19 | | #include <glog/logging.h> |
20 | | |
21 | | #include <algorithm> |
22 | | #include <cstdint> |
23 | | #include <limits> // IWYU pragma: keep |
24 | | |
25 | | #include "common/cast_set.h" |
26 | | #include "util/bit_stream_utils.inline.h" |
27 | | #include "util/bit_util.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | | // Utility classes to do run length encoding (RLE) for fixed bit width values. If runs |
32 | | // are sufficiently long, RLE is used, otherwise, the values are just bit-packed |
33 | | // (literal encoding). |
34 | | // For both types of runs, there is a byte-aligned indicator which encodes the length |
35 | | // of the run and the type of the run. |
36 | | // This encoding has the benefit that when there aren't any long enough runs, values |
37 | | // are always decoded at fixed (can be precomputed) bit offsets OR both the value and |
38 | | // the run length are byte aligned. This allows for very efficient decoding |
39 | | // implementations. |
40 | | // The encoding is: |
41 | | // encoded-block := run* |
42 | | // run := literal-run | repeated-run |
43 | | // literal-run := literal-indicator < literal bytes > |
44 | | // repeated-run := repeated-indicator < repeated value. padded to byte boundary > |
45 | | // literal-indicator := varint_encode( number_of_groups << 1 | 1) |
46 | | // repeated-indicator := varint_encode( number_of_repetitions << 1 ) |
47 | | // |
48 | | // Each run is preceded by a varint. The varint's least significant bit is |
49 | | // used to indicate whether the run is a literal run or a repeated run. The rest |
50 | | // of the varint is used to determine the length of the run (eg how many times the |
51 | | // value repeats). |
52 | | // |
53 | | // In the case of literal runs, the run length is always a multiple of 8 (i.e. encode |
54 | | // in groups of 8), so that no matter the bit-width of the value, the sequence will end |
55 | | // on a byte boundary without padding. |
56 | | // Given that we know it is a multiple of 8, we store the number of 8-groups rather than |
57 | | // the actual number of encoded ints. (This means that the total number of encoded values |
58 | | // can not be determined from the encoded data, since the number of values in the last |
59 | | // group may not be a multiple of 8). |
60 | | // There is a break-even point when it is more storage efficient to do run length |
61 | | // encoding. For 1 bit-width values, that point is 8 values. They require 2 bytes |
62 | | // for both the repeated encoding or the literal encoding. This value can always |
63 | | // be computed based on the bit-width. |
64 | | // TODO: think about how to use this for strings. The bit packing isn't quite the same. |
65 | | // |
66 | | // Examples with bit-width 1 (eg encoding booleans): |
67 | | // ---------------------------------------- |
68 | | // 100 1s followed by 100 0s: |
69 | | // <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte> |
70 | | // - (total 4 bytes) |
71 | | // |
72 | | // alternating 1s and 0s (200 total): |
73 | | // 200 ints = 25 groups of 8 |
74 | | // <varint((25 << 1) | 1)> <25 bytes of values, bitpacked> |
75 | | // (total 26 bytes, 1 byte overhead) |
76 | | // |
77 | | |
78 | | // Decoder class for RLE encoded data. |
79 | | // |
80 | | // NOTE: the encoded format does not have any length prefix or any other way of |
81 | | // indicating that the encoded sequence ends at a certain point, so the Decoder |
82 | | // methods may return some extra bits at the end before the read methods start |
83 | | // to return 0/false. |
84 | | template <typename T> |
85 | | class RleDecoder { |
86 | | public: |
87 | | // Create a decoder object. buffer/buffer_len is the decoded data. |
88 | | // bit_width is the width of each value (before encoding). |
89 | | RleDecoder(const uint8_t* buffer, int buffer_len, int bit_width) |
90 | 232k | : bit_reader_(buffer, buffer_len), |
91 | 232k | bit_width_(bit_width), |
92 | 232k | current_value_(0), |
93 | 232k | repeat_count_(0), |
94 | 232k | literal_count_(0), |
95 | 232k | rewind_state_(CANT_REWIND) { |
96 | 232k | DCHECK_GE(bit_width_, 1); |
97 | 232k | DCHECK_LE(bit_width_, 64); |
98 | 232k | } _ZN5doris10RleDecoderIsEC2EPKhii Line | Count | Source | 90 | 626 | : bit_reader_(buffer, buffer_len), | 91 | 626 | bit_width_(bit_width), | 92 | 626 | current_value_(0), | 93 | 626 | repeat_count_(0), | 94 | 626 | literal_count_(0), | 95 | 626 | rewind_state_(CANT_REWIND) { | 96 | 626 | DCHECK_GE(bit_width_, 1); | 97 | | DCHECK_LE(bit_width_, 64); | 98 | 626 | } |
_ZN5doris10RleDecoderIhEC2EPKhii Line | Count | Source | 90 | 29.3k | : bit_reader_(buffer, buffer_len), | 91 | 29.3k | bit_width_(bit_width), | 92 | 29.3k | current_value_(0), | 93 | 29.3k | repeat_count_(0), | 94 | 29.3k | literal_count_(0), | 95 | 29.3k | rewind_state_(CANT_REWIND) { | 96 | 29.3k | DCHECK_GE(bit_width_, 1); | 97 | | DCHECK_LE(bit_width_, 64); | 98 | 29.3k | } |
_ZN5doris10RleDecoderIbEC2EPKhii Line | Count | Source | 90 | 202k | : bit_reader_(buffer, buffer_len), | 91 | 202k | bit_width_(bit_width), | 92 | 202k | current_value_(0), | 93 | 202k | repeat_count_(0), | 94 | 202k | literal_count_(0), | 95 | 202k | rewind_state_(CANT_REWIND) { | 96 | 202k | DCHECK_GE(bit_width_, 1); | 97 | | DCHECK_LE(bit_width_, 64); | 98 | 202k | } |
|
99 | | |
100 | 29.2M | RleDecoder() {}_ZN5doris10RleDecoderIsEC2Ev Line | Count | Source | 100 | 1.30k | RleDecoder() {} |
_ZN5doris10RleDecoderIhEC2Ev Line | Count | Source | 100 | 27.5k | RleDecoder() {} |
_ZN5doris10RleDecoderIbEC2Ev Line | Count | Source | 100 | 29.2M | RleDecoder() {} |
|
101 | | |
102 | | // Skip n values, and returns the number of non-zero entries skipped. |
103 | | size_t Skip(size_t to_skip); |
104 | | |
105 | | // Gets the next value. Returns false if there are no more. |
106 | | bool Get(T* val); |
107 | | |
108 | | // Seek to the previous value. |
109 | | void RewindOne(); |
110 | | |
111 | | // Gets the next run of the same 'val'. Returns 0 if there is no |
112 | | // more data to be decoded. Will return a run of at most 'max_run' |
113 | | // values. If there are more values than this, the next call to |
114 | | // GetNextRun will return more from the same run. |
115 | | size_t GetNextRun(T* val, size_t max_run); |
116 | | |
117 | | size_t get_values(T* values, size_t num_values); |
118 | | |
119 | | // Get the count of current repeated value |
120 | | size_t repeated_count(); |
121 | | |
122 | | // Get current repeated value, make sure that count equals repeated_count() |
123 | | T get_repeated_value(size_t count); |
124 | | |
125 | 0 | const BitReader& bit_reader() const { return bit_reader_; } |
126 | | |
127 | | private: |
128 | | bool ReadHeader(); |
129 | | |
130 | | enum RewindState { REWIND_LITERAL, REWIND_RUN, CANT_REWIND }; |
131 | | |
132 | | BitReader bit_reader_; |
133 | | int bit_width_; |
134 | | uint64_t current_value_; |
135 | | uint32_t repeat_count_; |
136 | | uint32_t literal_count_; |
137 | | RewindState rewind_state_; |
138 | | }; |
139 | | |
140 | | // Class to incrementally build the rle data. |
141 | | // The encoding has two modes: encoding repeated runs and literal runs. |
142 | | // If the run is sufficiently short, it is more efficient to encode as a literal run. |
143 | | // This class does so by buffering 8 values at a time. If they are not all the same |
144 | | // they are added to the literal run. If they are the same, they are added to the |
145 | | // repeated run. When we switch modes, the previous run is flushed out. |
146 | | template <typename T> |
147 | | class RleEncoder { |
148 | | public: |
149 | | // buffer: buffer to write bits to. |
150 | | // bit_width: max number of bits for value. |
151 | | // TODO: consider adding a min_repeated_run_length so the caller can control |
152 | | // when values should be encoded as repeated runs. Currently this is derived |
153 | | // based on the bit_width, which can determine a storage optimal choice. |
154 | | explicit RleEncoder(faststring* buffer, int bit_width) |
155 | 568k | : bit_width_(bit_width), bit_writer_(buffer) { |
156 | 568k | DCHECK_GE(bit_width_, 1); |
157 | 568k | DCHECK_LE(bit_width_, 64); |
158 | 568k | Clear(); |
159 | 568k | } _ZN5doris10RleEncoderIbEC2EPNS_10faststringEi Line | Count | Source | 155 | 554k | : bit_width_(bit_width), bit_writer_(buffer) { | 156 | 554k | DCHECK_GE(bit_width_, 1); | 157 | | DCHECK_LE(bit_width_, 64); | 158 | 554k | Clear(); | 159 | 554k | } |
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi Line | Count | Source | 155 | 13.4k | : bit_width_(bit_width), bit_writer_(buffer) { | 156 | 13.4k | DCHECK_GE(bit_width_, 1); | 157 | | DCHECK_LE(bit_width_, 64); | 158 | 13.4k | Clear(); | 159 | 13.4k | } |
|
160 | | |
161 | | // The largest number of values a single repeated run can describe. |
162 | | // |
163 | | // A repeated run is written as a vlq-encoded indicator holding |
164 | | // `run_length << 1` (see FlushRepeatedRun) and read back into a uint32_t by |
165 | | // BitReader::GetVlqInt. Capping the run length here keeps the indicator |
166 | | // inside int32_t's positive range, which is exactly the range every released |
167 | | // version could already encode, so the output stays byte-identical for any |
168 | | // run length that used to work. |
169 | | static constexpr int64_t MAX_VALUES_PER_REPEATED_RUN = (int64_t {1} << 30) - 1; |
170 | | |
171 | | // Reserve 'num_bytes' bytes for a plain encoded header, set each |
172 | | // byte with 'val': this is used for the RLE-encoded data blocks in |
173 | | // order to be able to able to store the initial ordinal position |
174 | | // and number of elements. This is a part of RleEncoder in order to |
175 | | // maintain the correct offset in 'buffer'. |
176 | | void Reserve(int num_bytes, uint8_t val); |
177 | | |
178 | | // Encode value. This value must be representable with bit_width_ bits. |
179 | | void Put(T value, size_t run_length = 1); |
180 | | |
181 | | // Flushes any pending values to the underlying buffer. |
182 | | // Returns the total number of bytes written |
183 | | int Flush(); |
184 | | |
185 | | // Resets all the state in the encoder. |
186 | | void Clear(); |
187 | | |
188 | 223k | int32_t len() const { return bit_writer_.bytes_written(); } |
189 | | |
190 | | private: |
191 | | // Flushes any buffered values. If this is part of a repeated run, this is largely |
192 | | // a no-op. |
193 | | // If it is part of a literal run, this will call FlushLiteralRun, which writes |
194 | | // out the buffered literal values. |
195 | | // If 'done' is true, the current run would be written even if it would normally |
196 | | // have been buffered more. This should only be called at the end, when the |
197 | | // encoder has received all values even if it would normally continue to be |
198 | | // buffered. |
199 | | void FlushBufferedValues(bool done); |
200 | | |
201 | | // Flushes literal values to the underlying buffer. If update_indicator_byte, |
202 | | // then the current literal run is complete and the indicator byte is updated. |
203 | | void FlushLiteralRun(bool update_indicator_byte); |
204 | | |
205 | | // Flushes a repeated run to the underlying buffer. |
206 | | void FlushRepeatedRun(); |
207 | | |
208 | | // Number of bits needed to encode the value. |
209 | | const int bit_width_; |
210 | | |
211 | | // Underlying buffer. |
212 | | BitWriter bit_writer_; |
213 | | |
214 | | // We need to buffer at most 8 values for literals. This happens when the |
215 | | // bit_width is 1 (so 8 values fit in one byte). |
216 | | // TODO: generalize this to other bit widths |
217 | | uint64_t buffered_values_[8]; |
218 | | |
219 | | // Number of values in buffered_values_ |
220 | | int num_buffered_values_; |
221 | | |
222 | | // The current (also last) value that was written and the count of how |
223 | | // many times in a row that value has been seen. This is maintained even |
224 | | // if we are in a literal run. If the repeat_count_ get high enough, we switch |
225 | | // to encoding repeated runs. |
226 | | uint64_t current_value_; |
227 | | int64_t repeat_count_; |
228 | | |
229 | | // Number of literals in the current run. This does not include the literals |
230 | | // that might be in buffered_values_. Only after we've got a group big enough |
231 | | // can we decide if they should part of the literal_count_ or repeat_count_ |
232 | | int literal_count_; |
233 | | |
234 | | // Index of a byte in the underlying buffer that stores the indicator byte. |
235 | | // This is reserved as soon as we need a literal run but the value is written |
236 | | // when the literal run is complete. We maintain an index rather than a pointer |
237 | | // into the underlying buffer because the pointer value may become invalid if |
238 | | // the underlying buffer is resized. |
239 | | int literal_indicator_byte_idx_; |
240 | | }; |
241 | | |
242 | | template <typename T> |
243 | 14.5M | bool RleDecoder<T>::ReadHeader() { |
244 | 14.5M | DCHECK(bit_reader_.is_initialized()); |
245 | 14.5M | if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] { |
246 | | // Read the next run's indicator int, it could be a literal or repeated run |
247 | | // The int is encoded as a vlq-encoded value. |
248 | 1.37M | uint32_t indicator_value = 0; |
249 | 1.37M | bool result = bit_reader_.GetVlqInt(&indicator_value); |
250 | 1.37M | if (!result) [[unlikely]] { |
251 | 2.68k | return false; |
252 | 2.68k | } |
253 | | |
254 | | // lsb indicates if it is a literal run or repeated run |
255 | 1.37M | bool is_literal = indicator_value & 1; |
256 | 1.37M | if (is_literal) { |
257 | 665k | literal_count_ = (indicator_value >> 1) * 8; |
258 | 665k | DCHECK_GT(literal_count_, 0); |
259 | 706k | } else { |
260 | 706k | repeat_count_ = indicator_value >> 1; |
261 | 706k | DCHECK_GT(repeat_count_, 0); |
262 | 706k | bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8), |
263 | 706k | reinterpret_cast<T*>(¤t_value_)); |
264 | 706k | DCHECK(result1); |
265 | 706k | } |
266 | 1.37M | } |
267 | 14.5M | return true; |
268 | 14.5M | } _ZN5doris10RleDecoderIsE10ReadHeaderEv Line | Count | Source | 243 | 1.19k | bool RleDecoder<T>::ReadHeader() { | 244 | 1.19k | DCHECK(bit_reader_.is_initialized()); | 245 | 1.19k | if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] { | 246 | | // Read the next run's indicator int, it could be a literal or repeated run | 247 | | // The int is encoded as a vlq-encoded value. | 248 | 634 | uint32_t indicator_value = 0; | 249 | 634 | bool result = bit_reader_.GetVlqInt(&indicator_value); | 250 | 634 | if (!result) [[unlikely]] { | 251 | 0 | return false; | 252 | 0 | } | 253 | | | 254 | | // lsb indicates if it is a literal run or repeated run | 255 | 634 | bool is_literal = indicator_value & 1; | 256 | 634 | if (is_literal) { | 257 | 53 | literal_count_ = (indicator_value >> 1) * 8; | 258 | 53 | DCHECK_GT(literal_count_, 0); | 259 | 581 | } else { | 260 | 581 | repeat_count_ = indicator_value >> 1; | 261 | 581 | DCHECK_GT(repeat_count_, 0); | 262 | 581 | bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8), | 263 | 581 | reinterpret_cast<T*>(¤t_value_)); | 264 | 581 | DCHECK(result1); | 265 | 581 | } | 266 | 634 | } | 267 | 1.19k | return true; | 268 | 1.19k | } |
_ZN5doris10RleDecoderIhE10ReadHeaderEv Line | Count | Source | 243 | 7.99M | bool RleDecoder<T>::ReadHeader() { | 244 | 7.99M | DCHECK(bit_reader_.is_initialized()); | 245 | 7.99M | if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] { | 246 | | // Read the next run's indicator int, it could be a literal or repeated run | 247 | | // The int is encoded as a vlq-encoded value. | 248 | 100k | uint32_t indicator_value = 0; | 249 | 100k | bool result = bit_reader_.GetVlqInt(&indicator_value); | 250 | 100k | if (!result) [[unlikely]] { | 251 | 0 | return false; | 252 | 0 | } | 253 | | | 254 | | // lsb indicates if it is a literal run or repeated run | 255 | 100k | bool is_literal = indicator_value & 1; | 256 | 100k | if (is_literal) { | 257 | 63.5k | literal_count_ = (indicator_value >> 1) * 8; | 258 | 63.5k | DCHECK_GT(literal_count_, 0); | 259 | 63.5k | } else { | 260 | 37.2k | repeat_count_ = indicator_value >> 1; | 261 | 37.2k | DCHECK_GT(repeat_count_, 0); | 262 | 37.2k | bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8), | 263 | 37.2k | reinterpret_cast<T*>(¤t_value_)); | 264 | 37.2k | DCHECK(result1); | 265 | 37.2k | } | 266 | 100k | } | 267 | 7.99M | return true; | 268 | 7.99M | } |
_ZN5doris10RleDecoderIbE10ReadHeaderEv Line | Count | Source | 243 | 6.52M | bool RleDecoder<T>::ReadHeader() { | 244 | 6.52M | DCHECK(bit_reader_.is_initialized()); | 245 | 6.52M | if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] { | 246 | | // Read the next run's indicator int, it could be a literal or repeated run | 247 | | // The int is encoded as a vlq-encoded value. | 248 | 1.27M | uint32_t indicator_value = 0; | 249 | 1.27M | bool result = bit_reader_.GetVlqInt(&indicator_value); | 250 | 1.27M | if (!result) [[unlikely]] { | 251 | 2.68k | return false; | 252 | 2.68k | } | 253 | | | 254 | | // lsb indicates if it is a literal run or repeated run | 255 | 1.27M | bool is_literal = indicator_value & 1; | 256 | 1.27M | if (is_literal) { | 257 | 602k | literal_count_ = (indicator_value >> 1) * 8; | 258 | 602k | DCHECK_GT(literal_count_, 0); | 259 | 668k | } else { | 260 | 668k | repeat_count_ = indicator_value >> 1; | 261 | 668k | DCHECK_GT(repeat_count_, 0); | 262 | 668k | bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8), | 263 | 668k | reinterpret_cast<T*>(¤t_value_)); | 264 | 668k | DCHECK(result1); | 265 | 668k | } | 266 | 1.27M | } | 267 | 6.52M | return true; | 268 | 6.52M | } |
|
269 | | |
270 | | template <typename T> |
271 | 7.64M | bool RleDecoder<T>::Get(T* val) { |
272 | 7.64M | DCHECK(bit_reader_.is_initialized()); |
273 | 7.64M | if (!ReadHeader()) [[unlikely]] { |
274 | 0 | return false; |
275 | 0 | } |
276 | | |
277 | 7.64M | if (repeat_count_ > 0) [[likely]] { |
278 | 241k | *val = cast_set<T>(current_value_); |
279 | 241k | --repeat_count_; |
280 | 241k | rewind_state_ = REWIND_RUN; |
281 | 7.39M | } else { |
282 | 7.39M | DCHECK(literal_count_ > 0); |
283 | 7.39M | bool result = bit_reader_.GetValue(bit_width_, val); |
284 | 7.39M | DCHECK(result); |
285 | 7.39M | --literal_count_; |
286 | 7.39M | rewind_state_ = REWIND_LITERAL; |
287 | 7.39M | } |
288 | | |
289 | 7.64M | return true; |
290 | 7.64M | } _ZN5doris10RleDecoderIsE3GetEPs Line | Count | Source | 271 | 21 | bool RleDecoder<T>::Get(T* val) { | 272 | 21 | DCHECK(bit_reader_.is_initialized()); | 273 | 21 | if (!ReadHeader()) [[unlikely]] { | 274 | 0 | return false; | 275 | 0 | } | 276 | | | 277 | 21 | if (repeat_count_ > 0) [[likely]] { | 278 | 6 | *val = cast_set<T>(current_value_); | 279 | 6 | --repeat_count_; | 280 | 6 | rewind_state_ = REWIND_RUN; | 281 | 15 | } else { | 282 | 15 | DCHECK(literal_count_ > 0); | 283 | 15 | bool result = bit_reader_.GetValue(bit_width_, val); | 284 | 15 | DCHECK(result); | 285 | 15 | --literal_count_; | 286 | 15 | rewind_state_ = REWIND_LITERAL; | 287 | 15 | } | 288 | | | 289 | 21 | return true; | 290 | 21 | } |
_ZN5doris10RleDecoderIhE3GetEPh Line | Count | Source | 271 | 7.64M | bool RleDecoder<T>::Get(T* val) { | 272 | 7.64M | DCHECK(bit_reader_.is_initialized()); | 273 | 7.64M | if (!ReadHeader()) [[unlikely]] { | 274 | 0 | return false; | 275 | 0 | } | 276 | | | 277 | 7.64M | if (repeat_count_ > 0) [[likely]] { | 278 | 241k | *val = cast_set<T>(current_value_); | 279 | 241k | --repeat_count_; | 280 | 241k | rewind_state_ = REWIND_RUN; | 281 | 7.39M | } else { | 282 | 7.39M | DCHECK(literal_count_ > 0); | 283 | 7.39M | bool result = bit_reader_.GetValue(bit_width_, val); | 284 | 7.39M | DCHECK(result); | 285 | 7.39M | --literal_count_; | 286 | 7.39M | rewind_state_ = REWIND_LITERAL; | 287 | 7.39M | } | 288 | | | 289 | 7.64M | return true; | 290 | 7.64M | } |
|
291 | | |
292 | | template <typename T> |
293 | 5 | void RleDecoder<T>::RewindOne() { |
294 | 5 | DCHECK(bit_reader_.is_initialized()); |
295 | | |
296 | 5 | switch (rewind_state_) { |
297 | 0 | case CANT_REWIND: |
298 | 0 | throw Exception(Status::FatalError("Can't rewind more than once after each read!")); |
299 | 0 | break; |
300 | 2 | case REWIND_RUN: |
301 | 2 | ++repeat_count_; |
302 | 2 | break; |
303 | 3 | case REWIND_LITERAL: { |
304 | 3 | bit_reader_.Rewind(bit_width_); |
305 | 3 | ++literal_count_; |
306 | 3 | break; |
307 | 0 | } |
308 | 5 | } |
309 | | |
310 | 5 | rewind_state_ = CANT_REWIND; |
311 | 5 | } |
312 | | |
313 | | template <typename T> |
314 | 5.31M | size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) { |
315 | 5.31M | DCHECK(bit_reader_.is_initialized()); |
316 | 5.31M | DCHECK_GT(max_run, 0); |
317 | 5.31M | size_t ret = 0; |
318 | 5.31M | size_t rem = max_run; |
319 | 5.94M | while (ReadHeader()) { |
320 | 5.94M | if (repeat_count_ > 0) [[likely]] { |
321 | 3.54M | if (ret > 0 && *val != current_value_) [[unlikely]] { |
322 | 47.3k | return ret; |
323 | 47.3k | } |
324 | 3.49M | *val = cast_set<T>(current_value_); |
325 | 3.49M | if (repeat_count_ >= rem) { |
326 | | // The next run is longer than the amount of remaining data |
327 | | // that the caller wants to read. Only consume it partially. |
328 | 3.18M | repeat_count_ -= rem; |
329 | 3.18M | ret += rem; |
330 | 3.18M | return ret; |
331 | 3.18M | } |
332 | 316k | ret += repeat_count_; |
333 | 316k | rem -= repeat_count_; |
334 | 316k | repeat_count_ = 0; |
335 | 2.39M | } else { |
336 | 2.39M | DCHECK(literal_count_ > 0); |
337 | 2.39M | if (ret == 0) { |
338 | 2.10M | bool has_more = bit_reader_.GetValue(bit_width_, val); |
339 | 2.10M | DCHECK(has_more); |
340 | 2.10M | literal_count_--; |
341 | 2.10M | ret++; |
342 | 2.10M | rem--; |
343 | 2.10M | } |
344 | | |
345 | 5.87M | while (literal_count_ > 0) { |
346 | 5.57M | bool result = bit_reader_.GetValue(bit_width_, ¤t_value_); |
347 | 5.57M | DCHECK(result); |
348 | 5.57M | if (current_value_ != *val || rem == 0) { |
349 | 2.09M | bit_reader_.Rewind(bit_width_); |
350 | 2.09M | return ret; |
351 | 2.09M | } |
352 | 3.47M | ret++; |
353 | 3.47M | rem--; |
354 | 3.47M | literal_count_--; |
355 | 3.47M | } |
356 | 2.39M | } |
357 | 5.94M | } |
358 | 18.4E | return ret; |
359 | 5.31M | } _ZN5doris10RleDecoderIsE10GetNextRunEPsm Line | Count | Source | 314 | 1.08k | size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) { | 315 | 1.08k | DCHECK(bit_reader_.is_initialized()); | 316 | 1.08k | DCHECK_GT(max_run, 0); | 317 | 1.08k | size_t ret = 0; | 318 | 1.08k | size_t rem = max_run; | 319 | 1.10k | while (ReadHeader()) { | 320 | 1.10k | if (repeat_count_ > 0) [[likely]] { | 321 | 1.02k | if (ret > 0 && *val != current_value_) [[unlikely]] { | 322 | 22 | return ret; | 323 | 22 | } | 324 | 999 | *val = cast_set<T>(current_value_); | 325 | 999 | if (repeat_count_ >= rem) { | 326 | | // The next run is longer than the amount of remaining data | 327 | | // that the caller wants to read. Only consume it partially. | 328 | 977 | repeat_count_ -= rem; | 329 | 977 | ret += rem; | 330 | 977 | return ret; | 331 | 977 | } | 332 | 22 | ret += repeat_count_; | 333 | 22 | rem -= repeat_count_; | 334 | 22 | repeat_count_ = 0; | 335 | 87 | } else { | 336 | 87 | DCHECK(literal_count_ > 0); | 337 | 87 | if (ret == 0) { | 338 | 87 | bool has_more = bit_reader_.GetValue(bit_width_, val); | 339 | 87 | DCHECK(has_more); | 340 | 87 | literal_count_--; | 341 | 87 | ret++; | 342 | 87 | rem--; | 343 | 87 | } | 344 | | | 345 | 168 | while (literal_count_ > 0) { | 346 | 168 | bool result = bit_reader_.GetValue(bit_width_, ¤t_value_); | 347 | 168 | DCHECK(result); | 348 | 168 | if (current_value_ != *val || rem == 0) { | 349 | 87 | bit_reader_.Rewind(bit_width_); | 350 | 87 | return ret; | 351 | 87 | } | 352 | 81 | ret++; | 353 | 81 | rem--; | 354 | 81 | literal_count_--; | 355 | 81 | } | 356 | 87 | } | 357 | 1.10k | } | 358 | 0 | return ret; | 359 | 1.08k | } |
_ZN5doris10RleDecoderIbE10GetNextRunEPbm Line | Count | Source | 314 | 5.31M | size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) { | 315 | 5.31M | DCHECK(bit_reader_.is_initialized()); | 316 | 5.31M | DCHECK_GT(max_run, 0); | 317 | 5.31M | size_t ret = 0; | 318 | 5.31M | size_t rem = max_run; | 319 | 5.94M | while (ReadHeader()) { | 320 | 5.94M | if (repeat_count_ > 0) [[likely]] { | 321 | 3.54M | if (ret > 0 && *val != current_value_) [[unlikely]] { | 322 | 47.3k | return ret; | 323 | 47.3k | } | 324 | 3.49M | *val = cast_set<T>(current_value_); | 325 | 3.49M | if (repeat_count_ >= rem) { | 326 | | // The next run is longer than the amount of remaining data | 327 | | // that the caller wants to read. Only consume it partially. | 328 | 3.17M | repeat_count_ -= rem; | 329 | 3.17M | ret += rem; | 330 | 3.17M | return ret; | 331 | 3.17M | } | 332 | 316k | ret += repeat_count_; | 333 | 316k | rem -= repeat_count_; | 334 | 316k | repeat_count_ = 0; | 335 | 2.39M | } else { | 336 | 2.39M | DCHECK(literal_count_ > 0); | 337 | 2.39M | if (ret == 0) { | 338 | 2.10M | bool has_more = bit_reader_.GetValue(bit_width_, val); | 339 | 2.10M | DCHECK(has_more); | 340 | 2.10M | literal_count_--; | 341 | 2.10M | ret++; | 342 | 2.10M | rem--; | 343 | 2.10M | } | 344 | | | 345 | 5.87M | while (literal_count_ > 0) { | 346 | 5.57M | bool result = bit_reader_.GetValue(bit_width_, ¤t_value_); | 347 | 5.57M | DCHECK(result); | 348 | 5.57M | if (current_value_ != *val || rem == 0) { | 349 | 2.09M | bit_reader_.Rewind(bit_width_); | 350 | 2.09M | return ret; | 351 | 2.09M | } | 352 | 3.47M | ret++; | 353 | 3.47M | rem--; | 354 | 3.47M | literal_count_--; | 355 | 3.47M | } | 356 | 2.39M | } | 357 | 5.94M | } | 358 | 18.4E | return ret; | 359 | 5.31M | } |
|
360 | | |
361 | | template <typename T> |
362 | 68 | size_t RleDecoder<T>::get_values(T* values, size_t num_values) { |
363 | 68 | size_t read_num = 0; |
364 | 201 | while (read_num < num_values) { |
365 | 133 | size_t read_this_time = num_values - read_num; |
366 | | |
367 | 133 | if (LIKELY(repeat_count_ > 0)) { |
368 | 42 | read_this_time = std::min((size_t)repeat_count_, read_this_time); |
369 | 42 | std::fill(values, values + read_this_time, current_value_); |
370 | 42 | values += read_this_time; |
371 | 42 | repeat_count_ -= read_this_time; |
372 | 42 | read_num += read_this_time; |
373 | 91 | } else if (literal_count_ > 0) { |
374 | 25 | read_this_time = std::min((size_t)literal_count_, read_this_time); |
375 | 107 | for (int i = 0; i < read_this_time; ++i) { |
376 | 82 | bool result = bit_reader_.GetValue(bit_width_, values); |
377 | 82 | DCHECK(result); |
378 | 82 | values++; |
379 | 82 | } |
380 | 25 | literal_count_ -= read_this_time; |
381 | 25 | read_num += read_this_time; |
382 | 66 | } else { |
383 | 66 | if (!ReadHeader()) { |
384 | 0 | return read_num; |
385 | 0 | } |
386 | 66 | } |
387 | 133 | } |
388 | 68 | return read_num; |
389 | 68 | } _ZN5doris10RleDecoderIsE10get_valuesEPsm Line | Count | Source | 362 | 63 | size_t RleDecoder<T>::get_values(T* values, size_t num_values) { | 363 | 63 | size_t read_num = 0; | 364 | 187 | while (read_num < num_values) { | 365 | 124 | size_t read_this_time = num_values - read_num; | 366 | | | 367 | 124 | if (LIKELY(repeat_count_ > 0)) { | 368 | 42 | read_this_time = std::min((size_t)repeat_count_, read_this_time); | 369 | 42 | std::fill(values, values + read_this_time, current_value_); | 370 | 42 | values += read_this_time; | 371 | 42 | repeat_count_ -= read_this_time; | 372 | 42 | read_num += read_this_time; | 373 | 82 | } else if (literal_count_ > 0) { | 374 | 20 | read_this_time = std::min((size_t)literal_count_, read_this_time); | 375 | 67 | for (int i = 0; i < read_this_time; ++i) { | 376 | 47 | bool result = bit_reader_.GetValue(bit_width_, values); | 377 | 47 | DCHECK(result); | 378 | 47 | values++; | 379 | 47 | } | 380 | 20 | literal_count_ -= read_this_time; | 381 | 20 | read_num += read_this_time; | 382 | 62 | } else { | 383 | 62 | if (!ReadHeader()) { | 384 | 0 | return read_num; | 385 | 0 | } | 386 | 62 | } | 387 | 124 | } | 388 | 63 | return read_num; | 389 | 63 | } |
_ZN5doris10RleDecoderIhE10get_valuesEPhm Line | Count | Source | 362 | 5 | size_t RleDecoder<T>::get_values(T* values, size_t num_values) { | 363 | 5 | size_t read_num = 0; | 364 | 14 | while (read_num < num_values) { | 365 | 9 | size_t read_this_time = num_values - read_num; | 366 | | | 367 | 9 | if (LIKELY(repeat_count_ > 0)) { | 368 | 0 | read_this_time = std::min((size_t)repeat_count_, read_this_time); | 369 | 0 | std::fill(values, values + read_this_time, current_value_); | 370 | 0 | values += read_this_time; | 371 | 0 | repeat_count_ -= read_this_time; | 372 | 0 | read_num += read_this_time; | 373 | 9 | } else if (literal_count_ > 0) { | 374 | 5 | read_this_time = std::min((size_t)literal_count_, read_this_time); | 375 | 40 | for (int i = 0; i < read_this_time; ++i) { | 376 | 35 | bool result = bit_reader_.GetValue(bit_width_, values); | 377 | 35 | DCHECK(result); | 378 | 35 | values++; | 379 | 35 | } | 380 | 5 | literal_count_ -= read_this_time; | 381 | 5 | read_num += read_this_time; | 382 | 5 | } else { | 383 | 4 | if (!ReadHeader()) { | 384 | 0 | return read_num; | 385 | 0 | } | 386 | 4 | } | 387 | 9 | } | 388 | 5 | return read_num; | 389 | 5 | } |
|
390 | | |
391 | | template <typename T> |
392 | | size_t RleDecoder<T>::repeated_count() { |
393 | | if (repeat_count_ > 0) { |
394 | | return repeat_count_; |
395 | | } |
396 | | if (literal_count_ == 0) { |
397 | | ReadHeader(); |
398 | | } |
399 | | return repeat_count_; |
400 | | } |
401 | | |
402 | | template <typename T> |
403 | | T RleDecoder<T>::get_repeated_value(size_t count) { |
404 | | DCHECK_GE(repeat_count_, count); |
405 | | repeat_count_ -= count; |
406 | | return current_value_; |
407 | | } |
408 | | |
409 | | template <typename T> |
410 | 1.32M | size_t RleDecoder<T>::Skip(size_t to_skip) { |
411 | 1.32M | DCHECK(bit_reader_.is_initialized()); |
412 | | |
413 | 1.32M | size_t set_count = 0; |
414 | 2.18M | while (to_skip > 0) { |
415 | 861k | bool result = ReadHeader(); |
416 | 861k | DCHECK(result); |
417 | | |
418 | 861k | if (repeat_count_ > 0) [[likely]] { |
419 | 327k | size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip; |
420 | 327k | repeat_count_ -= nskip; |
421 | 327k | to_skip -= nskip; |
422 | 327k | if (current_value_ != 0) { |
423 | 261k | set_count += nskip; |
424 | 261k | } |
425 | 534k | } else { |
426 | 534k | DCHECK(literal_count_ > 0); |
427 | 534k | size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip; |
428 | 534k | literal_count_ -= nskip; |
429 | 534k | to_skip -= nskip; |
430 | 17.8M | for (; nskip > 0; nskip--) { |
431 | 17.2M | T value = 0; |
432 | 17.2M | bool result1 = bit_reader_.GetValue(bit_width_, &value); |
433 | 17.2M | DCHECK(result1); |
434 | 17.2M | if (value != 0) { |
435 | 9.34M | set_count++; |
436 | 9.34M | } |
437 | 17.2M | } |
438 | 534k | } |
439 | 861k | } |
440 | 1.32M | return set_count; |
441 | 1.32M | } _ZN5doris10RleDecoderIhE4SkipEm Line | Count | Source | 410 | 1.20M | size_t RleDecoder<T>::Skip(size_t to_skip) { | 411 | 1.20M | DCHECK(bit_reader_.is_initialized()); | 412 | | | 413 | 1.20M | size_t set_count = 0; | 414 | 1.49M | while (to_skip > 0) { | 415 | 292k | bool result = ReadHeader(); | 416 | 292k | DCHECK(result); | 417 | | | 418 | 292k | if (repeat_count_ > 0) [[likely]] { | 419 | 19.6k | size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip; | 420 | 19.6k | repeat_count_ -= nskip; | 421 | 19.6k | to_skip -= nskip; | 422 | 19.6k | if (current_value_ != 0) { | 423 | 9.21k | set_count += nskip; | 424 | 9.21k | } | 425 | 272k | } else { | 426 | 272k | DCHECK(literal_count_ > 0); | 427 | 272k | size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip; | 428 | 272k | literal_count_ -= nskip; | 429 | 272k | to_skip -= nskip; | 430 | 13.4M | for (; nskip > 0; nskip--) { | 431 | 13.1M | T value = 0; | 432 | 13.1M | bool result1 = bit_reader_.GetValue(bit_width_, &value); | 433 | 13.1M | DCHECK(result1); | 434 | 13.1M | if (value != 0) { | 435 | 6.66M | set_count++; | 436 | 6.66M | } | 437 | 13.1M | } | 438 | 272k | } | 439 | 292k | } | 440 | 1.20M | return set_count; | 441 | 1.20M | } |
_ZN5doris10RleDecoderIbE4SkipEm Line | Count | Source | 410 | 119k | size_t RleDecoder<T>::Skip(size_t to_skip) { | 411 | 119k | DCHECK(bit_reader_.is_initialized()); | 412 | | | 413 | 119k | size_t set_count = 0; | 414 | 689k | while (to_skip > 0) { | 415 | 569k | bool result = ReadHeader(); | 416 | 569k | DCHECK(result); | 417 | | | 418 | 569k | if (repeat_count_ > 0) [[likely]] { | 419 | 307k | size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip; | 420 | 307k | repeat_count_ -= nskip; | 421 | 307k | to_skip -= nskip; | 422 | 307k | if (current_value_ != 0) { | 423 | 252k | set_count += nskip; | 424 | 252k | } | 425 | 307k | } else { | 426 | 261k | DCHECK(literal_count_ > 0); | 427 | 261k | size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip; | 428 | 261k | literal_count_ -= nskip; | 429 | 261k | to_skip -= nskip; | 430 | 4.37M | for (; nskip > 0; nskip--) { | 431 | 4.11M | T value = 0; | 432 | 4.11M | bool result1 = bit_reader_.GetValue(bit_width_, &value); | 433 | 4.11M | DCHECK(result1); | 434 | 4.11M | if (value != 0) { | 435 | 2.68M | set_count++; | 436 | 2.68M | } | 437 | 4.11M | } | 438 | 261k | } | 439 | 569k | } | 440 | 119k | return set_count; | 441 | 119k | } |
|
442 | | |
443 | | // This function buffers input values 8 at a time. After seeing all 8 values, |
444 | | // it decides whether they should be encoded as a literal or repeated run. |
445 | | template <typename T> |
446 | 12.1M | void RleEncoder<T>::Put(T value, size_t run_length) { |
447 | 12.1M | DCHECK(bit_width_ == 64 || value < (1LL << bit_width_)); |
448 | | |
449 | | // Fast path: if this is a continuation of the current repeated run and |
450 | | // we've already buffered enough values, just increment repeat_count_ |
451 | 12.1M | if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] { |
452 | 1.13M | repeat_count_ += static_cast<int64_t>(run_length); |
453 | 1.13M | return; |
454 | 1.13M | } |
455 | | |
456 | | // Handle run_length > 1 more efficiently |
457 | 25.9M | while (run_length > 0) { |
458 | 16.4M | if (current_value_ == value) [[likely]] { |
459 | | // Need to buffer values until we reach 8 |
460 | 7.70M | size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_)); |
461 | 36.6M | for (size_t i = 0; i < to_buffer; ++i) { |
462 | 28.9M | buffered_values_[num_buffered_values_++] = value; |
463 | 28.9M | ++repeat_count_; |
464 | 28.9M | } |
465 | 7.70M | run_length -= to_buffer; |
466 | 7.70M | if (num_buffered_values_ == 8) { |
467 | 3.86M | DCHECK_EQ(literal_count_ % 8, 0); |
468 | 3.86M | FlushBufferedValues(false); |
469 | | // After flushing, if we still have a repeated run and more values, |
470 | | // we can add them directly to repeat_count_ |
471 | 3.86M | if (repeat_count_ >= 8 && run_length > 0) { |
472 | 1.49M | repeat_count_ += static_cast<int64_t>(run_length); |
473 | 1.49M | return; |
474 | 1.49M | } |
475 | 3.86M | } |
476 | 8.75M | } else { |
477 | | // Value changed |
478 | 8.75M | if (repeat_count_ >= 8) { |
479 | | // We had a run that was long enough but it has ended. Flush the |
480 | | // current repeated run. |
481 | 1.41M | DCHECK_EQ(literal_count_, 0); |
482 | 1.41M | FlushRepeatedRun(); |
483 | 1.41M | } |
484 | 8.75M | repeat_count_ = 1; |
485 | 8.75M | current_value_ = value; |
486 | | |
487 | 8.75M | buffered_values_[num_buffered_values_++] = value; |
488 | 8.75M | --run_length; |
489 | 8.75M | if (num_buffered_values_ == 8) { |
490 | 760k | DCHECK_EQ(literal_count_ % 8, 0); |
491 | 760k | FlushBufferedValues(false); |
492 | 760k | } |
493 | 8.75M | } |
494 | 16.4M | } |
495 | 11.0M | } _ZN5doris10RleEncoderIbE3PutEbm Line | Count | Source | 446 | 8.00M | void RleEncoder<T>::Put(T value, size_t run_length) { | 447 | 8.00M | DCHECK(bit_width_ == 64 || value < (1LL << bit_width_)); | 448 | | | 449 | | // Fast path: if this is a continuation of the current repeated run and | 450 | | // we've already buffered enough values, just increment repeat_count_ | 451 | 8.00M | if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] { | 452 | 646k | repeat_count_ += static_cast<int64_t>(run_length); | 453 | 646k | return; | 454 | 646k | } | 455 | | | 456 | | // Handle run_length > 1 more efficiently | 457 | 18.6M | while (run_length > 0) { | 458 | 12.7M | if (current_value_ == value) [[likely]] { | 459 | | // Need to buffer values until we reach 8 | 460 | 5.86M | size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_)); | 461 | 32.9M | for (size_t i = 0; i < to_buffer; ++i) { | 462 | 27.0M | buffered_values_[num_buffered_values_++] = value; | 463 | 27.0M | ++repeat_count_; | 464 | 27.0M | } | 465 | 5.86M | run_length -= to_buffer; | 466 | 5.86M | if (num_buffered_values_ == 8) { | 467 | 3.62M | DCHECK_EQ(literal_count_ % 8, 0); | 468 | 3.62M | FlushBufferedValues(false); | 469 | | // After flushing, if we still have a repeated run and more values, | 470 | | // we can add them directly to repeat_count_ | 471 | 3.62M | if (repeat_count_ >= 8 && run_length > 0) { | 472 | 1.49M | repeat_count_ += static_cast<int64_t>(run_length); | 473 | 1.49M | return; | 474 | 1.49M | } | 475 | 3.62M | } | 476 | 6.93M | } else { | 477 | | // Value changed | 478 | 6.93M | if (repeat_count_ >= 8) { | 479 | | // We had a run that was long enough but it has ended. Flush the | 480 | | // current repeated run. | 481 | 1.41M | DCHECK_EQ(literal_count_, 0); | 482 | 1.41M | FlushRepeatedRun(); | 483 | 1.41M | } | 484 | 6.93M | repeat_count_ = 1; | 485 | 6.93M | current_value_ = value; | 486 | | | 487 | 6.93M | buffered_values_[num_buffered_values_++] = value; | 488 | 6.93M | --run_length; | 489 | 6.93M | if (num_buffered_values_ == 8) { | 490 | | DCHECK_EQ(literal_count_ % 8, 0); | 491 | 532k | FlushBufferedValues(false); | 492 | 532k | } | 493 | 6.93M | } | 494 | 12.7M | } | 495 | 7.35M | } |
_ZN5doris10RleEncoderIhE3PutEhm Line | Count | Source | 446 | 4.15M | void RleEncoder<T>::Put(T value, size_t run_length) { | 447 | 4.15M | DCHECK(bit_width_ == 64 || value < (1LL << bit_width_)); | 448 | | | 449 | | // Fast path: if this is a continuation of the current repeated run and | 450 | | // we've already buffered enough values, just increment repeat_count_ | 451 | 4.15M | if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] { | 452 | 483k | repeat_count_ += static_cast<int64_t>(run_length); | 453 | 483k | return; | 454 | 483k | } | 455 | | | 456 | | // Handle run_length > 1 more efficiently | 457 | 7.33M | while (run_length > 0) { | 458 | 3.66M | if (current_value_ == value) [[likely]] { | 459 | | // Need to buffer values until we reach 8 | 460 | 1.84M | size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_)); | 461 | 3.69M | for (size_t i = 0; i < to_buffer; ++i) { | 462 | 1.84M | buffered_values_[num_buffered_values_++] = value; | 463 | 1.84M | ++repeat_count_; | 464 | 1.84M | } | 465 | 1.84M | run_length -= to_buffer; | 466 | 1.84M | if (num_buffered_values_ == 8) { | 467 | 233k | DCHECK_EQ(literal_count_ % 8, 0); | 468 | 233k | FlushBufferedValues(false); | 469 | | // After flushing, if we still have a repeated run and more values, | 470 | | // we can add them directly to repeat_count_ | 471 | 233k | if (repeat_count_ >= 8 && run_length > 0) { | 472 | 3 | repeat_count_ += static_cast<int64_t>(run_length); | 473 | 3 | return; | 474 | 3 | } | 475 | 233k | } | 476 | 1.84M | } else { | 477 | | // Value changed | 478 | 1.81M | if (repeat_count_ >= 8) { | 479 | | // We had a run that was long enough but it has ended. Flush the | 480 | | // current repeated run. | 481 | 6.38k | DCHECK_EQ(literal_count_, 0); | 482 | 6.38k | FlushRepeatedRun(); | 483 | 6.38k | } | 484 | 1.81M | repeat_count_ = 1; | 485 | 1.81M | current_value_ = value; | 486 | | | 487 | 1.81M | buffered_values_[num_buffered_values_++] = value; | 488 | 1.81M | --run_length; | 489 | 1.81M | if (num_buffered_values_ == 8) { | 490 | | DCHECK_EQ(literal_count_ % 8, 0); | 491 | 227k | FlushBufferedValues(false); | 492 | 227k | } | 493 | 1.81M | } | 494 | 3.66M | } | 495 | 3.66M | } |
|
496 | | |
497 | | template <typename T> |
498 | 4.45M | void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) { |
499 | 4.45M | if (literal_indicator_byte_idx_ < 0) { |
500 | | // The literal indicator byte has not been reserved yet, get one now. |
501 | 1.44M | literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1)); |
502 | 1.44M | DCHECK_GE(literal_indicator_byte_idx_, 0); |
503 | 1.44M | } |
504 | | |
505 | | // Write all the buffered values as bit packed literals |
506 | 28.5M | for (int i = 0; i < num_buffered_values_; ++i) { |
507 | 24.1M | bit_writer_.PutValue(buffered_values_[i], bit_width_); |
508 | 24.1M | } |
509 | 4.45M | num_buffered_values_ = 0; |
510 | | |
511 | 4.45M | if (update_indicator_byte) { |
512 | | // At this point we need to write the indicator byte for the literal run. |
513 | | // We only reserve one byte, to allow for streaming writes of literal values. |
514 | | // The logic makes sure we flush literal runs often enough to not overrun |
515 | | // the 1 byte. |
516 | 1.44M | int num_groups = BitUtil::Ceil(literal_count_, 8); |
517 | 1.44M | int32_t indicator_value = (num_groups << 1) | 1; |
518 | 1.44M | DCHECK_EQ(indicator_value & 0xFFFFFF00, 0); |
519 | 1.44M | bit_writer_.buffer()->data()[literal_indicator_byte_idx_] = |
520 | 1.44M | cast_set<uint8_t>(indicator_value); |
521 | 1.44M | literal_indicator_byte_idx_ = -1; |
522 | 1.44M | literal_count_ = 0; |
523 | 1.44M | } |
524 | 4.45M | } _ZN5doris10RleEncoderIbE15FlushLiteralRunEb Line | Count | Source | 498 | 3.98M | void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) { | 499 | 3.98M | if (literal_indicator_byte_idx_ < 0) { | 500 | | // The literal indicator byte has not been reserved yet, get one now. | 501 | 1.42M | literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1)); | 502 | 1.42M | DCHECK_GE(literal_indicator_byte_idx_, 0); | 503 | 1.42M | } | 504 | | | 505 | | // Write all the buffered values as bit packed literals | 506 | 24.5M | for (int i = 0; i < num_buffered_values_; ++i) { | 507 | 20.5M | bit_writer_.PutValue(buffered_values_[i], bit_width_); | 508 | 20.5M | } | 509 | 3.98M | num_buffered_values_ = 0; | 510 | | | 511 | 3.98M | if (update_indicator_byte) { | 512 | | // At this point we need to write the indicator byte for the literal run. | 513 | | // We only reserve one byte, to allow for streaming writes of literal values. | 514 | | // The logic makes sure we flush literal runs often enough to not overrun | 515 | | // the 1 byte. | 516 | 1.42M | int num_groups = BitUtil::Ceil(literal_count_, 8); | 517 | 1.42M | int32_t indicator_value = (num_groups << 1) | 1; | 518 | | DCHECK_EQ(indicator_value & 0xFFFFFF00, 0); | 519 | 1.42M | bit_writer_.buffer()->data()[literal_indicator_byte_idx_] = | 520 | 1.42M | cast_set<uint8_t>(indicator_value); | 521 | 1.42M | literal_indicator_byte_idx_ = -1; | 522 | 1.42M | literal_count_ = 0; | 523 | 1.42M | } | 524 | 3.98M | } |
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb Line | Count | Source | 498 | 460k | void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) { | 499 | 460k | if (literal_indicator_byte_idx_ < 0) { | 500 | | // The literal indicator byte has not been reserved yet, get one now. | 501 | 13.7k | literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1)); | 502 | 13.7k | DCHECK_GE(literal_indicator_byte_idx_, 0); | 503 | 13.7k | } | 504 | | | 505 | | // Write all the buffered values as bit packed literals | 506 | 4.04M | for (int i = 0; i < num_buffered_values_; ++i) { | 507 | 3.58M | bit_writer_.PutValue(buffered_values_[i], bit_width_); | 508 | 3.58M | } | 509 | 460k | num_buffered_values_ = 0; | 510 | | | 511 | 460k | if (update_indicator_byte) { | 512 | | // At this point we need to write the indicator byte for the literal run. | 513 | | // We only reserve one byte, to allow for streaming writes of literal values. | 514 | | // The logic makes sure we flush literal runs often enough to not overrun | 515 | | // the 1 byte. | 516 | 13.7k | int num_groups = BitUtil::Ceil(literal_count_, 8); | 517 | 13.7k | int32_t indicator_value = (num_groups << 1) | 1; | 518 | | DCHECK_EQ(indicator_value & 0xFFFFFF00, 0); | 519 | 13.7k | bit_writer_.buffer()->data()[literal_indicator_byte_idx_] = | 520 | 13.7k | cast_set<uint8_t>(indicator_value); | 521 | 13.7k | literal_indicator_byte_idx_ = -1; | 522 | 13.7k | literal_count_ = 0; | 523 | 13.7k | } | 524 | 460k | } |
|
525 | | |
526 | | template <typename T> |
527 | 1.57M | void RleEncoder<T>::FlushRepeatedRun() { |
528 | 1.57M | DCHECK_GT(repeat_count_, 0); |
529 | | // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a |
530 | | // single indicator, so emit it as several back-to-back repeated runs of the |
531 | | // same value. Decoders stitch those back together transparently |
532 | | // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so |
533 | | // splitting is invisible to readers. |
534 | 3.14M | while (repeat_count_ > 0) { |
535 | 1.57M | const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN); |
536 | | // The lsb of 0 indicates this is a repeated run |
537 | 1.57M | bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1); |
538 | 1.57M | bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8)); |
539 | 1.57M | repeat_count_ -= run_length; |
540 | 1.57M | } |
541 | 1.57M | num_buffered_values_ = 0; |
542 | | // The loop above drains repeat_count_; assign it anyway so the postcondition |
543 | | // holds even if it somehow entered non-positive. |
544 | 1.57M | repeat_count_ = 0; |
545 | 1.57M | } _ZN5doris10RleEncoderIbE16FlushRepeatedRunEv Line | Count | Source | 527 | 1.55M | void RleEncoder<T>::FlushRepeatedRun() { | 528 | 1.55M | DCHECK_GT(repeat_count_, 0); | 529 | | // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a | 530 | | // single indicator, so emit it as several back-to-back repeated runs of the | 531 | | // same value. Decoders stitch those back together transparently | 532 | | // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so | 533 | | // splitting is invisible to readers. | 534 | 3.11M | while (repeat_count_ > 0) { | 535 | 1.55M | const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN); | 536 | | // The lsb of 0 indicates this is a repeated run | 537 | 1.55M | bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1); | 538 | 1.55M | bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8)); | 539 | 1.55M | repeat_count_ -= run_length; | 540 | 1.55M | } | 541 | 1.55M | num_buffered_values_ = 0; | 542 | | // The loop above drains repeat_count_; assign it anyway so the postcondition | 543 | | // holds even if it somehow entered non-positive. | 544 | 1.55M | repeat_count_ = 0; | 545 | 1.55M | } |
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv Line | Count | Source | 527 | 14.7k | void RleEncoder<T>::FlushRepeatedRun() { | 528 | 14.7k | DCHECK_GT(repeat_count_, 0); | 529 | | // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a | 530 | | // single indicator, so emit it as several back-to-back repeated runs of the | 531 | | // same value. Decoders stitch those back together transparently | 532 | | // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so | 533 | | // splitting is invisible to readers. | 534 | 29.5k | while (repeat_count_ > 0) { | 535 | 14.7k | const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN); | 536 | | // The lsb of 0 indicates this is a repeated run | 537 | 14.7k | bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1); | 538 | 14.7k | bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8)); | 539 | 14.7k | repeat_count_ -= run_length; | 540 | 14.7k | } | 541 | 14.7k | num_buffered_values_ = 0; | 542 | | // The loop above drains repeat_count_; assign it anyway so the postcondition | 543 | | // holds even if it somehow entered non-positive. | 544 | 14.7k | repeat_count_ = 0; | 545 | 14.7k | } |
|
546 | | |
547 | | // Flush the values that have been buffered. At this point we decide whether |
548 | | // we need to switch between the run types or continue the current one. |
549 | | template <typename T> |
550 | 4.60M | void RleEncoder<T>::FlushBufferedValues(bool done) { |
551 | 4.60M | if (repeat_count_ >= 8) { |
552 | | // Clear the buffered values. They are part of the repeated run now and we |
553 | | // don't want to flush them out as literals. |
554 | 1.59M | num_buffered_values_ = 0; |
555 | 1.59M | if (literal_count_ != 0) { |
556 | | // There was a current literal run. All the values in it have been flushed |
557 | | // but we still need to update the indicator byte. |
558 | 1.38M | DCHECK_EQ(literal_count_ % 8, 0); |
559 | 1.38M | DCHECK_EQ(repeat_count_, 8); |
560 | 1.38M | FlushLiteralRun(true); |
561 | 1.38M | } |
562 | 1.59M | DCHECK_EQ(literal_count_, 0); |
563 | 1.59M | return; |
564 | 1.59M | } |
565 | | |
566 | 3.00M | literal_count_ += num_buffered_values_; |
567 | 3.00M | int num_groups = BitUtil::Ceil(literal_count_, 8); |
568 | 3.00M | if (num_groups + 1 >= (1 << 6)) { |
569 | | // We need to start a new literal run because the indicator byte we've reserved |
570 | | // cannot store more values. |
571 | 6.71k | DCHECK_GE(literal_indicator_byte_idx_, 0); |
572 | 6.71k | FlushLiteralRun(true); |
573 | 3.00M | } else { |
574 | 3.00M | FlushLiteralRun(done); |
575 | 3.00M | } |
576 | 3.00M | repeat_count_ = 0; |
577 | 3.00M | } _ZN5doris10RleEncoderIbE19FlushBufferedValuesEb Line | Count | Source | 550 | 4.14M | void RleEncoder<T>::FlushBufferedValues(bool done) { | 551 | 4.14M | if (repeat_count_ >= 8) { | 552 | | // Clear the buffered values. They are part of the repeated run now and we | 553 | | // don't want to flush them out as literals. | 554 | 1.58M | num_buffered_values_ = 0; | 555 | 1.58M | if (literal_count_ != 0) { | 556 | | // There was a current literal run. All the values in it have been flushed | 557 | | // but we still need to update the indicator byte. | 558 | 1.37M | DCHECK_EQ(literal_count_ % 8, 0); | 559 | 1.37M | DCHECK_EQ(repeat_count_, 8); | 560 | 1.37M | FlushLiteralRun(true); | 561 | 1.37M | } | 562 | 1.58M | DCHECK_EQ(literal_count_, 0); | 563 | 1.58M | return; | 564 | 1.58M | } | 565 | | | 566 | 2.55M | literal_count_ += num_buffered_values_; | 567 | 2.55M | int num_groups = BitUtil::Ceil(literal_count_, 8); | 568 | 2.55M | if (num_groups + 1 >= (1 << 6)) { | 569 | | // We need to start a new literal run because the indicator byte we've reserved | 570 | | // cannot store more values. | 571 | 1.38k | DCHECK_GE(literal_indicator_byte_idx_, 0); | 572 | 1.38k | FlushLiteralRun(true); | 573 | 2.55M | } else { | 574 | 2.55M | FlushLiteralRun(done); | 575 | 2.55M | } | 576 | 2.55M | repeat_count_ = 0; | 577 | 2.55M | } |
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb Line | Count | Source | 550 | 460k | void RleEncoder<T>::FlushBufferedValues(bool done) { | 551 | 460k | if (repeat_count_ >= 8) { | 552 | | // Clear the buffered values. They are part of the repeated run now and we | 553 | | // don't want to flush them out as literals. | 554 | 7.71k | num_buffered_values_ = 0; | 555 | 7.71k | if (literal_count_ != 0) { | 556 | | // There was a current literal run. All the values in it have been flushed | 557 | | // but we still need to update the indicator byte. | 558 | 5.00k | DCHECK_EQ(literal_count_ % 8, 0); | 559 | 5.00k | DCHECK_EQ(repeat_count_, 8); | 560 | 5.00k | FlushLiteralRun(true); | 561 | 5.00k | } | 562 | 7.71k | DCHECK_EQ(literal_count_, 0); | 563 | 7.71k | return; | 564 | 7.71k | } | 565 | | | 566 | 452k | literal_count_ += num_buffered_values_; | 567 | 452k | int num_groups = BitUtil::Ceil(literal_count_, 8); | 568 | 452k | if (num_groups + 1 >= (1 << 6)) { | 569 | | // We need to start a new literal run because the indicator byte we've reserved | 570 | | // cannot store more values. | 571 | 5.33k | DCHECK_GE(literal_indicator_byte_idx_, 0); | 572 | 5.33k | FlushLiteralRun(true); | 573 | 447k | } else { | 574 | 447k | FlushLiteralRun(done); | 575 | 447k | } | 576 | 452k | repeat_count_ = 0; | 577 | 452k | } |
|
578 | | |
579 | | template <typename T> |
580 | 25.8k | void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) { |
581 | 129k | for (int i = 0; i < num_bytes; ++i) { |
582 | 103k | bit_writer_.PutValue(val, 8); |
583 | 103k | } |
584 | 25.8k | } |
585 | | |
586 | | template <typename T> |
587 | 204k | int RleEncoder<T>::Flush() { |
588 | 204k | if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) { |
589 | 204k | bool all_repeat = literal_count_ == 0 && |
590 | 204k | (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0); |
591 | | // There is something pending, figure out if it's a repeated or literal run |
592 | 204k | if (repeat_count_ > 0 && all_repeat) { |
593 | 153k | FlushRepeatedRun(); |
594 | 153k | } else { |
595 | 50.6k | literal_count_ += num_buffered_values_; |
596 | 50.6k | FlushLiteralRun(true); |
597 | 50.6k | repeat_count_ = 0; |
598 | 50.6k | } |
599 | 204k | } |
600 | 204k | bit_writer_.Flush(); |
601 | 204k | DCHECK_EQ(num_buffered_values_, 0); |
602 | 204k | DCHECK_EQ(literal_count_, 0); |
603 | 204k | DCHECK_EQ(repeat_count_, 0); |
604 | 204k | return bit_writer_.bytes_written(); |
605 | 204k | } _ZN5doris10RleEncoderIbE5FlushEv Line | Count | Source | 587 | 192k | int RleEncoder<T>::Flush() { | 588 | 192k | if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) { | 589 | 192k | bool all_repeat = literal_count_ == 0 && | 590 | 192k | (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0); | 591 | | // There is something pending, figure out if it's a repeated or literal run | 592 | 192k | if (repeat_count_ > 0 && all_repeat) { | 593 | 145k | FlushRepeatedRun(); | 594 | 145k | } else { | 595 | 47.2k | literal_count_ += num_buffered_values_; | 596 | 47.2k | FlushLiteralRun(true); | 597 | 47.2k | repeat_count_ = 0; | 598 | 47.2k | } | 599 | 192k | } | 600 | 192k | bit_writer_.Flush(); | 601 | 192k | DCHECK_EQ(num_buffered_values_, 0); | 602 | 192k | DCHECK_EQ(literal_count_, 0); | 603 | | DCHECK_EQ(repeat_count_, 0); | 604 | 192k | return bit_writer_.bytes_written(); | 605 | 192k | } |
_ZN5doris10RleEncoderIhE5FlushEv Line | Count | Source | 587 | 12.3k | int RleEncoder<T>::Flush() { | 588 | 12.3k | if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) { | 589 | 11.8k | bool all_repeat = literal_count_ == 0 && | 590 | 11.8k | (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0); | 591 | | // There is something pending, figure out if it's a repeated or literal run | 592 | 11.8k | if (repeat_count_ > 0 && all_repeat) { | 593 | 8.37k | FlushRepeatedRun(); | 594 | 8.37k | } else { | 595 | 3.43k | literal_count_ += num_buffered_values_; | 596 | 3.43k | FlushLiteralRun(true); | 597 | 3.43k | repeat_count_ = 0; | 598 | 3.43k | } | 599 | 11.8k | } | 600 | 12.3k | bit_writer_.Flush(); | 601 | 12.3k | DCHECK_EQ(num_buffered_values_, 0); | 602 | 12.3k | DCHECK_EQ(literal_count_, 0); | 603 | | DCHECK_EQ(repeat_count_, 0); | 604 | 12.3k | return bit_writer_.bytes_written(); | 605 | 12.3k | } |
|
606 | | |
607 | | template <typename T> |
608 | 1.14M | void RleEncoder<T>::Clear() { |
609 | 1.14M | current_value_ = 0; |
610 | 1.14M | repeat_count_ = 0; |
611 | 1.14M | num_buffered_values_ = 0; |
612 | 1.14M | literal_count_ = 0; |
613 | 1.14M | literal_indicator_byte_idx_ = -1; |
614 | 1.14M | bit_writer_.Clear(); |
615 | 1.14M | } _ZN5doris10RleEncoderIbE5ClearEv Line | Count | Source | 608 | 1.10M | void RleEncoder<T>::Clear() { | 609 | 1.10M | current_value_ = 0; | 610 | 1.10M | repeat_count_ = 0; | 611 | 1.10M | num_buffered_values_ = 0; | 612 | 1.10M | literal_count_ = 0; | 613 | 1.10M | literal_indicator_byte_idx_ = -1; | 614 | 1.10M | bit_writer_.Clear(); | 615 | 1.10M | } |
_ZN5doris10RleEncoderIhE5ClearEv Line | Count | Source | 608 | 39.2k | void RleEncoder<T>::Clear() { | 609 | 39.2k | current_value_ = 0; | 610 | 39.2k | repeat_count_ = 0; | 611 | 39.2k | num_buffered_values_ = 0; | 612 | 39.2k | literal_count_ = 0; | 613 | 39.2k | literal_indicator_byte_idx_ = -1; | 614 | 39.2k | bit_writer_.Clear(); | 615 | 39.2k | } |
|
616 | | |
617 | | // Copy from https://github.com/apache/impala/blob/master/be/src/util/rle-encoding.h |
618 | | // Utility classes to do run length encoding (RLE) for fixed bit width values. If runs |
619 | | // are sufficiently long, RLE is used, otherwise, the values are just bit-packed |
620 | | // (literal encoding). |
621 | | // |
622 | | // For both types of runs, there is a byte-aligned indicator which encodes the length |
623 | | // of the run and the type of the run. |
624 | | // |
625 | | // This encoding has the benefit that when there aren't any long enough runs, values |
626 | | // are always decoded at fixed (can be precomputed) bit offsets OR both the value and |
627 | | // the run length are byte aligned. This allows for very efficient decoding |
628 | | // implementations. |
629 | | // The encoding is: |
630 | | // encoded-block := run* |
631 | | // run := literal-run | repeated-run |
632 | | // literal-run := literal-indicator < literal bytes > |
633 | | // repeated-run := repeated-indicator < repeated value. padded to byte boundary > |
634 | | // literal-indicator := varint_encode( number_of_groups << 1 | 1) |
635 | | // repeated-indicator := varint_encode( number_of_repetitions << 1 ) |
636 | | // |
637 | | // Each run is preceded by a varint. The varint's least significant bit is |
638 | | // used to indicate whether the run is a literal run or a repeated run. The rest |
639 | | // of the varint is used to determine the length of the run (eg how many times the |
640 | | // value repeats). |
641 | | // |
642 | | // In the case of literal runs, the run length is always a multiple of 8 (i.e. encode |
643 | | // in groups of 8), so that no matter the bit-width of the value, the sequence will end |
644 | | // on a byte boundary without padding. |
645 | | // Given that we know it is a multiple of 8, we store the number of 8-groups rather than |
646 | | // the actual number of encoded ints. (This means that the total number of encoded values |
647 | | // can not be determined from the encoded data, since the number of values in the last |
648 | | // group may not be a multiple of 8). For the last group of literal runs, we pad |
649 | | // the group to 8 with zeros. This allows for 8 at a time decoding on the read side |
650 | | // without the need for additional checks. |
651 | | // |
652 | | // There is a break-even point when it is more storage efficient to do run length |
653 | | // encoding. For 1 bit-width values, that point is 8 values. They require 2 bytes |
654 | | // for both the repeated encoding or the literal encoding. This value can always |
655 | | // be computed based on the bit-width. |
656 | | // TODO: For 1 bit-width values it can be optimal to use 16 or 24 values, but more |
657 | | // investigation is needed to do this efficiently, see the reverted IMPALA-6658. |
658 | | // TODO: think about how to use this for strings. The bit packing isn't quite the same. |
659 | | // |
660 | | // Examples with bit-width 1 (eg encoding booleans): |
661 | | // ---------------------------------------- |
662 | | // 100 1s followed by 100 0s: |
663 | | // <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte> |
664 | | // - (total 4 bytes) |
665 | | // |
666 | | // alternating 1s and 0s (200 total): |
667 | | // 200 ints = 25 groups of 8 |
668 | | // <varint((25 << 1) | 1)> <25 bytes of values, bitpacked> |
669 | | // (total 26 bytes, 1 byte overhead) |
670 | | |
671 | | // RLE decoder with a batch-oriented interface that enables fast decoding. |
672 | | // Users of this class must first initialize the class to point to a buffer of |
673 | | // RLE-encoded data, passed into the constructor or Reset(). The provided |
674 | | // bit_width must be at most min(sizeof(T) * 8, BatchedBitReader::MAX_BITWIDTH). |
675 | | // Then they can decode data by checking NextNumRepeats()/NextNumLiterals() to |
676 | | // see if the next run is a repeated or literal run, then calling |
677 | | // GetRepeatedValue() or GetLiteralValues() respectively to read the values. |
678 | | // |
679 | | // End-of-input is signalled by NextNumRepeats() == NextNumLiterals() == 0. |
680 | | // Other decoding errors are signalled by functions returning false. If an |
681 | | // error is encountered then it is not valid to read any more data until |
682 | | // Reset() is called. |
683 | | |
684 | | //bit-packed-run-len and rle-run-len must be in the range [1, 2^31 - 1]. |
685 | | // This means that a Parquet implementation can always store the run length in a signed 32-bit integer. |
686 | | template <typename T> |
687 | | class RleBatchDecoder { |
688 | | public: |
689 | 13.1k | RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) { |
690 | 13.1k | Reset(buffer, buffer_len, bit_width); |
691 | 13.1k | } _ZN5doris15RleBatchDecoderItEC2EPhii Line | Count | Source | 689 | 7.57k | RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) { | 690 | 7.57k | Reset(buffer, buffer_len, bit_width); | 691 | 7.57k | } |
_ZN5doris15RleBatchDecoderIjEC2EPhii Line | Count | Source | 689 | 5.57k | RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) { | 690 | 5.57k | Reset(buffer, buffer_len, bit_width); | 691 | 5.57k | } |
_ZN5doris15RleBatchDecoderIhEC2EPhii Line | Count | Source | 689 | 6 | RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) { | 690 | 6 | Reset(buffer, buffer_len, bit_width); | 691 | 6 | } |
|
692 | | |
693 | 12.3k | RleBatchDecoder() = default; _ZN5doris15RleBatchDecoderItEC2Ev Line | Count | Source | 693 | 12.3k | RleBatchDecoder() = default; |
_ZN5doris15RleBatchDecoderIhEC2Ev Line | Count | Source | 693 | 8 | RleBatchDecoder() = default; |
|
694 | | |
695 | | // Reset the decoder to read from a new buffer. |
696 | | void Reset(uint8_t* buffer, int buffer_len, int bit_width); |
697 | | |
698 | | // Return the size of the current repeated run. Returns zero if the current run is |
699 | | // a literal run or if no more runs can be read from the input. |
700 | | int32_t NextNumRepeats(); |
701 | | |
702 | | // Get the value of the current repeated run and consume the given number of repeats. |
703 | | // Only valid to call when NextNumRepeats() > 0. The given number of repeats cannot |
704 | | // be greater than the remaining number of repeats in the run. 'num_repeats_to_consume' |
705 | | // can be set to 0 to peek at the value without consuming repeats. |
706 | | T GetRepeatedValue(int32_t num_repeats_to_consume); |
707 | | |
708 | | // Return the size of the current literal run. Returns zero if the current run is |
709 | | // a repeated run or if no more runs can be read from the input. |
710 | | int32_t NextNumLiterals(); |
711 | | |
712 | | // Consume 'num_literals_to_consume' literals from the current literal run, |
713 | | // copying the values to 'values'. 'num_literals_to_consume' must be <= |
714 | | // NextNumLiterals(). Returns true if the requested number of literals were |
715 | | // successfully read or false if an error was encountered, e.g. the input was |
716 | | // truncated. |
717 | | bool GetLiteralValues(int32_t num_literals_to_consume, T* values) WARN_UNUSED_RESULT; |
718 | | |
719 | | // Consume 'num_values_to_consume' values and copy them to 'values'. |
720 | | // Returns the number of consumed values or 0 if an error occurred. |
721 | | uint32_t GetBatch(T* values, uint32_t batch_num); |
722 | | |
723 | | private: |
724 | | // Called when both 'literal_count_' and 'repeat_count_' have been exhausted. |
725 | | // Sets either 'literal_count_' or 'repeat_count_' to the size of the next literal |
726 | | // or repeated run, or leaves both at 0 if no more values can be read (either because |
727 | | // the end of the input was reached or an error was encountered decoding). |
728 | | void NextCounts(); |
729 | | |
730 | | /// Fill the literal buffer. Invalid to call if there are already buffered literals. |
731 | | /// Return false if the input was truncated. This does not advance 'literal_count_'. |
732 | | bool FillLiteralBuffer() WARN_UNUSED_RESULT; |
733 | | |
734 | 1.06M | bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }_ZNK5doris15RleBatchDecoderItE20HaveBufferedLiteralsEv Line | Count | Source | 734 | 1.02M | bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; } |
_ZNK5doris15RleBatchDecoderIjE20HaveBufferedLiteralsEv Line | Count | Source | 734 | 30.1k | bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; } |
_ZNK5doris15RleBatchDecoderIhE20HaveBufferedLiteralsEv Line | Count | Source | 734 | 9 | bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; } |
|
735 | | |
736 | | /// Output buffered literals, advancing 'literal_buffer_pos_' and decrementing |
737 | | /// 'literal_count_'. Returns the number of literals outputted. |
738 | | int32_t OutputBufferedLiterals(int32_t max_to_output, T* values); |
739 | | |
740 | | BatchedBitReader bit_reader_; |
741 | | |
742 | | // Number of bits needed to encode the value. Must be between 0 and 64 after |
743 | | // the decoder is initialized with a buffer. -1 indicates the decoder was not |
744 | | // initialized. |
745 | | int bit_width_ = -1; |
746 | | |
747 | | // If a repeated run, the number of repeats remaining in the current run to be read. |
748 | | // If the current run is a literal run, this is 0. |
749 | | int32_t repeat_count_ = 0; |
750 | | |
751 | | // If a literal run, the number of literals remaining in the current run to be read. |
752 | | // If the current run is a repeated run, this is 0. |
753 | | int32_t literal_count_ = 0; |
754 | | |
755 | | // If a repeated run, the current repeated value. |
756 | | T repeated_value_; |
757 | | |
758 | | // Size of buffer for literal values. Large enough to decode a full batch of 32 |
759 | | // literals. The buffer is needed to allow clients to read in batches that are not |
760 | | // multiples of 32. |
761 | | static constexpr int LITERAL_BUFFER_LEN = 32; |
762 | | |
763 | | // Buffer containing 'num_buffered_literals_' values. 'literal_buffer_pos_' is the |
764 | | // position of the next literal to be read from the buffer. |
765 | | T literal_buffer_[LITERAL_BUFFER_LEN]; |
766 | | int num_buffered_literals_ = 0; |
767 | | int literal_buffer_pos_ = 0; |
768 | | }; |
769 | | |
770 | | template <typename T> |
771 | 1.06M | int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) { |
772 | 1.06M | int32_t num_to_output = |
773 | 1.06M | std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_); |
774 | 1.06M | memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output); |
775 | 1.06M | literal_buffer_pos_ += num_to_output; |
776 | 1.06M | literal_count_ -= num_to_output; |
777 | 1.06M | return num_to_output; |
778 | 1.06M | } _ZN5doris15RleBatchDecoderItE22OutputBufferedLiteralsEiPt Line | Count | Source | 771 | 1.02M | int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) { | 772 | 1.02M | int32_t num_to_output = | 773 | 1.02M | std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_); | 774 | 1.02M | memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output); | 775 | 1.02M | literal_buffer_pos_ += num_to_output; | 776 | 1.02M | literal_count_ -= num_to_output; | 777 | 1.02M | return num_to_output; | 778 | 1.02M | } |
_ZN5doris15RleBatchDecoderIjE22OutputBufferedLiteralsEiPj Line | Count | Source | 771 | 30.5k | int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) { | 772 | 30.5k | int32_t num_to_output = | 773 | 30.5k | std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_); | 774 | 30.5k | memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output); | 775 | 30.5k | literal_buffer_pos_ += num_to_output; | 776 | 30.5k | literal_count_ -= num_to_output; | 777 | 30.5k | return num_to_output; | 778 | 30.5k | } |
_ZN5doris15RleBatchDecoderIhE22OutputBufferedLiteralsEiPh Line | Count | Source | 771 | 6 | int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) { | 772 | 6 | int32_t num_to_output = | 773 | 6 | std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_); | 774 | 6 | memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output); | 775 | 6 | literal_buffer_pos_ += num_to_output; | 776 | 6 | literal_count_ -= num_to_output; | 777 | 6 | return num_to_output; | 778 | 6 | } |
|
779 | | |
780 | | template <typename T> |
781 | 13.1k | void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) { |
782 | 13.1k | bit_reader_.Reset(buffer, buffer_len); |
783 | 13.1k | bit_width_ = bit_width; |
784 | 13.1k | repeat_count_ = 0; |
785 | 13.1k | literal_count_ = 0; |
786 | 13.1k | num_buffered_literals_ = 0; |
787 | 13.1k | literal_buffer_pos_ = 0; |
788 | 13.1k | } _ZN5doris15RleBatchDecoderItE5ResetEPhii Line | Count | Source | 781 | 7.57k | void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) { | 782 | 7.57k | bit_reader_.Reset(buffer, buffer_len); | 783 | 7.57k | bit_width_ = bit_width; | 784 | 7.57k | repeat_count_ = 0; | 785 | 7.57k | literal_count_ = 0; | 786 | 7.57k | num_buffered_literals_ = 0; | 787 | 7.57k | literal_buffer_pos_ = 0; | 788 | 7.57k | } |
_ZN5doris15RleBatchDecoderIjE5ResetEPhii Line | Count | Source | 781 | 5.57k | void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) { | 782 | 5.57k | bit_reader_.Reset(buffer, buffer_len); | 783 | 5.57k | bit_width_ = bit_width; | 784 | 5.57k | repeat_count_ = 0; | 785 | 5.57k | literal_count_ = 0; | 786 | 5.57k | num_buffered_literals_ = 0; | 787 | 5.57k | literal_buffer_pos_ = 0; | 788 | 5.57k | } |
_ZN5doris15RleBatchDecoderIhE5ResetEPhii Line | Count | Source | 781 | 6 | void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) { | 782 | 6 | bit_reader_.Reset(buffer, buffer_len); | 783 | 6 | bit_width_ = bit_width; | 784 | 6 | repeat_count_ = 0; | 785 | 6 | literal_count_ = 0; | 786 | 6 | num_buffered_literals_ = 0; | 787 | 6 | literal_buffer_pos_ = 0; | 788 | 6 | } |
|
789 | | |
790 | | template <typename T> |
791 | 9.46M | int32_t RleBatchDecoder<T>::NextNumRepeats() { |
792 | 9.46M | if (repeat_count_ > 0) return repeat_count_; |
793 | 1.16M | if (literal_count_ == 0) NextCounts(); |
794 | 1.16M | return repeat_count_; |
795 | 9.46M | } _ZN5doris15RleBatchDecoderItE14NextNumRepeatsEv Line | Count | Source | 791 | 9.43M | int32_t RleBatchDecoder<T>::NextNumRepeats() { | 792 | 9.43M | if (repeat_count_ > 0) return repeat_count_; | 793 | 1.12M | if (literal_count_ == 0) NextCounts(); | 794 | 1.12M | return repeat_count_; | 795 | 9.43M | } |
_ZN5doris15RleBatchDecoderIjE14NextNumRepeatsEv Line | Count | Source | 791 | 31.5k | int32_t RleBatchDecoder<T>::NextNumRepeats() { | 792 | 31.5k | if (repeat_count_ > 0) return repeat_count_; | 793 | 31.0k | if (literal_count_ == 0) NextCounts(); | 794 | 31.0k | return repeat_count_; | 795 | 31.5k | } |
_ZN5doris15RleBatchDecoderIhE14NextNumRepeatsEv Line | Count | Source | 791 | 265 | int32_t RleBatchDecoder<T>::NextNumRepeats() { | 792 | 265 | if (repeat_count_ > 0) return repeat_count_; | 793 | 10 | if (literal_count_ == 0) NextCounts(); | 794 | 10 | return repeat_count_; | 795 | 265 | } |
|
796 | | |
797 | | template <typename T> |
798 | 213k | void RleBatchDecoder<T>::NextCounts() { |
799 | | // Read the next run's indicator int, it could be a literal or repeated run. |
800 | | // The int is encoded as a ULEB128-encoded value. |
801 | 213k | uint32_t indicator_value = 0; |
802 | 213k | if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) { |
803 | 2 | return; |
804 | 2 | } |
805 | | |
806 | | // lsb indicates if it is a literal run or repeated run |
807 | 213k | bool is_literal = indicator_value & 1; |
808 | | |
809 | | // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully. |
810 | | // The Parquet standard does not allow longer runs - see PARQUET-1290. |
811 | 213k | uint32_t run_len = indicator_value >> 1; |
812 | 213k | if (is_literal) { |
813 | | // Use int64_t to avoid overflowing multiplication. |
814 | 113k | int64_t literal_count = static_cast<int64_t>(run_len) * 8; |
815 | 113k | if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return; |
816 | 113k | literal_count_ = cast_set<int32_t>(literal_count); |
817 | 113k | } else { |
818 | 100k | if (UNLIKELY(run_len == 0)) return; |
819 | 100k | bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_); |
820 | 100k | if (UNLIKELY(!result)) return; |
821 | 100k | repeat_count_ = run_len; |
822 | 100k | } |
823 | 213k | } _ZN5doris15RleBatchDecoderItE10NextCountsEv Line | Count | Source | 798 | 195k | void RleBatchDecoder<T>::NextCounts() { | 799 | | // Read the next run's indicator int, it could be a literal or repeated run. | 800 | | // The int is encoded as a ULEB128-encoded value. | 801 | 195k | uint32_t indicator_value = 0; | 802 | 195k | if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) { | 803 | 2 | return; | 804 | 2 | } | 805 | | | 806 | | // lsb indicates if it is a literal run or repeated run | 807 | 195k | bool is_literal = indicator_value & 1; | 808 | | | 809 | | // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully. | 810 | | // The Parquet standard does not allow longer runs - see PARQUET-1290. | 811 | 195k | uint32_t run_len = indicator_value >> 1; | 812 | 195k | if (is_literal) { | 813 | | // Use int64_t to avoid overflowing multiplication. | 814 | 95.8k | int64_t literal_count = static_cast<int64_t>(run_len) * 8; | 815 | 95.8k | if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return; | 816 | 95.8k | literal_count_ = cast_set<int32_t>(literal_count); | 817 | 99.6k | } else { | 818 | 99.6k | if (UNLIKELY(run_len == 0)) return; | 819 | 99.6k | bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_); | 820 | 99.6k | if (UNLIKELY(!result)) return; | 821 | 99.6k | repeat_count_ = run_len; | 822 | 99.6k | } | 823 | 195k | } |
_ZN5doris15RleBatchDecoderIjE10NextCountsEv Line | Count | Source | 798 | 18.3k | void RleBatchDecoder<T>::NextCounts() { | 799 | | // Read the next run's indicator int, it could be a literal or repeated run. | 800 | | // The int is encoded as a ULEB128-encoded value. | 801 | 18.3k | uint32_t indicator_value = 0; | 802 | 18.3k | if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) { | 803 | 0 | return; | 804 | 0 | } | 805 | | | 806 | | // lsb indicates if it is a literal run or repeated run | 807 | 18.3k | bool is_literal = indicator_value & 1; | 808 | | | 809 | | // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully. | 810 | | // The Parquet standard does not allow longer runs - see PARQUET-1290. | 811 | 18.3k | uint32_t run_len = indicator_value >> 1; | 812 | 18.3k | if (is_literal) { | 813 | | // Use int64_t to avoid overflowing multiplication. | 814 | 17.4k | int64_t literal_count = static_cast<int64_t>(run_len) * 8; | 815 | 17.4k | if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return; | 816 | 17.4k | literal_count_ = cast_set<int32_t>(literal_count); | 817 | 17.4k | } else { | 818 | 851 | if (UNLIKELY(run_len == 0)) return; | 819 | 851 | bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_); | 820 | 851 | if (UNLIKELY(!result)) return; | 821 | 851 | repeat_count_ = run_len; | 822 | 851 | } | 823 | 18.3k | } |
_ZN5doris15RleBatchDecoderIhE10NextCountsEv Line | Count | Source | 798 | 8 | void RleBatchDecoder<T>::NextCounts() { | 799 | | // Read the next run's indicator int, it could be a literal or repeated run. | 800 | | // The int is encoded as a ULEB128-encoded value. | 801 | 8 | uint32_t indicator_value = 0; | 802 | 8 | if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) { | 803 | 0 | return; | 804 | 0 | } | 805 | | | 806 | | // lsb indicates if it is a literal run or repeated run | 807 | 8 | bool is_literal = indicator_value & 1; | 808 | | | 809 | | // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully. | 810 | | // The Parquet standard does not allow longer runs - see PARQUET-1290. | 811 | 8 | uint32_t run_len = indicator_value >> 1; | 812 | 8 | if (is_literal) { | 813 | | // Use int64_t to avoid overflowing multiplication. | 814 | 7 | int64_t literal_count = static_cast<int64_t>(run_len) * 8; | 815 | 7 | if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return; | 816 | 7 | literal_count_ = cast_set<int32_t>(literal_count); | 817 | 7 | } else { | 818 | 1 | if (UNLIKELY(run_len == 0)) return; | 819 | 1 | bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_); | 820 | 1 | if (UNLIKELY(!result)) return; | 821 | 1 | repeat_count_ = run_len; | 822 | 1 | } | 823 | 8 | } |
|
824 | | |
825 | | template <typename T> |
826 | 8.40M | T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) { |
827 | 8.40M | repeat_count_ -= num_repeats_to_consume; |
828 | 8.40M | return repeated_value_; |
829 | 8.40M | } _ZN5doris15RleBatchDecoderItE16GetRepeatedValueEi Line | Count | Source | 826 | 8.40M | T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) { | 827 | 8.40M | repeat_count_ -= num_repeats_to_consume; | 828 | 8.40M | return repeated_value_; | 829 | 8.40M | } |
_ZN5doris15RleBatchDecoderIjE16GetRepeatedValueEi Line | Count | Source | 826 | 1.38k | T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) { | 827 | 1.38k | repeat_count_ -= num_repeats_to_consume; | 828 | 1.38k | return repeated_value_; | 829 | 1.38k | } |
_ZN5doris15RleBatchDecoderIhE16GetRepeatedValueEi Line | Count | Source | 826 | 256 | T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) { | 827 | 256 | repeat_count_ -= num_repeats_to_consume; | 828 | 256 | return repeated_value_; | 829 | 256 | } |
|
830 | | |
831 | | template <typename T> |
832 | 1.06M | int32_t RleBatchDecoder<T>::NextNumLiterals() { |
833 | 1.06M | if (literal_count_ > 0) return literal_count_; |
834 | 1 | if (repeat_count_ == 0) NextCounts(); |
835 | 1 | return literal_count_; |
836 | 1.06M | } _ZN5doris15RleBatchDecoderItE15NextNumLiteralsEv Line | Count | Source | 832 | 1.02M | int32_t RleBatchDecoder<T>::NextNumLiterals() { | 833 | 1.02M | if (literal_count_ > 0) return literal_count_; | 834 | 1 | if (repeat_count_ == 0) NextCounts(); | 835 | 1 | return literal_count_; | 836 | 1.02M | } |
_ZN5doris15RleBatchDecoderIjE15NextNumLiteralsEv Line | Count | Source | 832 | 30.1k | int32_t RleBatchDecoder<T>::NextNumLiterals() { | 833 | 30.1k | if (literal_count_ > 0) return literal_count_; | 834 | 0 | if (repeat_count_ == 0) NextCounts(); | 835 | 0 | return literal_count_; | 836 | 30.1k | } |
_ZN5doris15RleBatchDecoderIhE15NextNumLiteralsEv Line | Count | Source | 832 | 9 | int32_t RleBatchDecoder<T>::NextNumLiterals() { | 833 | 9 | if (literal_count_ > 0) return literal_count_; | 834 | 0 | if (repeat_count_ == 0) NextCounts(); | 835 | 0 | return literal_count_; | 836 | 9 | } |
|
837 | | |
838 | | template <typename T> |
839 | 1.06M | bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) { |
840 | 1.06M | int32_t num_consumed = 0; |
841 | | // Copy any buffered literals left over from previous calls. |
842 | 1.06M | if (HaveBufferedLiterals()) { |
843 | 939k | num_consumed = OutputBufferedLiterals(num_literals_to_consume, values); |
844 | 939k | } |
845 | | |
846 | 1.06M | int32_t num_remaining = num_literals_to_consume - num_consumed; |
847 | | // Copy literals directly to the output, bypassing 'literal_buffer_' when possible. |
848 | | // Need to round to a batch of 32 if the caller is consuming only part of the current |
849 | | // run avoid ending on a non-byte boundary. |
850 | 1.06M | int32_t num_to_bypass = |
851 | 1.06M | std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32)); |
852 | 1.06M | if (num_to_bypass > 0) { |
853 | 13.8k | int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed); |
854 | | // If we couldn't read the expected number, that means the input was truncated. |
855 | 13.8k | if (num_read < num_to_bypass) return false; |
856 | 13.8k | literal_count_ -= num_to_bypass; |
857 | 13.8k | num_consumed += num_to_bypass; |
858 | 13.8k | num_remaining = num_literals_to_consume - num_consumed; |
859 | 13.8k | } |
860 | | |
861 | 1.06M | if (num_remaining > 0) { |
862 | | // We weren't able to copy all the literals requested directly from the input. |
863 | | // Buffer literals and copy over the requested number. |
864 | 120k | if (UNLIKELY(!FillLiteralBuffer())) return false; |
865 | 120k | OutputBufferedLiterals(num_remaining, values + num_consumed); |
866 | 120k | } |
867 | 1.06M | return true; |
868 | 1.06M | } _ZN5doris15RleBatchDecoderItE16GetLiteralValuesEiPt Line | Count | Source | 839 | 1.02M | bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) { | 840 | 1.02M | int32_t num_consumed = 0; | 841 | | // Copy any buffered literals left over from previous calls. | 842 | 1.02M | if (HaveBufferedLiterals()) { | 843 | 927k | num_consumed = OutputBufferedLiterals(num_literals_to_consume, values); | 844 | 927k | } | 845 | | | 846 | 1.02M | int32_t num_remaining = num_literals_to_consume - num_consumed; | 847 | | // Copy literals directly to the output, bypassing 'literal_buffer_' when possible. | 848 | | // Need to round to a batch of 32 if the caller is consuming only part of the current | 849 | | // run avoid ending on a non-byte boundary. | 850 | 1.02M | int32_t num_to_bypass = | 851 | 1.02M | std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32)); | 852 | 1.02M | if (num_to_bypass > 0) { | 853 | 204 | int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed); | 854 | | // If we couldn't read the expected number, that means the input was truncated. | 855 | 204 | if (num_read < num_to_bypass) return false; | 856 | 204 | literal_count_ -= num_to_bypass; | 857 | 204 | num_consumed += num_to_bypass; | 858 | 204 | num_remaining = num_literals_to_consume - num_consumed; | 859 | 204 | } | 860 | | | 861 | 1.02M | if (num_remaining > 0) { | 862 | | // We weren't able to copy all the literals requested directly from the input. | 863 | | // Buffer literals and copy over the requested number. | 864 | 102k | if (UNLIKELY(!FillLiteralBuffer())) return false; | 865 | 102k | OutputBufferedLiterals(num_remaining, values + num_consumed); | 866 | 102k | } | 867 | 1.02M | return true; | 868 | 1.02M | } |
_ZN5doris15RleBatchDecoderIjE16GetLiteralValuesEiPj Line | Count | Source | 839 | 30.1k | bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) { | 840 | 30.1k | int32_t num_consumed = 0; | 841 | | // Copy any buffered literals left over from previous calls. | 842 | 30.1k | if (HaveBufferedLiterals()) { | 843 | 12.2k | num_consumed = OutputBufferedLiterals(num_literals_to_consume, values); | 844 | 12.2k | } | 845 | | | 846 | 30.1k | int32_t num_remaining = num_literals_to_consume - num_consumed; | 847 | | // Copy literals directly to the output, bypassing 'literal_buffer_' when possible. | 848 | | // Need to round to a batch of 32 if the caller is consuming only part of the current | 849 | | // run avoid ending on a non-byte boundary. | 850 | 30.1k | int32_t num_to_bypass = | 851 | 30.1k | std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32)); | 852 | 30.1k | if (num_to_bypass > 0) { | 853 | 13.6k | int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed); | 854 | | // If we couldn't read the expected number, that means the input was truncated. | 855 | 13.6k | if (num_read < num_to_bypass) return false; | 856 | 13.6k | literal_count_ -= num_to_bypass; | 857 | 13.6k | num_consumed += num_to_bypass; | 858 | 13.6k | num_remaining = num_literals_to_consume - num_consumed; | 859 | 13.6k | } | 860 | | | 861 | 30.1k | if (num_remaining > 0) { | 862 | | // We weren't able to copy all the literals requested directly from the input. | 863 | | // Buffer literals and copy over the requested number. | 864 | 18.3k | if (UNLIKELY(!FillLiteralBuffer())) return false; | 865 | 18.3k | OutputBufferedLiterals(num_remaining, values + num_consumed); | 866 | 18.3k | } | 867 | 30.1k | return true; | 868 | 30.1k | } |
_ZN5doris15RleBatchDecoderIhE16GetLiteralValuesEiPh Line | Count | Source | 839 | 9 | bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) { | 840 | 9 | int32_t num_consumed = 0; | 841 | | // Copy any buffered literals left over from previous calls. | 842 | 9 | if (HaveBufferedLiterals()) { | 843 | 2 | num_consumed = OutputBufferedLiterals(num_literals_to_consume, values); | 844 | 2 | } | 845 | | | 846 | 9 | int32_t num_remaining = num_literals_to_consume - num_consumed; | 847 | | // Copy literals directly to the output, bypassing 'literal_buffer_' when possible. | 848 | | // Need to round to a batch of 32 if the caller is consuming only part of the current | 849 | | // run avoid ending on a non-byte boundary. | 850 | 9 | int32_t num_to_bypass = | 851 | 9 | std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32)); | 852 | 9 | if (num_to_bypass > 0) { | 853 | 0 | int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed); | 854 | | // If we couldn't read the expected number, that means the input was truncated. | 855 | 0 | if (num_read < num_to_bypass) return false; | 856 | 0 | literal_count_ -= num_to_bypass; | 857 | 0 | num_consumed += num_to_bypass; | 858 | 0 | num_remaining = num_literals_to_consume - num_consumed; | 859 | 0 | } | 860 | | | 861 | 9 | if (num_remaining > 0) { | 862 | | // We weren't able to copy all the literals requested directly from the input. | 863 | | // Buffer literals and copy over the requested number. | 864 | 7 | if (UNLIKELY(!FillLiteralBuffer())) return false; | 865 | 4 | OutputBufferedLiterals(num_remaining, values + num_consumed); | 866 | 4 | } | 867 | 6 | return true; | 868 | 9 | } |
|
869 | | |
870 | | template <typename T> |
871 | 120k | bool RleBatchDecoder<T>::FillLiteralBuffer() { |
872 | 120k | int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_); |
873 | 120k | num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_); |
874 | | // If we couldn't read the expected number, that means the input was truncated. |
875 | 120k | if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false; |
876 | 120k | literal_buffer_pos_ = 0; |
877 | 120k | return true; |
878 | 120k | } _ZN5doris15RleBatchDecoderItE17FillLiteralBufferEv Line | Count | Source | 871 | 102k | bool RleBatchDecoder<T>::FillLiteralBuffer() { | 872 | 102k | int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_); | 873 | 102k | num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_); | 874 | | // If we couldn't read the expected number, that means the input was truncated. | 875 | 102k | if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false; | 876 | 102k | literal_buffer_pos_ = 0; | 877 | 102k | return true; | 878 | 102k | } |
_ZN5doris15RleBatchDecoderIjE17FillLiteralBufferEv Line | Count | Source | 871 | 18.3k | bool RleBatchDecoder<T>::FillLiteralBuffer() { | 872 | 18.3k | int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_); | 873 | 18.3k | num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_); | 874 | | // If we couldn't read the expected number, that means the input was truncated. | 875 | 18.3k | if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false; | 876 | 18.3k | literal_buffer_pos_ = 0; | 877 | 18.3k | return true; | 878 | 18.3k | } |
_ZN5doris15RleBatchDecoderIhE17FillLiteralBufferEv Line | Count | Source | 871 | 7 | bool RleBatchDecoder<T>::FillLiteralBuffer() { | 872 | 7 | int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_); | 873 | 7 | num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_); | 874 | | // If we couldn't read the expected number, that means the input was truncated. | 875 | 7 | if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false; | 876 | 4 | literal_buffer_pos_ = 0; | 877 | 4 | return true; | 878 | 7 | } |
|
879 | | |
880 | | template <typename T> |
881 | 9.31M | uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) { |
882 | 9.31M | uint32_t num_consumed = 0; |
883 | 18.6M | while (num_consumed < batch_num) { |
884 | | // Add RLE encoded values by repeating the current value this number of times. |
885 | 9.32M | uint32_t num_repeats = NextNumRepeats(); |
886 | 9.32M | if (num_repeats > 0) { |
887 | 8.40M | int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed); |
888 | 8.40M | T repeated_value = GetRepeatedValue(num_repeats_to_set); |
889 | 28.3M | for (int i = 0; i < num_repeats_to_set; ++i) { |
890 | 19.8M | values[num_consumed + i] = repeated_value; |
891 | 19.8M | } |
892 | 8.40M | num_consumed += num_repeats_to_set; |
893 | 8.40M | continue; |
894 | 8.40M | } |
895 | | |
896 | | // Add remaining literal values, if any. |
897 | 920k | uint32_t num_literals = NextNumLiterals(); |
898 | 920k | if (num_literals == 0) { |
899 | 1 | break; |
900 | 1 | } |
901 | 920k | uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed); |
902 | 920k | if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) { |
903 | 4 | return 0; |
904 | 4 | } |
905 | 920k | num_consumed += num_literals_to_set; |
906 | 920k | } |
907 | 9.31M | return num_consumed; |
908 | 9.31M | } _ZN5doris15RleBatchDecoderItE8GetBatchEPtj Line | Count | Source | 881 | 9.31M | uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) { | 882 | 9.31M | uint32_t num_consumed = 0; | 883 | 18.6M | while (num_consumed < batch_num) { | 884 | | // Add RLE encoded values by repeating the current value this number of times. | 885 | 9.31M | uint32_t num_repeats = NextNumRepeats(); | 886 | 9.31M | if (num_repeats > 0) { | 887 | 8.40M | int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed); | 888 | 8.40M | T repeated_value = GetRepeatedValue(num_repeats_to_set); | 889 | 26.1M | for (int i = 0; i < num_repeats_to_set; ++i) { | 890 | 17.7M | values[num_consumed + i] = repeated_value; | 891 | 17.7M | } | 892 | 8.40M | num_consumed += num_repeats_to_set; | 893 | 8.40M | continue; | 894 | 8.40M | } | 895 | | | 896 | | // Add remaining literal values, if any. | 897 | 914k | uint32_t num_literals = NextNumLiterals(); | 898 | 914k | if (num_literals == 0) { | 899 | 1 | break; | 900 | 1 | } | 901 | 914k | uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed); | 902 | 914k | if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) { | 903 | 1 | return 0; | 904 | 1 | } | 905 | 914k | num_consumed += num_literals_to_set; | 906 | 914k | } | 907 | 9.31M | return num_consumed; | 908 | 9.31M | } |
_ZN5doris15RleBatchDecoderIjE8GetBatchEPjj Line | Count | Source | 881 | 2.95k | uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) { | 882 | 2.95k | uint32_t num_consumed = 0; | 883 | 9.86k | while (num_consumed < batch_num) { | 884 | | // Add RLE encoded values by repeating the current value this number of times. | 885 | 6.91k | uint32_t num_repeats = NextNumRepeats(); | 886 | 6.91k | if (num_repeats > 0) { | 887 | 934 | int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed); | 888 | 934 | T repeated_value = GetRepeatedValue(num_repeats_to_set); | 889 | 1.12M | for (int i = 0; i < num_repeats_to_set; ++i) { | 890 | 1.12M | values[num_consumed + i] = repeated_value; | 891 | 1.12M | } | 892 | 934 | num_consumed += num_repeats_to_set; | 893 | 934 | continue; | 894 | 934 | } | 895 | | | 896 | | // Add remaining literal values, if any. | 897 | 5.98k | uint32_t num_literals = NextNumLiterals(); | 898 | 5.98k | if (num_literals == 0) { | 899 | 0 | break; | 900 | 0 | } | 901 | 5.98k | uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed); | 902 | 5.98k | if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) { | 903 | 0 | return 0; | 904 | 0 | } | 905 | 5.98k | num_consumed += num_literals_to_set; | 906 | 5.98k | } | 907 | 2.95k | return num_consumed; | 908 | 2.95k | } |
_ZN5doris15RleBatchDecoderIhE8GetBatchEPhj Line | Count | Source | 881 | 263 | uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) { | 882 | 263 | uint32_t num_consumed = 0; | 883 | 525 | while (num_consumed < batch_num) { | 884 | | // Add RLE encoded values by repeating the current value this number of times. | 885 | 265 | uint32_t num_repeats = NextNumRepeats(); | 886 | 265 | if (num_repeats > 0) { | 887 | 256 | int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed); | 888 | 256 | T repeated_value = GetRepeatedValue(num_repeats_to_set); | 889 | 1.04M | for (int i = 0; i < num_repeats_to_set; ++i) { | 890 | 1.04M | values[num_consumed + i] = repeated_value; | 891 | 1.04M | } | 892 | 256 | num_consumed += num_repeats_to_set; | 893 | 256 | continue; | 894 | 256 | } | 895 | | | 896 | | // Add remaining literal values, if any. | 897 | 9 | uint32_t num_literals = NextNumLiterals(); | 898 | 9 | if (num_literals == 0) { | 899 | 0 | break; | 900 | 0 | } | 901 | 9 | uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed); | 902 | 9 | if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) { | 903 | 3 | return 0; | 904 | 3 | } | 905 | 6 | num_consumed += num_literals_to_set; | 906 | 6 | } | 907 | 260 | return num_consumed; | 908 | 263 | } |
|
909 | | } // namespace doris |