Coverage Report

Created: 2026-04-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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 <limits> // IWYU pragma: keep
22
23
#include "common/cast_set.h"
24
#include "util/bit_stream_utils.inline.h"
25
#include "util/bit_util.h"
26
27
namespace doris {
28
#include "common/compile_check_begin.h"
29
30
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
31
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
32
// (literal encoding).
33
// For both types of runs, there is a byte-aligned indicator which encodes the length
34
// of the run and the type of the run.
35
// This encoding has the benefit that when there aren't any long enough runs, values
36
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
37
// the run length are byte aligned. This allows for very efficient decoding
38
// implementations.
39
// The encoding is:
40
//    encoded-block := run*
41
//    run := literal-run | repeated-run
42
//    literal-run := literal-indicator < literal bytes >
43
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
44
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
45
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
46
//
47
// Each run is preceded by a varint. The varint's least significant bit is
48
// used to indicate whether the run is a literal run or a repeated run. The rest
49
// of the varint is used to determine the length of the run (eg how many times the
50
// value repeats).
51
//
52
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
53
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
54
// on a byte boundary without padding.
55
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
56
// the actual number of encoded ints. (This means that the total number of encoded values
57
// can not be determined from the encoded data, since the number of values in the last
58
// group may not be a multiple of 8).
59
// There is a break-even point when it is more storage efficient to do run length
60
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
61
// for both the repeated encoding or the literal encoding.  This value can always
62
// be computed based on the bit-width.
63
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
64
//
65
// Examples with bit-width 1 (eg encoding booleans):
66
// ----------------------------------------
67
// 100 1s followed by 100 0s:
68
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
69
//  - (total 4 bytes)
70
//
71
// alternating 1s and 0s (200 total):
72
// 200 ints = 25 groups of 8
73
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
74
// (total 26 bytes, 1 byte overhead)
75
//
76
77
// Decoder class for RLE encoded data.
78
//
79
// NOTE: the encoded format does not have any length prefix or any other way of
80
// indicating that the encoded sequence ends at a certain point, so the Decoder
81
// methods may return some extra bits at the end before the read methods start
82
// to return 0/false.
83
template <typename T>
84
class RleDecoder {
85
public:
86
    // Create a decoder object. buffer/buffer_len is the decoded data.
87
    // bit_width is the width of each value (before encoding).
88
    RleDecoder(const uint8_t* buffer, int buffer_len, int bit_width)
89
1.26M
            : bit_reader_(buffer, buffer_len),
90
1.26M
              bit_width_(bit_width),
91
1.26M
              current_value_(0),
92
1.26M
              repeat_count_(0),
93
1.26M
              literal_count_(0),
94
1.26M
              rewind_state_(CANT_REWIND) {
95
1.26M
        DCHECK_GE(bit_width_, 1);
96
1.26M
        DCHECK_LE(bit_width_, 64);
97
1.26M
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
89
487k
            : bit_reader_(buffer, buffer_len),
90
487k
              bit_width_(bit_width),
91
487k
              current_value_(0),
92
487k
              repeat_count_(0),
93
487k
              literal_count_(0),
94
487k
              rewind_state_(CANT_REWIND) {
95
487k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
487k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
89
23.4k
            : bit_reader_(buffer, buffer_len),
90
23.4k
              bit_width_(bit_width),
91
23.4k
              current_value_(0),
92
23.4k
              repeat_count_(0),
93
23.4k
              literal_count_(0),
94
23.4k
              rewind_state_(CANT_REWIND) {
95
23.4k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
23.4k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
89
750k
            : bit_reader_(buffer, buffer_len),
90
750k
              bit_width_(bit_width),
91
750k
              current_value_(0),
92
750k
              repeat_count_(0),
93
750k
              literal_count_(0),
94
750k
              rewind_state_(CANT_REWIND) {
95
750k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
750k
    }
98
99
37.1M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
99
36.7M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
99
20.6k
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
99
348k
    RleDecoder() {}
100
101
    // Skip n values, and returns the number of non-zero entries skipped.
102
    size_t Skip(size_t to_skip);
103
104
    // Gets the next value.  Returns false if there are no more.
105
    bool Get(T* val);
106
107
    // Seek to the previous value.
108
    void RewindOne();
109
110
    // Gets the next run of the same 'val'. Returns 0 if there is no
111
    // more data to be decoded. Will return a run of at most 'max_run'
112
    // values. If there are more values than this, the next call to
113
    // GetNextRun will return more from the same run.
114
    size_t GetNextRun(T* val, size_t max_run);
115
116
    size_t get_values(T* values, size_t num_values);
117
118
    // Get the count of current repeated value
119
    size_t repeated_count();
120
121
    // Get current repeated value, make sure that count equals repeated_count()
122
    T get_repeated_value(size_t count);
123
124
0
    const BitReader& bit_reader() const { return bit_reader_; }
125
126
private:
127
    bool ReadHeader();
128
129
    enum RewindState { REWIND_LITERAL, REWIND_RUN, CANT_REWIND };
130
131
    BitReader bit_reader_;
132
    int bit_width_;
133
    uint64_t current_value_;
134
    uint32_t repeat_count_;
135
    uint32_t literal_count_;
136
    RewindState rewind_state_;
137
};
138
139
// Class to incrementally build the rle data.
140
// The encoding has two modes: encoding repeated runs and literal runs.
141
// If the run is sufficiently short, it is more efficient to encode as a literal run.
142
// This class does so by buffering 8 values at a time.  If they are not all the same
143
// they are added to the literal run.  If they are the same, they are added to the
144
// repeated run.  When we switch modes, the previous run is flushed out.
145
template <typename T>
146
class RleEncoder {
147
public:
148
    // buffer: buffer to write bits to.
149
    // bit_width: max number of bits for value.
150
    // TODO: consider adding a min_repeated_run_length so the caller can control
151
    // when values should be encoded as repeated runs.  Currently this is derived
152
    // based on the bit_width, which can determine a storage optimal choice.
153
    explicit RleEncoder(faststring* buffer, int bit_width)
154
533k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
533k
        DCHECK_GE(bit_width_, 1);
156
533k
        DCHECK_LE(bit_width_, 64);
157
533k
        Clear();
158
533k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
154
13.5k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
13.5k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
13.5k
        Clear();
158
13.5k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
154
519k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
519k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
519k
        Clear();
158
519k
    }
159
160
    // Reserve 'num_bytes' bytes for a plain encoded header, set each
161
    // byte with 'val': this is used for the RLE-encoded data blocks in
162
    // order to be able to able to store the initial ordinal position
163
    // and number of elements. This is a part of RleEncoder in order to
164
    // maintain the correct offset in 'buffer'.
165
    void Reserve(int num_bytes, uint8_t val);
166
167
    // Encode value. This value must be representable with bit_width_ bits.
168
    void Put(T value, size_t run_length = 1);
169
170
    // Flushes any pending values to the underlying buffer.
171
    // Returns the total number of bytes written
172
    int Flush();
173
174
    // Resets all the state in the encoder.
175
    void Clear();
176
177
97.6k
    int32_t len() const { return bit_writer_.bytes_written(); }
178
179
private:
180
    // Flushes any buffered values.  If this is part of a repeated run, this is largely
181
    // a no-op.
182
    // If it is part of a literal run, this will call FlushLiteralRun, which writes
183
    // out the buffered literal values.
184
    // If 'done' is true, the current run would be written even if it would normally
185
    // have been buffered more.  This should only be called at the end, when the
186
    // encoder has received all values even if it would normally continue to be
187
    // buffered.
188
    void FlushBufferedValues(bool done);
189
190
    // Flushes literal values to the underlying buffer.  If update_indicator_byte,
191
    // then the current literal run is complete and the indicator byte is updated.
192
    void FlushLiteralRun(bool update_indicator_byte);
193
194
    // Flushes a repeated run to the underlying buffer.
195
    void FlushRepeatedRun();
196
197
    // Number of bits needed to encode the value.
198
    const int bit_width_;
199
200
    // Underlying buffer.
201
    BitWriter bit_writer_;
202
203
    // We need to buffer at most 8 values for literals.  This happens when the
204
    // bit_width is 1 (so 8 values fit in one byte).
205
    // TODO: generalize this to other bit widths
206
    uint64_t buffered_values_[8];
207
208
    // Number of values in buffered_values_
209
    int num_buffered_values_;
210
211
    // The current (also last) value that was written and the count of how
212
    // many times in a row that value has been seen.  This is maintained even
213
    // if we are in a literal run.  If the repeat_count_ get high enough, we switch
214
    // to encoding repeated runs.
215
    uint64_t current_value_;
216
    int repeat_count_;
217
218
    // Number of literals in the current run.  This does not include the literals
219
    // that might be in buffered_values_.  Only after we've got a group big enough
220
    // can we decide if they should part of the literal_count_ or repeat_count_
221
    int literal_count_;
222
223
    // Index of a byte in the underlying buffer that stores the indicator byte.
224
    // This is reserved as soon as we need a literal run but the value is written
225
    // when the literal run is complete. We maintain an index rather than a pointer
226
    // into the underlying buffer because the pointer value may become invalid if
227
    // the underlying buffer is resized.
228
    int literal_indicator_byte_idx_;
229
};
230
231
template <typename T>
232
136M
bool RleDecoder<T>::ReadHeader() {
233
136M
    DCHECK(bit_reader_.is_initialized());
234
136M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
8.12M
        uint32_t indicator_value = 0;
238
8.12M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
8.12M
        if (!result) [[unlikely]] {
240
2.76k
            return false;
241
2.76k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
8.12M
        bool is_literal = indicator_value & 1;
245
8.12M
        if (is_literal) {
246
3.93M
            literal_count_ = (indicator_value >> 1) * 8;
247
3.93M
            DCHECK_GT(literal_count_, 0);
248
4.18M
        } else {
249
4.18M
            repeat_count_ = indicator_value >> 1;
250
4.18M
            DCHECK_GT(repeat_count_, 0);
251
4.18M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
4.18M
                                                     reinterpret_cast<T*>(&current_value_));
253
4.18M
            DCHECK(result1);
254
4.18M
        }
255
8.12M
    }
256
136M
    return true;
257
136M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
232
122M
bool RleDecoder<T>::ReadHeader() {
233
122M
    DCHECK(bit_reader_.is_initialized());
234
122M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
5.84M
        uint32_t indicator_value = 0;
238
5.84M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
5.84M
        if (!result) [[unlikely]] {
240
53
            return false;
241
53
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
5.84M
        bool is_literal = indicator_value & 1;
245
5.84M
        if (is_literal) {
246
2.78M
            literal_count_ = (indicator_value >> 1) * 8;
247
2.78M
            DCHECK_GT(literal_count_, 0);
248
3.05M
        } else {
249
3.05M
            repeat_count_ = indicator_value >> 1;
250
3.05M
            DCHECK_GT(repeat_count_, 0);
251
3.05M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
3.05M
                                                     reinterpret_cast<T*>(&current_value_));
253
3.05M
            DCHECK(result1);
254
3.05M
        }
255
5.84M
    }
256
122M
    return true;
257
122M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
232
6.04M
bool RleDecoder<T>::ReadHeader() {
233
6.04M
    DCHECK(bit_reader_.is_initialized());
234
6.04M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
2.08M
        uint32_t indicator_value = 0;
238
2.08M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
2.08M
        if (!result) [[unlikely]] {
240
2.70k
            return false;
241
2.70k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
2.07M
        bool is_literal = indicator_value & 1;
245
2.07M
        if (is_literal) {
246
1.08M
            literal_count_ = (indicator_value >> 1) * 8;
247
1.08M
            DCHECK_GT(literal_count_, 0);
248
1.08M
        } else {
249
995k
            repeat_count_ = indicator_value >> 1;
250
995k
            DCHECK_GT(repeat_count_, 0);
251
995k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
995k
                                                     reinterpret_cast<T*>(&current_value_));
253
995k
            DCHECK(result1);
254
995k
        }
255
2.07M
    }
256
6.04M
    return true;
257
6.04M
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
232
8.03M
bool RleDecoder<T>::ReadHeader() {
233
8.03M
    DCHECK(bit_reader_.is_initialized());
234
8.03M
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
235
        // Read the next run's indicator int, it could be a literal or repeated run
236
        // The int is encoded as a vlq-encoded value.
237
193k
        uint32_t indicator_value = 0;
238
193k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
193k
        if (!result) [[unlikely]] {
240
0
            return false;
241
0
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
193k
        bool is_literal = indicator_value & 1;
245
193k
        if (is_literal) {
246
66.1k
            literal_count_ = (indicator_value >> 1) * 8;
247
66.1k
            DCHECK_GT(literal_count_, 0);
248
127k
        } else {
249
127k
            repeat_count_ = indicator_value >> 1;
250
127k
            DCHECK_GT(repeat_count_, 0);
251
127k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
127k
                                                     reinterpret_cast<T*>(&current_value_));
253
127k
            DCHECK(result1);
254
127k
        }
255
193k
    }
256
8.03M
    return true;
257
8.03M
}
258
259
template <typename T>
260
125M
bool RleDecoder<T>::Get(T* val) {
261
125M
    DCHECK(bit_reader_.is_initialized());
262
125M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
125M
    if (repeat_count_ > 0) [[likely]] {
267
61.6M
        *val = cast_set<T>(current_value_);
268
61.6M
        --repeat_count_;
269
61.6M
        rewind_state_ = REWIND_RUN;
270
63.5M
    } else {
271
63.5M
        DCHECK(literal_count_ > 0);
272
63.5M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
63.5M
        DCHECK(result);
274
63.5M
        --literal_count_;
275
63.5M
        rewind_state_ = REWIND_LITERAL;
276
63.5M
    }
277
278
125M
    return true;
279
125M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
260
117M
bool RleDecoder<T>::Get(T* val) {
261
117M
    DCHECK(bit_reader_.is_initialized());
262
117M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
117M
    if (repeat_count_ > 0) [[likely]] {
267
61.3M
        *val = cast_set<T>(current_value_);
268
61.3M
        --repeat_count_;
269
61.3M
        rewind_state_ = REWIND_RUN;
270
61.3M
    } else {
271
56.0M
        DCHECK(literal_count_ > 0);
272
56.0M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
56.0M
        DCHECK(result);
274
56.0M
        --literal_count_;
275
56.0M
        rewind_state_ = REWIND_LITERAL;
276
56.0M
    }
277
278
117M
    return true;
279
117M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
260
7.81M
bool RleDecoder<T>::Get(T* val) {
261
7.81M
    DCHECK(bit_reader_.is_initialized());
262
7.81M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
7.81M
    if (repeat_count_ > 0) [[likely]] {
267
263k
        *val = cast_set<T>(current_value_);
268
263k
        --repeat_count_;
269
263k
        rewind_state_ = REWIND_RUN;
270
7.55M
    } else {
271
7.55M
        DCHECK(literal_count_ > 0);
272
7.55M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
7.55M
        DCHECK(result);
274
7.55M
        --literal_count_;
275
7.55M
        rewind_state_ = REWIND_LITERAL;
276
7.55M
    }
277
278
7.81M
    return true;
279
7.81M
}
280
281
template <typename T>
282
32.8k
void RleDecoder<T>::RewindOne() {
283
32.8k
    DCHECK(bit_reader_.is_initialized());
284
285
32.8k
    switch (rewind_state_) {
286
0
    case CANT_REWIND:
287
0
        throw Exception(Status::FatalError("Can't rewind more than once after each read!"));
288
0
        break;
289
14.5k
    case REWIND_RUN:
290
14.5k
        ++repeat_count_;
291
14.5k
        break;
292
18.3k
    case REWIND_LITERAL: {
293
18.3k
        bit_reader_.Rewind(bit_width_);
294
18.3k
        ++literal_count_;
295
18.3k
        break;
296
0
    }
297
32.8k
    }
298
299
32.8k
    rewind_state_ = CANT_REWIND;
300
32.8k
}
301
302
template <typename T>
303
6.01M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
6.01M
    DCHECK(bit_reader_.is_initialized());
305
6.01M
    DCHECK_GT(max_run, 0);
306
6.01M
    size_t ret = 0;
307
6.01M
    size_t rem = max_run;
308
7.31M
    while (ReadHeader()) {
309
7.31M
        if (repeat_count_ > 0) [[likely]] {
310
2.35M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
72.9k
                return ret;
312
72.9k
            }
313
2.28M
            *val = cast_set<T>(current_value_);
314
2.28M
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
1.63M
                repeat_count_ -= rem;
318
1.63M
                ret += rem;
319
1.63M
                return ret;
320
1.63M
            }
321
649k
            ret += repeat_count_;
322
649k
            rem -= repeat_count_;
323
649k
            repeat_count_ = 0;
324
4.95M
        } else {
325
4.95M
            DCHECK(literal_count_ > 0);
326
4.95M
            if (ret == 0) {
327
4.33M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
4.33M
                DCHECK(has_more);
329
4.33M
                literal_count_--;
330
4.33M
                ret++;
331
4.33M
                rem--;
332
4.33M
            }
333
334
12.7M
            while (literal_count_ > 0) {
335
12.0M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
12.0M
                DCHECK(result);
337
12.0M
                if (current_value_ != *val || rem == 0) {
338
4.31M
                    bit_reader_.Rewind(bit_width_);
339
4.31M
                    return ret;
340
4.31M
                }
341
7.76M
                ret++;
342
7.76M
                rem--;
343
7.76M
                literal_count_--;
344
7.76M
            }
345
4.95M
        }
346
7.31M
    }
347
18.4E
    return ret;
348
6.01M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
303
1.89M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
1.89M
    DCHECK(bit_reader_.is_initialized());
305
1.89M
    DCHECK_GT(max_run, 0);
306
1.89M
    size_t ret = 0;
307
1.89M
    size_t rem = max_run;
308
2.28M
    while (ReadHeader()) {
309
2.28M
        if (repeat_count_ > 0) [[likely]] {
310
713k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
15.3k
                return ret;
312
15.3k
            }
313
698k
            *val = cast_set<T>(current_value_);
314
698k
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
501k
                repeat_count_ -= rem;
318
501k
                ret += rem;
319
501k
                return ret;
320
501k
            }
321
196k
            ret += repeat_count_;
322
196k
            rem -= repeat_count_;
323
196k
            repeat_count_ = 0;
324
1.57M
        } else {
325
1.57M
            DCHECK(literal_count_ > 0);
326
1.57M
            if (ret == 0) {
327
1.37M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
1.37M
                DCHECK(has_more);
329
1.37M
                literal_count_--;
330
1.37M
                ret++;
331
1.37M
                rem--;
332
1.37M
            }
333
334
4.49M
            while (literal_count_ > 0) {
335
4.29M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
4.29M
                DCHECK(result);
337
4.29M
                if (current_value_ != *val || rem == 0) {
338
1.37M
                    bit_reader_.Rewind(bit_width_);
339
1.37M
                    return ret;
340
1.37M
                }
341
2.92M
                ret++;
342
2.92M
                rem--;
343
2.92M
                literal_count_--;
344
2.92M
            }
345
1.57M
        }
346
2.28M
    }
