Coverage Report

Created: 2026-03-13 09:58

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.48M
            : bit_reader_(buffer, buffer_len),
90
1.48M
              bit_width_(bit_width),
91
1.48M
              current_value_(0),
92
1.48M
              repeat_count_(0),
93
1.48M
              literal_count_(0),
94
1.48M
              rewind_state_(CANT_REWIND) {
95
1.48M
        DCHECK_GE(bit_width_, 1);
96
1.48M
        DCHECK_LE(bit_width_, 64);
97
1.48M
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
89
500k
            : bit_reader_(buffer, buffer_len),
90
500k
              bit_width_(bit_width),
91
500k
              current_value_(0),
92
500k
              repeat_count_(0),
93
500k
              literal_count_(0),
94
500k
              rewind_state_(CANT_REWIND) {
95
500k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
500k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
89
25.2k
            : bit_reader_(buffer, buffer_len),
90
25.2k
              bit_width_(bit_width),
91
25.2k
              current_value_(0),
92
25.2k
              repeat_count_(0),
93
25.2k
              literal_count_(0),
94
25.2k
              rewind_state_(CANT_REWIND) {
95
25.2k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
25.2k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
89
963k
            : bit_reader_(buffer, buffer_len),
90
963k
              bit_width_(bit_width),
91
963k
              current_value_(0),
92
963k
              repeat_count_(0),
93
963k
              literal_count_(0),
94
963k
              rewind_state_(CANT_REWIND) {
95
963k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
963k
    }
98
99
38.2M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
99
37.8M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
99
22.8k
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
99
361k
    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
603k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
603k
        DCHECK_GE(bit_width_, 1);
156
603k
        DCHECK_LE(bit_width_, 64);
157
603k
        Clear();
158
603k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
154
14.1k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
14.1k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
14.1k
        Clear();
158
14.1k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
154
589k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
589k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
589k
        Clear();
158
589k
    }
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
91.0k
    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
291M
bool RleDecoder<T>::ReadHeader() {
233
291M
    DCHECK(bit_reader_.is_initialized());
234
291M
    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
11.5M
        uint32_t indicator_value = 0;
238
11.5M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
11.5M
        if (!result) [[unlikely]] {
240
11.9k
            return false;
241
11.9k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
11.5M
        bool is_literal = indicator_value & 1;
245
11.5M
        if (is_literal) {
246
5.64M
            literal_count_ = (indicator_value >> 1) * 8;
247
5.64M
            DCHECK_GT(literal_count_, 0);
248
5.89M
        } else {
249
5.89M
            repeat_count_ = indicator_value >> 1;
250
5.89M
            DCHECK_GT(repeat_count_, 0);
251
5.89M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
5.89M
                                                     reinterpret_cast<T*>(&current_value_));
253
5.89M
            DCHECK(result1);
254
5.89M
        }
255
11.5M
    }
256
291M
    return true;
257
291M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
232
278M
bool RleDecoder<T>::ReadHeader() {
233
278M
    DCHECK(bit_reader_.is_initialized());
234
278M
    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
9.76M
        uint32_t indicator_value = 0;
238
9.76M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
9.76M
        if (!result) [[unlikely]] {
240
46
            return false;
241
46
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
9.76M
        bool is_literal = indicator_value & 1;
245
9.76M
        if (is_literal) {
246
4.68M
            literal_count_ = (indicator_value >> 1) * 8;
247
4.68M
            DCHECK_GT(literal_count_, 0);
248
5.07M
        } else {
249
5.07M
            repeat_count_ = indicator_value >> 1;
250
5.07M
            DCHECK_GT(repeat_count_, 0);
251
5.07M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
5.07M
                                                     reinterpret_cast<T*>(&current_value_));
253
5.07M
            DCHECK(result1);
254
5.07M
        }
255
9.76M
    }
256
278M
    return true;
257
278M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
232
5.18M
bool RleDecoder<T>::ReadHeader() {
233
5.18M
    DCHECK(bit_reader_.is_initialized());
234
5.18M
    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
1.64M
        uint32_t indicator_value = 0;
238
1.64M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
1.64M
        if (!result) [[unlikely]] {
240
11.8k
            return false;
241
11.8k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
1.63M
        bool is_literal = indicator_value & 1;
245
1.63M
        if (is_literal) {
246
879k
            literal_count_ = (indicator_value >> 1) * 8;
247
879k
            DCHECK_GT(literal_count_, 0);
248
879k
        } else {
249
755k
            repeat_count_ = indicator_value >> 1;
250
755k
            DCHECK_GT(repeat_count_, 0);
251
755k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
755k
                                                     reinterpret_cast<T*>(&current_value_));
253
755k
            DCHECK(result1);
254
755k
        }
255
1.63M
    }
256
5.17M
    return true;
257
5.18M
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
232
8.07M
bool RleDecoder<T>::ReadHeader() {
233
8.07M
    DCHECK(bit_reader_.is_initialized());
234
8.07M
    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
139k
        uint32_t indicator_value = 0;
238
139k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
139k
        if (!result) [[unlikely]] {
240
0
            return false;
241
0
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
139k
        bool is_literal = indicator_value & 1;
245
139k
        if (is_literal) {
246
80.6k
            literal_count_ = (indicator_value >> 1) * 8;
247
80.6k
            DCHECK_GT(literal_count_, 0);
248
80.6k
        } else {
249
58.6k
            repeat_count_ = indicator_value >> 1;
250
58.6k
            DCHECK_GT(repeat_count_, 0);
251
58.6k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
58.6k
                                                     reinterpret_cast<T*>(&current_value_));
253
58.6k
            DCHECK(result1);
254
58.6k
        }
255
139k
    }
256
8.07M
    return true;
257
8.07M
}
258
259
template <typename T>
260
280M
bool RleDecoder<T>::Get(T* val) {
261
280M
    DCHECK(bit_reader_.is_initialized());
262
280M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
280M
    if (repeat_count_ > 0) [[likely]] {
267
203M
        *val = cast_set<T>(current_value_);
268
203M
        --repeat_count_;
269
203M
        rewind_state_ = REWIND_RUN;
270
203M
    } else {
271
77.0M
        DCHECK(literal_count_ > 0);
272
77.0M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
77.0M
        DCHECK(result);
274
77.0M
        --literal_count_;
275
77.0M
        rewind_state_ = REWIND_LITERAL;
276
77.0M
    }
277
278
280M
    return true;
279
280M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
260
272M
bool RleDecoder<T>::Get(T* val) {
261
272M
    DCHECK(bit_reader_.is_initialized());
262
272M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
272M
    if (repeat_count_ > 0) [[likely]] {
267
202M
        *val = cast_set<T>(current_value_);
268
202M
        --repeat_count_;
269
202M
        rewind_state_ = REWIND_RUN;
270
202M
    } else {
271
69.4M
        DCHECK(literal_count_ > 0);
272
69.4M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
69.4M
        DCHECK(result);
274
69.4M
        --literal_count_;
275
69.4M
        rewind_state_ = REWIND_LITERAL;
276
69.4M
    }
277
278
272M
    return true;
279
272M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
260
7.91M
bool RleDecoder<T>::Get(T* val) {
261
7.91M
    DCHECK(bit_reader_.is_initialized());
262
7.91M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
7.91M
    if (repeat_count_ > 0) [[likely]] {
267
284k
        *val = cast_set<T>(current_value_);
268
284k
        --repeat_count_;
269
284k
        rewind_state_ = REWIND_RUN;
270
7.63M
    } else {
271
7.63M
        DCHECK(literal_count_ > 0);
272
7.63M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
7.63M
        DCHECK(result);
274
7.63M
        --literal_count_;
275
7.63M
        rewind_state_ = REWIND_LITERAL;
276
7.63M
    }
277
278
7.91M
    return true;
279
7.91M
}
280
281
template <typename T>
282
33.3k
void RleDecoder<T>::RewindOne() {
283
33.3k
    DCHECK(bit_reader_.is_initialized());
284
285
33.3k
    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.6k
    case REWIND_RUN:
290
14.6k
        ++repeat_count_;
291
14.6k
        break;
292
18.6k
    case REWIND_LITERAL: {
293
18.6k
        bit_reader_.Rewind(bit_width_);
294
18.6k
        ++literal_count_;
295
18.6k
        break;
296
0
    }
297
33.3k
    }
298
299
33.3k
    rewind_state_ = CANT_REWIND;
300
33.3k
}
301
302
template <typename T>
303
5.78M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
5.78M
    DCHECK(bit_reader_.is_initialized());
305
5.78M
    DCHECK_GT(max_run, 0);
306
5.78M
    size_t ret = 0;
307
5.78M
    size_t rem = max_run;
308
7.07M
    while (ReadHeader()) {
309
7.07M
        if (repeat_count_ > 0) [[likely]] {
310
2.16M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
72.2k
                return ret;
312
72.2k
            }
313
2.09M
            *val = cast_set<T>(current_value_);
314
2.09M
            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.44M
                repeat_count_ -= rem;
318
1.44M
                ret += rem;
319
1.44M
                return ret;
320
1.44M
            }
321
649k
            ret += repeat_count_;
322
649k
            rem -= repeat_count_;
323
649k
            repeat_count_ = 0;
324
4.90M
        } else {
325
4.90M
            DCHECK(literal_count_ > 0);
326
4.90M
            if (ret == 0) {
327
4.28M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
4.28M
                DCHECK(has_more);
329
4.28M
                literal_count_--;
330
4.28M
                ret++;
331
4.28M
                rem--;
332
4.28M
            }
333
334
12.7M
            while (literal_count_ > 0) {
335
12.1M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
12.1M
                DCHECK(result);
337
12.1M
                if (current_value_ != *val || rem == 0) {
338
4.26M
                    bit_reader_.Rewind(bit_width_);
339
4.26M
                    return ret;
340
4.26M
                }
341
7.87M
                ret++;
342
7.87M
                rem--;
343
7.87M
                literal_count_--;
344
7.87M
            }
345
4.90M
        }
346
7.07M
    }