347
57
    return ret;
348
1.89M
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
303
4.12M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
4.12M
    DCHECK(bit_reader_.is_initialized());
305
4.12M
    DCHECK_GT(max_run, 0);
306
4.12M
    size_t ret = 0;
307
4.12M
    size_t rem = max_run;
308
5.02M
    while (ReadHeader()) {
309
5.02M
        if (repeat_count_ > 0) [[likely]] {
310
1.63M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
57.6k
                return ret;
312
57.6k
            }
313
1.58M
            *val = cast_set<T>(current_value_);
314
1.58M
            if (repeat_count_ >= rem) {
315
                // The next run is longer than the amount of remaining data
316
                // that the caller wants to read. Only consume it partially.
317
1.12M
                repeat_count_ -= rem;
318
1.12M
                ret += rem;
319
1.12M
                return ret;
320
1.12M
            }
321
453k
            ret += repeat_count_;
322
453k
            rem -= repeat_count_;
323
453k
            repeat_count_ = 0;
324
3.38M
        } else {
325
3.38M
            DCHECK(literal_count_ > 0);
326
3.38M
            if (ret == 0) {
327
2.95M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
2.95M
                DCHECK(has_more);
329
2.95M
                literal_count_--;
330
2.95M
                ret++;
331
2.95M
                rem--;
332
2.95M
            }
333
334
8.23M
            while (literal_count_ > 0) {
335
7.78M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
7.78M
                DCHECK(result);
337
7.78M
                if (current_value_ != *val || rem == 0) {
338
2.94M
                    bit_reader_.Rewind(bit_width_);
339
2.94M
                    return ret;
340
2.94M
                }
341
4.84M
                ret++;
342
4.84M
                rem--;
343
4.84M
                literal_count_--;
344
4.84M
            }
345
3.38M
        }
346
5.02M
    }
347
18.4E
    return ret;
348
4.12M
}
349
350
template <typename T>
351
130k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
130k
    size_t read_num = 0;
353
5.26M
    while (read_num < num_values) {
354
5.13M
        size_t read_this_time = num_values - read_num;
355
356
5.13M
        if (LIKELY(repeat_count_ > 0)) {
357
1.30M
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.30M
            std::fill(values, values + read_this_time, current_value_);
359
1.30M
            values += read_this_time;
360
1.30M
            repeat_count_ -= read_this_time;
361
1.30M
            read_num += read_this_time;
362
3.82M
        } else if (literal_count_ > 0) {
363
1.26M
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
20.7M
            for (int i = 0; i < read_this_time; ++i) {
365
19.4M
                bool result = bit_reader_.GetValue(bit_width_, values);
366
19.4M
                DCHECK(result);
367
19.4M
                values++;
368
19.4M
            }
369
1.26M
            literal_count_ -= read_this_time;
370
1.26M
            read_num += read_this_time;
371
2.56M
        } else {
372
2.56M
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.56M
        }
376
5.13M
    }
377
130k
    return read_num;
378
130k
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
351
130k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
130k
    size_t read_num = 0;
353
5.26M
    while (read_num < num_values) {
354
5.13M
        size_t read_this_time = num_values - read_num;
355
356
5.13M
        if (LIKELY(repeat_count_ > 0)) {
357
1.30M
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.30M
            std::fill(values, values + read_this_time, current_value_);
359
1.30M
            values += read_this_time;
360
1.30M
            repeat_count_ -= read_this_time;
361
1.30M
            read_num += read_this_time;
362
3.82M
        } else if (literal_count_ > 0) {
363
1.26M
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
20.7M
            for (int i = 0; i < read_this_time; ++i) {
365
19.4M
                bool result = bit_reader_.GetValue(bit_width_, values);
366
19.4M
                DCHECK(result);
367
19.4M
                values++;
368
19.4M
            }
369
1.26M
            literal_count_ -= read_this_time;
370
1.26M
            read_num += read_this_time;
371
2.56M
        } else {
372
2.56M
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.56M
        }
376
5.13M
    }