347
6.12k
    return ret;
348
5.78M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
303
2.08M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
2.08M
    DCHECK(bit_reader_.is_initialized());
305
2.08M
    DCHECK_GT(max_run, 0);
306
2.08M
    size_t ret = 0;
307
2.08M
    size_t rem = max_run;
308
2.48M
    while (ReadHeader()) {
309
2.48M
        if (repeat_count_ > 0) [[likely]] {
310
868k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
15.3k
                return ret;
312
15.3k
            }
313
853k
            *val = cast_set<T>(current_value_);
314
853k
            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
655k
                repeat_count_ -= rem;
318
655k
                ret += rem;
319
655k
                return ret;
320
655k
            }
321
197k
            ret += repeat_count_;
322
197k
            rem -= repeat_count_;
323
197k
            repeat_count_ = 0;
324
1.61M
        } else {
325
1.61M
            DCHECK(literal_count_ > 0);
326
1.61M
            if (ret == 0) {
327
1.41M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
1.41M
                DCHECK(has_more);
329
1.41M
                literal_count_--;
330
1.41M
                ret++;
331
1.41M
                rem--;
332
1.41M
            }
333
334
4.73M
            while (literal_count_ > 0) {
335
4.53M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
4.53M
                DCHECK(result);
337
4.53M
                if (current_value_ != *val || rem == 0) {
338
1.41M
                    bit_reader_.Rewind(bit_width_);
339
1.41M
                    return ret;
340
1.41M
                }
341
3.11M
                ret++;
342
3.11M
                rem--;
343
3.11M
                literal_count_--;
344
3.11M
            }
345
1.61M
        }