377
130k
    return read_num;
378
130k
}
_ZN5doris10RleDecoderIhE10get_valuesEPhm
Line
Count
Source
351
397
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
397
    size_t read_num = 0;
353
1.19k
    while (read_num < num_values) {
354
793
        size_t read_this_time = num_values - read_num;
355
356
793
        if (LIKELY(repeat_count_ > 0)) {
357
40
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
40
            std::fill(values, values + read_this_time, current_value_);
359
40
            values += read_this_time;
360
40
            repeat_count_ -= read_this_time;
361
40
            read_num += read_this_time;
362
753
        } else if (literal_count_ > 0) {
363
357
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
1.69k
            for (int i = 0; i < read_this_time; ++i) {
365
1.33k
                bool result = bit_reader_.GetValue(bit_width_, values);
366
1.33k
                DCHECK(result);
367
1.33k
                values++;
368
1.33k
            }
369
357
            literal_count_ -= read_this_time;
370
357
            read_num += read_this_time;
371
396
        } else {
372
396
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
396
        }
376
793
    }
377
397
    return read_num;
378
397
}
379
380
template <typename T>
381
size_t RleDecoder<T>::repeated_count() {
382
    if (repeat_count_ > 0) {
383
        return repeat_count_;
384
    }
385
    if (literal_count_ == 0) {
386
        ReadHeader();
387
    }
388
    return repeat_count_;
389
}
390
391
template <typename T>
392
T RleDecoder<T>::get_repeated_value(size_t count) {
393
    DCHECK_GE(repeat_count_, count);
394
    repeat_count_ -= count;
395
    return current_value_;
396
}
397
398
template <typename T>
399
1.39M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.39M
    DCHECK(bit_reader_.is_initialized());
401
402
1.39M
    size_t set_count = 0;
403
2.60M
    while (to_skip > 0) {
404
1.20M
        bool result = ReadHeader();
405
1.20M
        DCHECK(result);
406
407
1.20M
        if (repeat_count_ > 0) [[likely]] {
408
556k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
556k
            repeat_count_ -= nskip;
410
556k
            to_skip -= nskip;
411
556k
            if (current_value_ != 0) {
412
474k
                set_count += nskip;
413
474k
            }
414
651k
        } else {
415
651k
            DCHECK(literal_count_ > 0);
416
651k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
651k
            literal_count_ -= nskip;
418
651k
            to_skip -= nskip;
419
25.3M
            for (; nskip > 0; nskip--) {
420
24.6M
                T value = 0;
421
24.6M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
24.6M
                DCHECK(result1);
423
24.6M
                if (value != 0) {
424
15.3M
                    set_count++;
425
15.3M
                }
426
24.6M
            }
427
651k
        }
428
1.20M
    }
429
1.39M
    return set_count;
430
1.39M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
399
354k
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
354k
    DCHECK(bit_reader_.is_initialized());
401
402
354k
    size_t set_count = 0;
403
1.36M
    while (to_skip > 0) {
404
1.01M
        bool result = ReadHeader();
405
1.01M
        DCHECK(result);
406
407
1.01M
        if (repeat_count_ > 0) [[likely]] {
408
427k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
427k
            repeat_count_ -= nskip;
410
427k
            to_skip -= nskip;
411
427k
            if (current_value_ != 0) {
412
414k
                set_count += nskip;
413
414k
            }
414
585k
        } else {
415
585k
            DCHECK(literal_count_ > 0);
416
585k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
585k
            literal_count_ -= nskip;
418
585k
            to_skip -= nskip;
419
10.3M
            for (; nskip > 0; nskip--) {
420
9.80M
                T value = 0;
421
9.80M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
9.80M
                DCHECK(result1);
423
9.80M
                if (value != 0) {
424
7.83M
                    set_count++;
425
7.83M
                }
426
9.80M
            }
427
585k
        }
428
1.01M
    }
429
354k
    return set_count;
430
354k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
399
1.04M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.04M
    DCHECK(bit_reader_.is_initialized());
401
402
1.04M
    size_t set_count = 0;
403
1.23M
    while (to_skip > 0) {
404
195k
        bool result = ReadHeader();
405
195k
        DCHECK(result);
406
407
195k
        if (repeat_count_ > 0) [[likely]] {
408
129k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
129k
            repeat_count_ -= nskip;
410
129k
            to_skip -= nskip;
411
129k
            if (current_value_ != 0) {
412
59.8k
                set_count += nskip;
413
59.8k
            }
414
129k
        } else {
415
66.2k
            DCHECK(literal_count_ > 0);
416
66.2k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
66.2k
            literal_count_ -= nskip;
418
66.2k
            to_skip -= nskip;
419
14.9M
            for (; nskip > 0; nskip--) {
420
14.8M
                T value = 0;
421
14.8M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
14.8M
                DCHECK(result1);
423
14.8M
                if (value != 0) {
424
7.54M
                    set_count++;
425
7.54M
                }
426
14.8M
            }
427
66.2k
        }
428
195k
    }
429
1.04M
    return set_count;
430
1.04M
}
431
432
// This function buffers input values 8 at a time.  After seeing all 8 values,
433
// it decides whether they should be encoded as a literal or repeated run.
434
template <typename T>
435
8.88M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
8.88M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
8.88M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
1.01M
        repeat_count_ += run_length;
442
1.01M
        return;
443
1.01M
    }
444
445
    // Handle run_length > 1 more efficiently
446
17.8M
    while (run_length > 0) {
447
10.7M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
5.10M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
21.2M
            for (size_t i = 0; i < to_buffer; ++i) {
451
16.1M
                buffered_values_[num_buffered_values_++] = value;
452
16.1M
                ++repeat_count_;
453
16.1M
            }
454
5.10M
            run_length -= to_buffer;
455
5.10M
            if (num_buffered_values_ == 8) {
456
2.09M
                DCHECK_EQ(literal_count_ % 8, 0);
457
2.09M
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
2.09M
                if (repeat_count_ >= 8 && run_length > 0) {
461
795k
                    repeat_count_ += run_length;
462
795k
                    return;
463
795k
                }
464
2.09M
            }
465
5.65M
        } else {
466
            // Value changed
467
5.65M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
706k
                DCHECK_EQ(literal_count_, 0);
471
706k
                FlushRepeatedRun();
472
706k
            }
473
5.65M
            repeat_count_ = 1;
474
5.65M
            current_value_ = value;
475
476
5.65M
            buffered_values_[num_buffered_values_++] = value;
477
5.65M
            --run_length;
478
5.65M
            if (num_buffered_values_ == 8) {
479
529k
                DCHECK_EQ(literal_count_ % 8, 0);
480
529k
                FlushBufferedValues(false);
481
529k
            }
482
5.65M
        }
483
10.7M
    }
484
7.86M
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
435
3.96M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
3.96M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
3.96M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
366k
        repeat_count_ += run_length;
442
366k
        return;
443
366k
    }
444
445
    // Handle run_length > 1 more efficiently
446
7.19M
    while (run_length > 0) {
447
3.59M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
1.79M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
3.59M
            for (size_t i = 0; i < to_buffer; ++i) {
451
1.79M
                buffered_values_[num_buffered_values_++] = value;
452
1.79M
                ++repeat_count_;
453
1.79M
            }
454
1.79M
            run_length -= to_buffer;
455
1.79M
            if (num_buffered_values_ == 8) {
456
225k
                DCHECK_EQ(literal_count_ % 8, 0);
457
225k
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
225k
                if (repeat_count_ >= 8 && run_length > 0) {
461
3
                    repeat_count_ += run_length;
462
3
                    return;
463
3
                }
464
225k
            }
465
1.80M
        } else {
466
            // Value changed
467
1.80M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
5.09k
                DCHECK_EQ(literal_count_, 0);
471
5.09k
                FlushRepeatedRun();
472
5.09k
            }
473
1.80M
            repeat_count_ = 1;
474
1.80M
            current_value_ = value;
475
476
1.80M
            buffered_values_[num_buffered_values_++] = value;
477
1.80M
            --run_length;
478
1.80M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
221k
                FlushBufferedValues(false);
481
221k
            }
482
1.80M
        }
483
3.59M
    }
484
3.59M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
435
4.91M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
4.91M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
437
438
    // Fast path: if this is a continuation of the current repeated run and
439
    // we've already buffered enough values, just increment repeat_count_
440
4.91M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
652k
        repeat_count_ += run_length;
442
652k
        return;
443
652k
    }
444
445
    // Handle run_length > 1 more efficiently
446
10.6M
    while (run_length > 0) {
447
7.16M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
3.30M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
17.6M
            for (size_t i = 0; i < to_buffer; ++i) {
451
14.3M
                buffered_values_[num_buffered_values_++] = value;
452
14.3M
                ++repeat_count_;
453
14.3M
            }
454
3.30M
            run_length -= to_buffer;
455
3.30M
            if (num_buffered_values_ == 8) {
456
1.86M
                DCHECK_EQ(literal_count_ % 8, 0);
457
1.86M
                FlushBufferedValues(false);
458
                // After flushing, if we still have a repeated run and more values,
459
                // we can add them directly to repeat_count_
460
1.86M
                if (repeat_count_ >= 8 && run_length > 0) {
461
795k
                    repeat_count_ += run_length;
462
795k
                    return;
463
795k
                }
464
1.86M
            }
465
3.85M
        } else {
466
            // Value changed
467
3.85M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
701k
                DCHECK_EQ(literal_count_, 0);
471
701k
                FlushRepeatedRun();
472
701k
            }
473
3.85M
            repeat_count_ = 1;
474
3.85M
            current_value_ = value;
475
476
3.85M
            buffered_values_[num_buffered_values_++] = value;
477
3.85M
            --run_length;
478
3.85M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
307k
                FlushBufferedValues(false);
481
307k
            }
482
3.85M
        }
483
7.16M
    }
484
4.26M
}
485
486
template <typename T>
487
2.48M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
2.48M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
728k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
728k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
728k
    }
493
494
    // Write all the buffered values as bit packed literals
495
16.6M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
14.1M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
14.1M
    }
498
2.48M
    num_buffered_values_ = 0;
499
500
2.48M
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
729k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
729k
        int32_t indicator_value = (num_groups << 1) | 1;
507
729k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
729k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
729k
                cast_set<uint8_t>(indicator_value);
510
729k
        literal_indicator_byte_idx_ = -1;
511
729k
        literal_count_ = 0;
512
729k
    }
513
2.48M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
487
447k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
447k
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
12.0k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
12.0k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
12.0k
    }
493
494
    // Write all the buffered values as bit packed literals
495
3.97M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
3.53M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
3.53M
    }
498
447k
    num_buffered_values_ = 0;