346
2.48M
    }
347
36
    return ret;
348
2.08M
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
303
3.69M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
3.69M
    DCHECK(bit_reader_.is_initialized());
305
3.69M
    DCHECK_GT(max_run, 0);
306
3.69M
    size_t ret = 0;
307
3.69M
    size_t rem = max_run;
308
4.59M
    while (ReadHeader()) {
309
4.58M
        if (repeat_count_ > 0) [[likely]] {
310
1.29M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
56.8k
                return ret;
312
56.8k
            }
313
1.23M
            *val = cast_set<T>(current_value_);
314
1.23M
            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
786k
                repeat_count_ -= rem;
318
786k
                ret += rem;
319
786k
                return ret;
320
786k
            }
321
451k
            ret += repeat_count_;
322
451k
            rem -= repeat_count_;
323
451k
            repeat_count_ = 0;
324
3.29M
        } else {
325
3.29M
            DCHECK(literal_count_ > 0);
326
3.29M
            if (ret == 0) {
327
2.86M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
2.86M
                DCHECK(has_more);
329
2.86M
                literal_count_--;
330
2.86M
                ret++;
331
2.86M
                rem--;
332
2.86M
            }
333
334
8.05M
            while (literal_count_ > 0) {
335
7.61M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
7.61M
                DCHECK(result);
337
7.61M
                if (current_value_ != *val || rem == 0) {
338
2.84M
                    bit_reader_.Rewind(bit_width_);
339
2.84M
                    return ret;
340
2.84M
                }
341
4.76M
                ret++;
342
4.76M
                rem--;
343
4.76M
                literal_count_--;
344
4.76M
            }
345
3.29M
        }
346
4.58M
    }
347
6.08k
    return ret;
348
3.69M
}
349
350
template <typename T>
351
176k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
176k
    size_t read_num = 0;