499
500
447k
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
12.0k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
12.0k
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
12.0k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
12.0k
                cast_set<uint8_t>(indicator_value);
510
12.0k
        literal_indicator_byte_idx_ = -1;
511
12.0k
        literal_count_ = 0;
512
12.0k
    }
513
447k
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
487
2.03M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
2.03M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
716k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
716k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
716k
    }
493
494
    // Write all the buffered values as bit packed literals
495
12.6M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
10.6M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
10.6M
    }
498
2.03M
    num_buffered_values_ = 0;
499
500
2.03M
    if (update_indicator_byte) {
501
        // At this point we need to write the indicator byte for the literal run.
502
        // We only reserve one byte, to allow for streaming writes of literal values.
503
        // The logic makes sure we flush literal runs often enough to not overrun
504
        // the 1 byte.
505
716k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
716k
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
716k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
716k
                cast_set<uint8_t>(indicator_value);
510
716k
        literal_indicator_byte_idx_ = -1;
511
716k
        literal_count_ = 0;
512
716k
    }
513
2.03M
}
514
515
template <typename T>
516
828k
void RleEncoder<T>::FlushRepeatedRun() {
517
828k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
828k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
828k
    bit_writer_.PutVlqInt(indicator_value);
521
828k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
828k
    num_buffered_values_ = 0;
523
828k
    repeat_count_ = 0;
524
828k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
516
14.1k
void RleEncoder<T>::FlushRepeatedRun() {
517
14.1k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
14.1k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
14.1k
    bit_writer_.PutVlqInt(indicator_value);
521
14.1k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
14.1k
    num_buffered_values_ = 0;
523
14.1k
    repeat_count_ = 0;
524
14.1k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
516
814k
void RleEncoder<T>::FlushRepeatedRun() {
517
814k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
814k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
814k
    bit_writer_.PutVlqInt(indicator_value);
521
814k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
814k
    num_buffered_values_ = 0;
523
814k
    repeat_count_ = 0;
524
814k
}
525
526
// Flush the values that have been buffered.  At this point we decide whether
527
// we need to switch between the run types or continue the current one.
528
template <typename T>
529
2.62M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
2.62M
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
857k
        num_buffered_values_ = 0;
534
857k
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
675k
            DCHECK_EQ(literal_count_ % 8, 0);
538
675k
            DCHECK_EQ(repeat_count_, 8);
539
675k
            FlushLiteralRun(true);
540
675k
        }
541
857k
        DCHECK_EQ(literal_count_, 0);
542
857k
        return;
543
857k
    }
544
545
1.76M
    literal_count_ += num_buffered_values_;
546
1.76M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
1.76M
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
7.56k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
7.56k
        FlushLiteralRun(true);
552
1.75M
    } else {
553
1.75M
        FlushLiteralRun(done);
554
1.75M
    }
555
1.76M
    repeat_count_ = 0;
556
1.76M
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
529
447k
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
447k
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
6.56k
        num_buffered_values_ = 0;
534
6.56k
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
3.86k
            DCHECK_EQ(literal_count_ % 8, 0);
538
3.86k
            DCHECK_EQ(repeat_count_, 8);
539
3.86k
            FlushLiteralRun(true);
540
3.86k
        }
541
6.56k
        DCHECK_EQ(literal_count_, 0);
542
6.56k
        return;
543
6.56k
    }
544
545
440k
    literal_count_ += num_buffered_values_;
546
440k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
440k
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
5.25k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
5.25k
        FlushLiteralRun(true);
552
435k
    } else {
553
435k
        FlushLiteralRun(done);
554
435k
    }
555
440k
    repeat_count_ = 0;
556
440k
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
529
2.17M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
2.17M
    if (repeat_count_ >= 8) {
531
        // Clear the buffered values.  They are part of the repeated run now and we
532
        // don't want to flush them out as literals.
533
850k
        num_buffered_values_ = 0;
534
850k
        if (literal_count_ != 0) {
535
            // There was a current literal run.  All the values in it have been flushed
536
            // but we still need to update the indicator byte.
537
671k
            DCHECK_EQ(literal_count_ % 8, 0);
538
671k
            DCHECK_EQ(repeat_count_, 8);
539
671k
            FlushLiteralRun(true);
540
671k
        }
541
850k
        DCHECK_EQ(literal_count_, 0);
542
850k
        return;
543
850k
    }
544
545
1.32M
    literal_count_ += num_buffered_values_;
546
1.32M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
1.32M
    if (num_groups + 1 >= (1 << 6)) {
548
        // We need to start a new literal run because the indicator byte we've reserved
549
        // cannot store more values.
550
2.31k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
2.31k
        FlushLiteralRun(true);
552
1.32M
    } else {
553
1.32M
        FlushLiteralRun(done);
554
1.32M
    }
555
1.32M
    repeat_count_ = 0;
556
1.32M
}
557
558
template <typename T>
559
26.0k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
560
130k
    for (int i = 0; i < num_bytes; ++i) {
561
104k
        bit_writer_.PutValue(val, 8);
562
104k
    }
563
26.0k
}
564
565
template <typename T>
566
168k
int RleEncoder<T>::Flush() {
567
168k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
168k
        bool all_repeat = literal_count_ == 0 &&
569
168k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
168k
        if (repeat_count_ > 0 && all_repeat) {
572
122k
            FlushRepeatedRun();
573
122k
        } else {
574
45.7k
            literal_count_ += num_buffered_values_;
575
45.7k
            FlushLiteralRun(true);
576
45.7k
            repeat_count_ = 0;
577
45.7k
        }
578
168k
    }
579
168k
    bit_writer_.Flush();
580
168k
    DCHECK_EQ(num_buffered_values_, 0);
581
168k
    DCHECK_EQ(literal_count_, 0);
582
168k
    DCHECK_EQ(repeat_count_, 0);
583
168k
    return bit_writer_.bytes_written();
584
168k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
566
12.5k
int RleEncoder<T>::Flush() {
567
12.5k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
11.9k
        bool all_repeat = literal_count_ == 0 &&
569
11.9k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
11.9k
        if (repeat_count_ > 0 && all_repeat) {
572
9.02k
            FlushRepeatedRun();
573
9.02k
        } else {
574
2.88k
            literal_count_ += num_buffered_values_;
575
2.88k
            FlushLiteralRun(true);
576
2.88k
            repeat_count_ = 0;
577
2.88k
        }
578
11.9k
    }
579
12.5k
    bit_writer_.Flush();
580
12.5k
    DCHECK_EQ(num_buffered_values_, 0);
581
12.5k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
12.5k
    return bit_writer_.bytes_written();
584
12.5k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
566
156k
int RleEncoder<T>::Flush() {
567
156k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
156k
        bool all_repeat = literal_count_ == 0 &&
569
156k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
570
        // There is something pending, figure out if it's a repeated or literal run
571
156k
        if (repeat_count_ > 0 && all_repeat) {
572
113k
            FlushRepeatedRun();
573
113k
        } else {
574
42.8k
            literal_count_ += num_buffered_values_;
575
42.8k
            FlushLiteralRun(true);
576
42.8k
            repeat_count_ = 0;
577
42.8k
        }
578
156k
    }
579
156k
    bit_writer_.Flush();
580
156k
    DCHECK_EQ(num_buffered_values_, 0);
581
156k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
156k
    return bit_writer_.bytes_written();
584
156k
}
585
586
template <typename T>
587
1.07M
void RleEncoder<T>::Clear() {
588
1.07M
    current_value_ = 0;
589
1.07M
    repeat_count_ = 0;
590
1.07M
    num_buffered_values_ = 0;
591
1.07M
    literal_count_ = 0;
592
1.07M
    literal_indicator_byte_idx_ = -1;
593
1.07M
    bit_writer_.Clear();
594
1.07M
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
587
39.6k
void RleEncoder<T>::Clear() {
588
39.6k
    current_value_ = 0;
589
39.6k
    repeat_count_ = 0;
590
39.6k
    num_buffered_values_ = 0;
591
39.6k
    literal_count_ = 0;
592
39.6k
    literal_indicator_byte_idx_ = -1;
593
39.6k
    bit_writer_.Clear();
594
39.6k
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
587
1.03M
void RleEncoder<T>::Clear() {
588
1.03M
    current_value_ = 0;
589
1.03M
    repeat_count_ = 0;
590
1.03M
    num_buffered_values_ = 0;
591
1.03M
    literal_count_ = 0;
592
1.03M
    literal_indicator_byte_idx_ = -1;
593
1.03M
    bit_writer_.Clear();
594
1.03M
}
595
596
// Copy from https://github.com/apache/impala/blob/master/be/src/util/rle-encoding.h
597
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
598
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
599
// (literal encoding).
600
//
601
// For both types of runs, there is a byte-aligned indicator which encodes the length
602
// of the run and the type of the run.
603
//
604
// This encoding has the benefit that when there aren't any long enough runs, values
605
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
606
// the run length are byte aligned. This allows for very efficient decoding
607
// implementations.
608
// The encoding is:
609
//    encoded-block := run*
610
//    run := literal-run | repeated-run
611
//    literal-run := literal-indicator < literal bytes >
612
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
613
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
614
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
615
//
616
// Each run is preceded by a varint. The varint's least significant bit is
617
// used to indicate whether the run is a literal run or a repeated run. The rest
618
// of the varint is used to determine the length of the run (eg how many times the
619
// value repeats).
620
//
621
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
622
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
623
// on a byte boundary without padding.
624
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
625
// the actual number of encoded ints. (This means that the total number of encoded values
626
// can not be determined from the encoded data, since the number of values in the last
627
// group may not be a multiple of 8). For the last group of literal runs, we pad
628
// the group to 8 with zeros. This allows for 8 at a time decoding on the read side
629
// without the need for additional checks.
630
//
631
// There is a break-even point when it is more storage efficient to do run length
632
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
633
// for both the repeated encoding or the literal encoding.  This value can always
634
// be computed based on the bit-width.
635
// TODO: For 1 bit-width values it can be optimal to use 16 or 24 values, but more
636
// investigation is needed to do this efficiently, see the reverted IMPALA-6658.
637
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
638
//
639
// Examples with bit-width 1 (eg encoding booleans):
640
// ----------------------------------------
641
// 100 1s followed by 100 0s:
642
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
643
//  - (total 4 bytes)
644
//
645
// alternating 1s and 0s (200 total):
646
// 200 ints = 25 groups of 8
647
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
648
// (total 26 bytes, 1 byte overhead)
649
650
// RLE decoder with a batch-oriented interface that enables fast decoding.
651
// Users of this class must first initialize the class to point to a buffer of
652
// RLE-encoded data, passed into the constructor or Reset(). The provided
653
// bit_width must be at most min(sizeof(T) * 8, BatchedBitReader::MAX_BITWIDTH).
654
// Then they can decode data by checking NextNumRepeats()/NextNumLiterals() to
655
// see if the next run is a repeated or literal run, then calling
656
// GetRepeatedValue() or GetLiteralValues() respectively to read the values.
657
//
658
// End-of-input is signalled by NextNumRepeats() == NextNumLiterals() == 0.
659
// Other decoding errors are signalled by functions returning false. If an
660
// error is encountered then it is not valid to read any more data until
661
// Reset() is called.
662
663
//bit-packed-run-len and rle-run-len must be in the range [1, 2^31 - 1].
664
// This means that a Parquet implementation can always store the run length in a signed 32-bit integer.
665
template <typename T>
666
class RleBatchDecoder {
667
public:
668
347k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
669
347k
        Reset(buffer, buffer_len, bit_width);
670
347k
    }
671
672
    RleBatchDecoder() = default;
673
674
    // Reset the decoder to read from a new buffer.
675
    void Reset(uint8_t* buffer, int buffer_len, int bit_width);
676
677
    // Return the size of the current repeated run. Returns zero if the current run is
678
    // a literal run or if no more runs can be read from the input.
679
    int32_t NextNumRepeats();
680
681
    // Get the value of the current repeated run and consume the given number of repeats.
682
    // Only valid to call when NextNumRepeats() > 0. The given number of repeats cannot
683
    // be greater than the remaining number of repeats in the run. 'num_repeats_to_consume'
684
    // can be set to 0 to peek at the value without consuming repeats.
685
    T GetRepeatedValue(int32_t num_repeats_to_consume);
686
687
    // Return the size of the current literal run. Returns zero if the current run is
688
    // a repeated run or if no more runs can be read from the input.
689
    int32_t NextNumLiterals();
690
691
    // Consume 'num_literals_to_consume' literals from the current literal run,
692
    // copying the values to 'values'. 'num_literals_to_consume' must be <=
693
    // NextNumLiterals(). Returns true if the requested number of literals were
694
    // successfully read or false if an error was encountered, e.g. the input was
695
    // truncated.
696
    bool GetLiteralValues(int32_t num_literals_to_consume, T* values) WARN_UNUSED_RESULT;
697
698
    // Consume 'num_values_to_consume' values and copy them to 'values'.
699
    // Returns the number of consumed values or 0 if an error occurred.
700
    uint32_t GetBatch(T* values, uint32_t batch_num);
701
702
private:
703
    // Called when both 'literal_count_' and 'repeat_count_' have been exhausted.
704
    // Sets either 'literal_count_' or 'repeat_count_' to the size of the next literal
705
    // or repeated run, or leaves both at 0 if no more values can be read (either because
706
    // the end of the input was reached or an error was encountered decoding).
707
    void NextCounts();
708
709
    /// Fill the literal buffer. Invalid to call if there are already buffered literals.
710
    /// Return false if the input was truncated. This does not advance 'literal_count_'.
711
    bool FillLiteralBuffer() WARN_UNUSED_RESULT;
712
713
4.04M
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
714
715
    /// Output buffered literals, advancing 'literal_buffer_pos_' and decrementing
716
    /// 'literal_count_'. Returns the number of literals outputted.
717
    int32_t OutputBufferedLiterals(int32_t max_to_output, T* values);
718
719
    BatchedBitReader bit_reader_;
720
721
    // Number of bits needed to encode the value. Must be between 0 and 64 after
722
    // the decoder is initialized with a buffer. -1 indicates the decoder was not
723
    // initialized.
724
    int bit_width_ = -1;
725
726
    // If a repeated run, the number of repeats remaining in the current run to be read.
727
    // If the current run is a literal run, this is 0.
728
    int32_t repeat_count_ = 0;
729
730
    // If a literal run, the number of literals remaining in the current run to be read.
731
    // If the current run is a repeated run, this is 0.
732
    int32_t literal_count_ = 0;
733
734
    // If a repeated run, the current repeated value.
735
    T repeated_value_;
736
737
    // Size of buffer for literal values. Large enough to decode a full batch of 32
738
    // literals. The buffer is needed to allow clients to read in batches that are not
739
    // multiples of 32.
740
    static constexpr int LITERAL_BUFFER_LEN = 32;
741
742
    // Buffer containing 'num_buffered_literals_' values. 'literal_buffer_pos_' is the
743
    // position of the next literal to be read from the buffer.
744
    T literal_buffer_[LITERAL_BUFFER_LEN];
745
    int num_buffered_literals_ = 0;
746
    int literal_buffer_pos_ = 0;
747
};
748
749
template <typename T>
750
3.73M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
751
3.73M
    int32_t num_to_output =
752
3.73M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
753
3.73M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
754
3.73M
    literal_buffer_pos_ += num_to_output;
755
3.73M
    literal_count_ -= num_to_output;
756
3.73M
    return num_to_output;
757
3.73M
}
758
759
template <typename T>
760
347k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
761
347k
    bit_reader_.Reset(buffer, buffer_len);