353
5.44M
    while (read_num < num_values) {
354
5.26M
        size_t read_this_time = num_values - read_num;
355
356
5.26M
        if (LIKELY(repeat_count_ > 0)) {
357
1.35M
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.35M
            std::fill(values, values + read_this_time, current_value_);
359
1.35M
            values += read_this_time;
360
1.35M
            repeat_count_ -= read_this_time;
361
1.35M
            read_num += read_this_time;
362
3.91M
        } else if (literal_count_ > 0) {
363
1.27M
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
20.9M
            for (int i = 0; i < read_this_time; ++i) {
365
19.6M
                bool result = bit_reader_.GetValue(bit_width_, values);
366
19.6M
                DCHECK(result);
367
19.6M
                values++;
368
19.6M
            }
369
1.27M
            literal_count_ -= read_this_time;
370
1.27M
            read_num += read_this_time;
371
2.63M
        } else {
372
2.63M
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.63M
        }
376
5.26M
    }
377
176k
    return read_num;
378
176k
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
351
175k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
175k
    size_t read_num = 0;
353
5.43M
    while (read_num < num_values) {
354
5.26M
        size_t read_this_time = num_values - read_num;
355
356
5.26M
        if (LIKELY(repeat_count_ > 0)) {
357
1.35M
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
1.35M
            std::fill(values, values + read_this_time, current_value_);
359
1.35M
            values += read_this_time;
360
1.35M
            repeat_count_ -= read_this_time;
361
1.35M
            read_num += read_this_time;
362
3.90M
        } else if (literal_count_ > 0) {
363
1.27M
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
20.9M
            for (int i = 0; i < read_this_time; ++i) {
365
19.6M
                bool result = bit_reader_.GetValue(bit_width_, values);
366
19.6M
                DCHECK(result);
367
19.6M
                values++;
368
19.6M
            }
369
1.27M
            literal_count_ -= read_this_time;
370
1.27M
            read_num += read_this_time;
371
2.63M
        } else {
372
2.63M
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
2.63M
        }
376
5.26M
    }
377
175k
    return read_num;
378
175k
}
_ZN5doris10RleDecoderIhE10get_valuesEPhm
Line
Count
Source
351
771
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
771
    size_t read_num = 0;
353
2.31k
    while (read_num < num_values) {
354
1.54k
        size_t read_this_time = num_values - read_num;
355
356
1.54k
        if (LIKELY(repeat_count_ > 0)) {
357
80
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
358
80
            std::fill(values, values + read_this_time, current_value_);
359
80
            values += read_this_time;
360
80
            repeat_count_ -= read_this_time;
361
80
            read_num += read_this_time;
362
1.46k
        } else if (literal_count_ > 0) {
363
691
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
3.15k
            for (int i = 0; i < read_this_time; ++i) {
365
2.45k
                bool result = bit_reader_.GetValue(bit_width_, values);
366
2.45k
                DCHECK(result);
367
2.45k
                values++;
368
2.45k
            }
369
691
            literal_count_ -= read_this_time;
370
691
            read_num += read_this_time;
371
770
        } else {
372
770
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
770
        }
376
1.54k
    }
377
771
    return read_num;
378
771
}
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.43M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.43M
    DCHECK(bit_reader_.is_initialized());
401
402
1.43M
    size_t set_count = 0;
403
2.13M
    while (to_skip > 0) {
404
703k
        bool result = ReadHeader();
405
703k
        DCHECK(result);
406
407
703k
        if (repeat_count_ > 0) [[likely]] {
408
258k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
258k
            repeat_count_ -= nskip;
410
258k
            to_skip -= nskip;
411
258k
            if (current_value_ != 0) {
412
215k
                set_count += nskip;
413
215k
            }
414
445k
        } else {
415
445k
            DCHECK(literal_count_ > 0);
416
445k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
445k
            literal_count_ -= nskip;
418
445k
            to_skip -= nskip;
419
25.8M
            for (; nskip > 0; nskip--) {
420
25.4M
                T value = 0;
421
25.4M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
25.4M
                DCHECK(result1);
423
25.4M
                if (value != 0) {
424
15.0M
                    set_count++;
425
15.0M
                }
426
25.4M
            }
427
445k
        }
428
703k
    }
429
1.43M
    return set_count;
430
1.43M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
399
365k
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
365k
    DCHECK(bit_reader_.is_initialized());
401
402
365k
    size_t set_count = 0;