762
347k
    bit_width_ = bit_width;
763
347k
    repeat_count_ = 0;
764
347k
    literal_count_ = 0;
765
347k
    num_buffered_literals_ = 0;
766
347k
    literal_buffer_pos_ = 0;
767
347k
}
768
769
template <typename T>
770
7.41M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
771
7.41M
    if (repeat_count_ > 0) return repeat_count_;
772
7.40M
    if (literal_count_ == 0) NextCounts();
773
7.40M
    return repeat_count_;
774
7.41M
}
775
776
template <typename T>
777
7.34M
void RleBatchDecoder<T>::NextCounts() {
778
    // Read the next run's indicator int, it could be a literal or repeated run.
779
    // The int is encoded as a ULEB128-encoded value.
780
7.34M
    uint32_t indicator_value = 0;
781
7.34M
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
782
0
        return;
783
0
    }
784
785
    // lsb indicates if it is a literal run or repeated run
786
7.34M
    bool is_literal = indicator_value & 1;
787
788
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
789
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
790
7.34M
    uint32_t run_len = indicator_value >> 1;
791
7.34M
    if (is_literal) {
792
        // Use int64_t to avoid overflowing multiplication.
793
3.98M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
794
3.98M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
795
3.98M
        literal_count_ = cast_set<int32_t>(literal_count);
796
3.98M
    } else {
797
3.35M
        if (UNLIKELY(run_len == 0)) return;
798
3.35M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
799
3.35M
        if (UNLIKELY(!result)) return;
800
3.35M
        repeat_count_ = run_len;
801
3.35M
    }
802
7.34M
}
803
804
template <typename T>
805
3.36M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
806
3.36M
    repeat_count_ -= num_repeats_to_consume;
807
3.36M
    return repeated_value_;
808
3.36M
}
809
810
template <typename T>
811
4.04M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
812
4.04M
    if (literal_count_ > 0) return literal_count_;
813
18.4E
    if (repeat_count_ == 0) NextCounts();
814
18.4E
    return literal_count_;
815
4.04M
}
816
817
template <typename T>
818
4.04M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
819
4.04M
    int32_t num_consumed = 0;
820
    // Copy any buffered literals left over from previous calls.
821
4.04M
    if (HaveBufferedLiterals()) {
822
47.2k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
823
47.2k
    }
824
825
4.04M
    int32_t num_remaining = num_literals_to_consume - num_consumed;
826
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
827
    // Need to round to a batch of 32 if the caller is consuming only part of the current
828
    // run avoid ending on a non-byte boundary.
829
4.04M
    int32_t num_to_bypass =
830
4.04M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
831
4.04M
    if (num_to_bypass > 0) {
832
2.50M
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
833
        // If we couldn't read the expected number, that means the input was truncated.
834
2.50M
        if (num_read < num_to_bypass) return false;
835
2.50M
        literal_count_ -= num_to_bypass;
836
2.50M
        num_consumed += num_to_bypass;
837
2.50M
        num_remaining = num_literals_to_consume - num_consumed;
838
2.50M
    }
839
840
4.04M
    if (num_remaining > 0) {
841
        // We weren't able to copy all the literals requested directly from the input.
842
        // Buffer literals and copy over the requested number.
843
3.68M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
844
3.68M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
845
3.68M
    }
846
4.04M
    return true;
847
4.04M
}
848
849
template <typename T>
850
3.68M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
851
3.68M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
852
3.68M
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
853
    // If we couldn't read the expected number, that means the input was truncated.
854
3.68M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
855
3.68M
    literal_buffer_pos_ = 0;
856
3.68M
    return true;
857
3.68M
}
858
859
template <typename T>
860
430k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
861
430k
    uint32_t num_consumed = 0;
862
7.84M
    while (num_consumed < batch_num) {
863
        // Add RLE encoded values by repeating the current value this number of times.
864
7.41M
        uint32_t num_repeats = NextNumRepeats();
865
7.41M
        if (num_repeats > 0) {
866
3.36M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
867
3.36M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
868
61.3M
            for (int i = 0; i < num_repeats_to_set; ++i) {
869
57.9M
                values[num_consumed + i] = repeated_value;
870
57.9M
            }
871
3.36M
            num_consumed += num_repeats_to_set;
872
3.36M
            continue;
873
3.36M
        }
874
875
        // Add remaining literal values, if any.
876
4.04M
        uint32_t num_literals = NextNumLiterals();
877
4.04M
        if (num_literals == 0) {
878
0
            break;
879
0
        }
880
4.04M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
881
4.04M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
882
0
            return 0;
883
0
        }
884
4.04M
        num_consumed += num_literals_to_set;
885
4.04M
    }
886
430k
    return num_consumed;
887
430k
}
888
#include "common/compile_check_end.h"
889
} // namespace doris