403
946k
    while (to_skip > 0) {
404
581k
        bool result = ReadHeader();
405
581k
        DCHECK(result);
406
407
581k
        if (repeat_count_ > 0) [[likely]] {
408
211k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
211k
            repeat_count_ -= nskip;
410
211k
            to_skip -= nskip;
411
211k
            if (current_value_ != 0) {
412
193k
                set_count += nskip;
413
193k
            }
414
370k
        } else {
415
370k
            DCHECK(literal_count_ > 0);
416
370k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
370k
            literal_count_ -= nskip;
418
370k
            to_skip -= nskip;
419
7.00M
            for (; nskip > 0; nskip--) {
420
6.63M
                T value = 0;
421
6.63M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
6.63M
                DCHECK(result1);
423
6.63M
                if (value != 0) {
424
5.29M
                    set_count++;
425
5.29M
                }
426
6.63M
            }
427
370k
        }
428
581k
    }
429
365k
    return set_count;
430
365k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
399
1.06M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.06M
    DCHECK(bit_reader_.is_initialized());
401
402
1.06M
    size_t set_count = 0;
403
1.18M
    while (to_skip > 0) {
404
122k
        bool result = ReadHeader();
405
122k
        DCHECK(result);
406
407
122k
        if (repeat_count_ > 0) [[likely]] {
408
46.8k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
46.8k
            repeat_count_ -= nskip;
410
46.8k
            to_skip -= nskip;
411
46.8k
            if (current_value_ != 0) {
412
22.3k
                set_count += nskip;
413
22.3k
            }
414
75.1k
        } else {
415
75.1k
            DCHECK(literal_count_ > 0);
416
75.1k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
75.1k
            literal_count_ -= nskip;
418
75.1k
            to_skip -= nskip;
419
18.8M
            for (; nskip > 0; nskip--) {
420
18.7M
                T value = 0;
421
18.7M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
18.7M
                DCHECK(result1);
423
18.7M
                if (value != 0) {
424
9.73M
                    set_count++;
425
9.73M
                }
426
18.7M
            }
427
75.1k
        }
428
122k
    }
429
1.06M
    return set_count;
430
1.06M
}
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.56M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
8.56M
    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.56M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
1.00M
        repeat_count_ += run_length;
442
1.00M
        return;
443
1.00M
    }
444
445
    // Handle run_length > 1 more efficiently
446
17.0M
    while (run_length > 0) {
447
10.2M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
4.93M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
20.5M
            for (size_t i = 0; i < to_buffer; ++i) {
451
15.6M
                buffered_values_[num_buffered_values_++] = value;
452
15.6M
                ++repeat_count_;
453
15.6M
            }
454
4.93M
            run_length -= to_buffer;
455
4.93M
            if (num_buffered_values_ == 8) {
456
2.02M
                DCHECK_EQ(literal_count_ % 8, 0);
457
2.02M
                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.02M
                if (repeat_count_ >= 8 && run_length > 0) {
461
797k
                    repeat_count_ += run_length;
462
797k
                    return;
463
797k
                }
464
2.02M
            }
465
5.31M
        } else {
466
            // Value changed
467
5.31M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
669k
                DCHECK_EQ(literal_count_, 0);
471
669k
                FlushRepeatedRun();
472
669k
            }
473
5.31M
            repeat_count_ = 1;
474
5.31M
            current_value_ = value;
475
476
5.31M
            buffered_values_[num_buffered_values_++] = value;
477
5.31M
            --run_length;
478
5.31M
            if (num_buffered_values_ == 8) {
479
490k
                DCHECK_EQ(literal_count_ % 8, 0);
480
490k
                FlushBufferedValues(false);
481
490k
            }
482
5.31M
        }
483
10.2M
    }
484
7.55M
}
_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
356k
        repeat_count_ += run_length;
442
356k
        return;
443
356k
    }
444
445
    // Handle run_length > 1 more efficiently
446
7.21M
    while (run_length > 0) {
447
3.60M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
1.80M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
3.60M
            for (size_t i = 0; i < to_buffer; ++i) {
451
1.80M
                buffered_values_[num_buffered_values_++] = value;
452
1.80M
                ++repeat_count_;
453
1.80M
            }
454
1.80M
            run_length -= to_buffer;
455
1.80M
            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.01k
                DCHECK_EQ(literal_count_, 0);
471
5.01k
                FlushRepeatedRun();
472
5.01k
            }
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
222k
                FlushBufferedValues(false);
481
222k
            }
482
1.80M
        }
483
3.60M
    }
484
3.60M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
435
4.60M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
4.60M
    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.60M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
651k
        repeat_count_ += run_length;
442
651k
        return;
443
651k
    }
444
445
    // Handle run_length > 1 more efficiently
446
9.79M
    while (run_length > 0) {
447
6.64M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
3.13M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
16.9M
            for (size_t i = 0; i < to_buffer; ++i) {
451
13.8M
                buffered_values_[num_buffered_values_++] = value;
452
13.8M
                ++repeat_count_;
453
13.8M
            }
454
3.13M
            run_length -= to_buffer;
455
3.13M
            if (num_buffered_values_ == 8) {
456
1.79M
                DCHECK_EQ(literal_count_ % 8, 0);
457
1.79M
                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.79M
                if (repeat_count_ >= 8 && run_length > 0) {
461
797k
                    repeat_count_ += run_length;
462
797k
                    return;
463
797k
                }
464
1.79M
            }
465
3.51M
        } else {
466
            // Value changed
467
3.51M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
664k
                DCHECK_EQ(literal_count_, 0);
471
664k
                FlushRepeatedRun();
472
664k
            }
473
3.51M
            repeat_count_ = 1;
474
3.51M
            current_value_ = value;
475
476
3.51M
            buffered_values_[num_buffered_values_++] = value;
477
3.51M
            --run_length;
478
3.51M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
267k
                FlushBufferedValues(false);
481
267k
            }
482
3.51M
        }
483
6.64M
    }
484
3.94M
}
485
486
template <typename T>
487
2.34M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
2.34M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
700k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
700k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
700k
    }
493
494
    // Write all the buffered values as bit packed literals
495
15.6M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
13.3M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
13.3M
    }
498
2.34M
    num_buffered_values_ = 0;
499
500
2.34M
    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
701k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
701k
        int32_t indicator_value = (num_groups << 1) | 1;
507
701k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
701k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
701k
                cast_set<uint8_t>(indicator_value);
510
701k
        literal_indicator_byte_idx_ = -1;
511
701k
        literal_count_ = 0;
512
701k
    }
513
2.34M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
487
448k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
448k
    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.98M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
3.54M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
3.54M
    }
498
448k
    num_buffered_values_ = 0;
499
500
448k
    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
448k
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
487
1.89M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
1.89M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
688k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
688k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
688k
    }
493
494
    // Write all the buffered values as bit packed literals
495
11.7M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
9.81M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
9.81M
    }
498
1.89M
    num_buffered_values_ = 0;
499
500
1.89M
    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
689k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
689k
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
689k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
689k
                cast_set<uint8_t>(indicator_value);
510
689k
        literal_indicator_byte_idx_ = -1;
511
689k
        literal_count_ = 0;
512
689k
    }
513
1.89M
}
514
515
template <typename T>
516
832k
void RleEncoder<T>::FlushRepeatedRun() {
517
832k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
832k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
832k
    bit_writer_.PutVlqInt(indicator_value);
521
832k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
832k
    num_buffered_values_ = 0;
523
832k
    repeat_count_ = 0;
524
832k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
516
14.5k
void RleEncoder<T>::FlushRepeatedRun() {
517
14.5k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
14.5k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
14.5k
    bit_writer_.PutVlqInt(indicator_value);
521
14.5k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
14.5k
    num_buffered_values_ = 0;
523
14.5k
    repeat_count_ = 0;
524
14.5k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
516
817k
void RleEncoder<T>::FlushRepeatedRun() {
517
817k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
817k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
817k
    bit_writer_.PutVlqInt(indicator_value);
521
817k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
817k
    num_buffered_values_ = 0;
523
817k
    repeat_count_ = 0;
524
817k
}
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.50M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
2.50M
    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
634k
            DCHECK_EQ(literal_count_ % 8, 0);
538
634k
            DCHECK_EQ(repeat_count_, 8);
539
634k
            FlushLiteralRun(true);
540
634k
        }
541
857k
        DCHECK_EQ(literal_count_, 0);
542
857k
        return;
543
857k
    }
544
545
1.64M
    literal_count_ += num_buffered_values_;
546
1.64M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
1.64M
    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.06k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
7.06k
        FlushLiteralRun(true);
552
1.64M
    } else {
553
1.64M
        FlushLiteralRun(done);
554
1.64M
    }
555
1.64M
    repeat_count_ = 0;
556
1.64M
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
529
448k
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
448k
    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.52k
        num_buffered_values_ = 0;
534
6.52k
        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.80k
            DCHECK_EQ(literal_count_ % 8, 0);
538
3.80k
            DCHECK_EQ(repeat_count_, 8);
539
3.80k
            FlushLiteralRun(true);
540
3.80k
        }
541
6.52k
        DCHECK_EQ(literal_count_, 0);
542
6.52k
        return;
543
6.52k
    }
544
545
441k
    literal_count_ += num_buffered_values_;
546
441k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
441k
    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.24k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
5.24k
        FlushLiteralRun(true);
552
436k
    } else {
553
436k
        FlushLiteralRun(done);
554
436k
    }
555
441k
    repeat_count_ = 0;
556
441k
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
529
2.05M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
2.05M
    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
631k
            DCHECK_EQ(literal_count_ % 8, 0);
538
631k
            DCHECK_EQ(repeat_count_, 8);
539
631k
            FlushLiteralRun(true);
540
631k
        }
541
850k
        DCHECK_EQ(literal_count_, 0);
542
850k
        return;
543
850k
    }
544
545
1.20M
    literal_count_ += num_buffered_values_;
546
1.20M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
1.20M
    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
1.81k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
1.81k
        FlushLiteralRun(true);
552
1.20M
    } else {
553
1.20M
        FlushLiteralRun(done);
554
1.20M
    }
555
1.20M
    repeat_count_ = 0;
556
1.20M
}
557
558
template <typename T>
559
27.3k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
560
136k
    for (int i = 0; i < num_bytes; ++i) {
561
109k
        bit_writer_.PutValue(val, 8);
562
109k
    }
563
27.3k
}
564
565
template <typename T>
566
222k
int RleEncoder<T>::Flush() {
567
222k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
222k
        bool all_repeat = literal_count_ == 0 &&
569
222k
                          (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
222k
        if (repeat_count_ > 0 && all_repeat) {
572
163k
            FlushRepeatedRun();
573
163k
        } else {
574
58.9k
            literal_count_ += num_buffered_values_;
575
58.9k
            FlushLiteralRun(true);
576
58.9k
            repeat_count_ = 0;
577
58.9k
        }
578
222k
    }
579
222k
    bit_writer_.Flush();
580
222k
    DCHECK_EQ(num_buffered_values_, 0);
581
222k
    DCHECK_EQ(literal_count_, 0);
582
222k
    DCHECK_EQ(repeat_count_, 0);
583
222k
    return bit_writer_.bytes_written();
584
222k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
566
13.1k
int RleEncoder<T>::Flush() {
567
13.1k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
12.5k
        bool all_repeat = literal_count_ == 0 &&
569
12.5k
                          (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
12.5k
        if (repeat_count_ > 0 && all_repeat) {
572
9.56k
            FlushRepeatedRun();
573
9.56k
        } else {
574
2.96k
            literal_count_ += num_buffered_values_;
575
2.96k
            FlushLiteralRun(true);
576
2.96k
            repeat_count_ = 0;
577
2.96k
        }
578
12.5k
    }
579
13.1k
    bit_writer_.Flush();
580
13.1k
    DCHECK_EQ(num_buffered_values_, 0);
581
13.1k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
13.1k
    return bit_writer_.bytes_written();
584
13.1k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
566
209k
int RleEncoder<T>::Flush() {
567
209k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
209k
        bool all_repeat = literal_count_ == 0 &&
569
209k
                          (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
209k
        if (repeat_count_ > 0 && all_repeat) {
572
153k
            FlushRepeatedRun();
573
153k
        } else {
574
56.0k
            literal_count_ += num_buffered_values_;
575
56.0k
            FlushLiteralRun(true);
576
56.0k
            repeat_count_ = 0;
577
56.0k
        }
578
209k
    }
579
209k
    bit_writer_.Flush();
580
209k
    DCHECK_EQ(num_buffered_values_, 0);
581
209k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
209k
    return bit_writer_.bytes_written();
584
209k
}
585
586
template <typename T>
587
1.21M
void RleEncoder<T>::Clear() {
588
1.21M
    current_value_ = 0;
589
1.21M
    repeat_count_ = 0;
590
1.21M
    num_buffered_values_ = 0;
591
1.21M
    literal_count_ = 0;
592
1.21M
    literal_indicator_byte_idx_ = -1;
593
1.21M
    bit_writer_.Clear();
594
1.21M
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
587
41.5k
void RleEncoder<T>::Clear() {
588
41.5k
    current_value_ = 0;
589
41.5k
    repeat_count_ = 0;
590
41.5k
    num_buffered_values_ = 0;
591
41.5k
    literal_count_ = 0;
592
41.5k
    literal_indicator_byte_idx_ = -1;
593
41.5k
    bit_writer_.Clear();
594
41.5k
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
587
1.17M
void RleEncoder<T>::Clear() {
588
1.17M
    current_value_ = 0;
589
1.17M
    repeat_count_ = 0;
590
1.17M
    num_buffered_values_ = 0;
591
1.17M
    literal_count_ = 0;
592
1.17M
    literal_indicator_byte_idx_ = -1;
593
1.17M
    bit_writer_.Clear();
594
1.17M
}
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
458k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
669
458k
        Reset(buffer, buffer_len, bit_width);
670
458k
    }
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
3.19M
    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.03M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
751
3.03M
    int32_t num_to_output =
752
3.03M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
753
3.03M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
754
3.03M
    literal_buffer_pos_ += num_to_output;
755
3.03M
    literal_count_ -= num_to_output;
756
3.03M
    return num_to_output;
757
3.03M
}
758
759
template <typename T>
760
458k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
761
458k
    bit_reader_.Reset(buffer, buffer_len);
762
458k
    bit_width_ = bit_width;
763
458k
    repeat_count_ = 0;
764
458k
    literal_count_ = 0;
765
458k
    num_buffered_literals_ = 0;
766
458k
    literal_buffer_pos_ = 0;
767
458k
}
768
769
template <typename T>
770
5.44M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
771
5.44M
    if (repeat_count_ > 0) return repeat_count_;
772
5.44M
    if (literal_count_ == 0) NextCounts();
773
5.44M
    return repeat_count_;
774
5.44M
}
775
776
template <typename T>
777
5.33M
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
5.33M
    uint32_t indicator_value = 0;
781
5.33M
    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
5.33M
    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
5.33M
    uint32_t run_len = indicator_value >> 1;
791
5.33M
    if (is_literal) {
792
        // Use int64_t to avoid overflowing multiplication.
793
3.09M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
794
3.09M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
795
3.09M
        literal_count_ = cast_set<int32_t>(literal_count);
796
3.09M
    } else {
797
2.24M
        if (UNLIKELY(run_len == 0)) return;
798
2.24M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
799
2.24M
        if (UNLIKELY(!result)) return;
800
2.24M
        repeat_count_ = run_len;
801
2.24M
    }
802
5.33M
}
803
804
template <typename T>
805
2.25M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
806
2.25M
    repeat_count_ -= num_repeats_to_consume;
807
2.25M
    return repeated_value_;
808
2.25M
}
809
810
template <typename T>
811
3.19M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
812
3.19M
    if (literal_count_ > 0) return literal_count_;
813
18.4E
    if (repeat_count_ == 0) NextCounts();
814
18.4E
    return literal_count_;
815
3.19M
}
816
817
template <typename T>
818
3.19M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
819
3.19M
    int32_t num_consumed = 0;
820
    // Copy any buffered literals left over from previous calls.
821
3.19M
    if (HaveBufferedLiterals()) {
822
82.5k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
823
82.5k
    }
824
825
3.19M
    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
3.19M
    int32_t num_to_bypass =
830
3.19M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
831
3.19M
    if (num_to_bypass > 0) {
832
2.06M
        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.06M
        if (num_read < num_to_bypass) return false;
835
2.06M
        literal_count_ -= num_to_bypass;
836
2.06M
        num_consumed += num_to_bypass;
837
2.06M
        num_remaining = num_literals_to_consume - num_consumed;
838
2.06M
    }
839
840
3.19M
    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
2.94M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
844
2.94M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
845
2.94M
    }
846
3.19M
    return true;
847
3.19M
}
848
849
template <typename T>
850
2.94M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
851
2.94M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
852
2.94M
    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
2.94M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
855
2.94M
    literal_buffer_pos_ = 0;
856
2.94M
    return true;
857
2.94M
}
858
859
template <typename T>
860
587k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
861
587k
    uint32_t num_consumed = 0;
862
6.03M
    while (num_consumed < batch_num) {
863
        // Add RLE encoded values by repeating the current value this number of times.
864
5.44M
        uint32_t num_repeats = NextNumRepeats();
865
5.44M
        if (num_repeats > 0) {
866
2.25M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
867
2.25M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
868
46.6M
            for (int i = 0; i < num_repeats_to_set; ++i) {
869
44.4M
                values[num_consumed + i] = repeated_value;
870
44.4M
            }
871
2.25M
            num_consumed += num_repeats_to_set;
872
2.25M
            continue;
873
2.25M
        }
874
875
        // Add remaining literal values, if any.
876
3.19M
        uint32_t num_literals = NextNumLiterals();
877
3.19M
        if (num_literals == 0) {
878
0
            break;
879
0
        }
880
3.19M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
881
3.19M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
882
0
            return 0;
883
0
        }
884
3.19M
        num_consumed += num_literals_to_set;
885
3.19M
    }
886
587k
    return num_consumed;
887
587k
}
888
#include "common/compile_check_end.h"
889
} // namespace doris