Coverage Report

Created: 2026-03-14 17:45

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.40M
            : bit_reader_(buffer, buffer_len),
90
1.40M
              bit_width_(bit_width),
91
1.40M
              current_value_(0),
92
1.40M
              repeat_count_(0),
93
1.40M
              literal_count_(0),
94
1.40M
              rewind_state_(CANT_REWIND) {
95
1.40M
        DCHECK_GE(bit_width_, 1);
96
1.40M
        DCHECK_LE(bit_width_, 64);
97
1.40M
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
89
408k
            : bit_reader_(buffer, buffer_len),
90
408k
              bit_width_(bit_width),
91
408k
              current_value_(0),
92
408k
              repeat_count_(0),
93
408k
              literal_count_(0),
94
408k
              rewind_state_(CANT_REWIND) {
95
408k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
408k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
89
23.2k
            : bit_reader_(buffer, buffer_len),
90
23.2k
              bit_width_(bit_width),
91
23.2k
              current_value_(0),
92
23.2k
              repeat_count_(0),
93
23.2k
              literal_count_(0),
94
23.2k
              rewind_state_(CANT_REWIND) {
95
23.2k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
23.2k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
89
976k
            : bit_reader_(buffer, buffer_len),
90
976k
              bit_width_(bit_width),
91
976k
              current_value_(0),
92
976k
              repeat_count_(0),
93
976k
              literal_count_(0),
94
976k
              rewind_state_(CANT_REWIND) {
95
976k
        DCHECK_GE(bit_width_, 1);
96
        DCHECK_LE(bit_width_, 64);
97
976k
    }
98
99
39.6M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
99
39.2M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
99
20.8k
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
99
362k
    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
530k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
530k
        DCHECK_GE(bit_width_, 1);
156
530k
        DCHECK_LE(bit_width_, 64);
157
530k
        Clear();
158
530k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
154
13.2k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
13.2k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
13.2k
        Clear();
158
13.2k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
154
517k
            : bit_width_(bit_width), bit_writer_(buffer) {
155
517k
        DCHECK_GE(bit_width_, 1);
156
        DCHECK_LE(bit_width_, 64);
157
517k
        Clear();
158
517k
    }
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
181k
    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
296M
bool RleDecoder<T>::ReadHeader() {
233
296M
    DCHECK(bit_reader_.is_initialized());
234
296M
    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
12.6M
        uint32_t indicator_value = 0;
238
12.6M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
12.6M
        if (!result) [[unlikely]] {
240
3.02k
            return false;
241
3.02k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
12.5M
        bool is_literal = indicator_value & 1;
245
12.5M
        if (is_literal) {
246
6.09M
            literal_count_ = (indicator_value >> 1) * 8;
247
6.09M
            DCHECK_GT(literal_count_, 0);
248
6.50M
        } else {
249
6.50M
            repeat_count_ = indicator_value >> 1;
250
6.50M
            DCHECK_GT(repeat_count_, 0);
251
6.50M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
6.50M
                                                     reinterpret_cast<T*>(&current_value_));
253
6.50M
            DCHECK(result1);
254
6.50M
        }
255
12.5M
    }
256
296M
    return true;
257
296M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
232
280M
bool RleDecoder<T>::ReadHeader() {
233
280M
    DCHECK(bit_reader_.is_initialized());
234
280M
    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.78M
        uint32_t indicator_value = 0;
238
9.78M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
9.78M
        if (!result) [[unlikely]] {
240
46
            return false;
241
46
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
9.78M
        bool is_literal = indicator_value & 1;
245
9.78M
        if (is_literal) {
246
4.68M
            literal_count_ = (indicator_value >> 1) * 8;
247
4.68M
            DCHECK_GT(literal_count_, 0);
248
5.10M
        } else {
249
5.10M
            repeat_count_ = indicator_value >> 1;
250
5.10M
            DCHECK_GT(repeat_count_, 0);
251
5.10M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
5.10M
                                                     reinterpret_cast<T*>(&current_value_));
253
5.10M
            DCHECK(result1);
254
5.10M
        }
255
9.78M
    }
256
280M
    return true;
257
280M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
232
7.74M
bool RleDecoder<T>::ReadHeader() {
233
7.74M
    DCHECK(bit_reader_.is_initialized());
234
7.74M
    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.69M
        uint32_t indicator_value = 0;
238
2.69M
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
2.69M
        if (!result) [[unlikely]] {
240
2.97k
            return false;
241
2.97k
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
2.69M
        bool is_literal = indicator_value & 1;
245
2.69M
        if (is_literal) {
246
1.33M
            literal_count_ = (indicator_value >> 1) * 8;
247
1.33M
            DCHECK_GT(literal_count_, 0);
248
1.36M
        } else {
249
1.36M
            repeat_count_ = indicator_value >> 1;
250
1.36M
            DCHECK_GT(repeat_count_, 0);
251
1.36M
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
1.36M
                                                     reinterpret_cast<T*>(&current_value_));
253
1.36M
            DCHECK(result1);
254
1.36M
        }
255
2.69M
    }
256
7.74M
    return true;
257
7.74M
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
232
7.93M
bool RleDecoder<T>::ReadHeader() {
233
7.93M
    DCHECK(bit_reader_.is_initialized());
234
7.93M
    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
114k
        uint32_t indicator_value = 0;
238
114k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
239
114k
        if (!result) [[unlikely]] {
240
0
            return false;
241
0
        }
242
243
        // lsb indicates if it is a literal run or repeated run
244
114k
        bool is_literal = indicator_value & 1;
245
114k
        if (is_literal) {
246
75.6k
            literal_count_ = (indicator_value >> 1) * 8;
247
75.6k
            DCHECK_GT(literal_count_, 0);
248
75.6k
        } else {
249
39.1k
            repeat_count_ = indicator_value >> 1;
250
39.1k
            DCHECK_GT(repeat_count_, 0);
251
39.1k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
252
39.1k
                                                     reinterpret_cast<T*>(&current_value_));
253
39.1k
            DCHECK(result1);
254
39.1k
        }
255
114k
    }
256
7.93M
    return true;
257
7.93M
}
258
259
template <typename T>
260
282M
bool RleDecoder<T>::Get(T* val) {
261
282M
    DCHECK(bit_reader_.is_initialized());
262
282M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
282M
    if (repeat_count_ > 0) [[likely]] {
267
205M
        *val = cast_set<T>(current_value_);
268
205M
        --repeat_count_;
269
205M
        rewind_state_ = REWIND_RUN;
270
205M
    } else {
271
77.4M
        DCHECK(literal_count_ > 0);
272
77.4M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
77.4M
        DCHECK(result);
274
77.4M
        --literal_count_;
275
77.4M
        rewind_state_ = REWIND_LITERAL;
276
77.4M
    }
277
278
282M
    return true;
279
282M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
260
275M
bool RleDecoder<T>::Get(T* val) {
261
275M
    DCHECK(bit_reader_.is_initialized());
262
275M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
275M
    if (repeat_count_ > 0) [[likely]] {
267
205M
        *val = cast_set<T>(current_value_);
268
205M
        --repeat_count_;
269
205M
        rewind_state_ = REWIND_RUN;
270
205M
    } else {
271
69.9M
        DCHECK(literal_count_ > 0);
272
69.9M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
69.9M
        DCHECK(result);
274
69.9M
        --literal_count_;
275
69.9M
        rewind_state_ = REWIND_LITERAL;
276
69.9M
    }
277
278
275M
    return true;
279
275M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
260
7.78M
bool RleDecoder<T>::Get(T* val) {
261
7.78M
    DCHECK(bit_reader_.is_initialized());
262
7.78M
    if (!ReadHeader()) [[unlikely]] {
263
0
        return false;
264
0
    }
265
266
7.78M
    if (repeat_count_ > 0) [[likely]] {
267
318k
        *val = cast_set<T>(current_value_);
268
318k
        --repeat_count_;
269
318k
        rewind_state_ = REWIND_RUN;
270
7.46M
    } else {
271
7.46M
        DCHECK(literal_count_ > 0);
272
7.46M
        bool result = bit_reader_.GetValue(bit_width_, val);
273
7.46M
        DCHECK(result);
274
7.46M
        --literal_count_;
275
7.46M
        rewind_state_ = REWIND_LITERAL;
276
7.46M
    }
277
278
7.78M
    return true;
279
7.78M
}
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
7.32M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
7.32M
    DCHECK(bit_reader_.is_initialized());
305
7.32M
    DCHECK_GT(max_run, 0);
306
7.32M
    size_t ret = 0;
307
7.32M
    size_t rem = max_run;
308
9.23M
    while (ReadHeader()) {
309
9.23M
        if (repeat_count_ > 0) [[likely]] {
310
2.53M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
103k
                return ret;
312
103k
            }
313
2.42M
            *val = cast_set<T>(current_value_);
314
2.42M
            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.45M
                repeat_count_ -= rem;
318
1.45M
                ret += rem;
319
1.45M
                return ret;
320
1.45M
            }
321
969k
            ret += repeat_count_;
322
969k
            rem -= repeat_count_;
323
969k
            repeat_count_ = 0;
324
6.70M
        } else {
325
6.70M
            DCHECK(literal_count_ > 0);
326
6.70M
            if (ret == 0) {
327
5.76M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
5.76M
                DCHECK(has_more);
329
5.76M
                literal_count_--;
330
5.76M
                ret++;
331
5.76M
                rem--;
332
5.76M
            }
333
334
17.7M
            while (literal_count_ > 0) {
335
16.8M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
16.8M
                DCHECK(result);
337
16.8M
                if (current_value_ != *val || rem == 0) {
338
5.76M
                    bit_reader_.Rewind(bit_width_);
339
5.76M
                    return ret;
340
5.76M
                }
341
11.0M
                ret++;
342
11.0M
                rem--;
343
11.0M
                literal_count_--;
344
11.0M
            }
345
6.70M
        }
346
9.23M
    }
347
18.4E
    return ret;
348
7.32M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
303
2.13M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
2.13M
    DCHECK(bit_reader_.is_initialized());
305
2.13M
    DCHECK_GT(max_run, 0);
306
2.13M
    size_t ret = 0;
307
2.13M
    size_t rem = max_run;
308
2.52M
    while (ReadHeader()) {
309
2.52M
        if (repeat_count_ > 0) [[likely]] {
310
911k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
15.3k
                return ret;
312
15.3k
            }
313
896k
            *val = cast_set<T>(current_value_);
314
896k
            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
698k
                repeat_count_ -= rem;
318
698k
                ret += rem;
319
698k
                return ret;
320
698k
            }
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.52M
    }
347
18.4E
    return ret;
348
2.13M
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
303
5.18M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
304
5.18M
    DCHECK(bit_reader_.is_initialized());
305
5.18M
    DCHECK_GT(max_run, 0);
306
5.18M
    size_t ret = 0;
307
5.18M
    size_t rem = max_run;
308
6.71M
    while (ReadHeader()) {
309
6.71M
        if (repeat_count_ > 0) [[likely]] {
310
1.61M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
311
88.4k
                return ret;
312
88.4k
            }
313
1.53M
            *val = cast_set<T>(current_value_);
314
1.53M
            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
758k
                repeat_count_ -= rem;
318
758k
                ret += rem;
319
758k
                return ret;
320
758k
            }
321
772k
            ret += repeat_count_;
322
772k
            rem -= repeat_count_;
323
772k
            repeat_count_ = 0;
324
5.09M
        } else {
325
5.09M
            DCHECK(literal_count_ > 0);
326
5.09M
            if (ret == 0) {
327
4.35M
                bool has_more = bit_reader_.GetValue(bit_width_, val);
328
4.35M
                DCHECK(has_more);
329
4.35M
                literal_count_--;
330
4.35M
                ret++;
331
4.35M
                rem--;
332
4.35M
            }
333
334
13.0M
            while (literal_count_ > 0) {
335
12.3M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
336
12.3M
                DCHECK(result);
337
12.3M
                if (current_value_ != *val || rem == 0) {
338
4.34M
                    bit_reader_.Rewind(bit_width_);
339
4.34M
                    return ret;
340
4.34M
                }
341
7.96M
                ret++;
342
7.96M
                rem--;
343
7.96M
                literal_count_--;
344
7.96M
            }
345
5.09M
        }
346
6.71M
    }
347
18.4E
    return ret;
348
5.18M
}
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.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
175k
    return read_num;
378
175k
}
_ZN5doris10RleDecoderIhE10get_valuesEPhm
Line
Count
Source
351
779
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
352
779
    size_t read_num = 0;
353
2.33k
    while (read_num < num_values) {
354
1.55k
        size_t read_this_time = num_values - read_num;
355
356
1.55k
        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.47k
        } else if (literal_count_ > 0) {
363
699
            read_this_time = std::min((size_t)literal_count_, read_this_time);
364
3.17k
            for (int i = 0; i < read_this_time; ++i) {
365
2.47k
                bool result = bit_reader_.GetValue(bit_width_, values);
366
2.47k
                DCHECK(result);
367
2.47k
                values++;
368
2.47k
            }
369
699
            literal_count_ -= read_this_time;
370
699
            read_num += read_this_time;
371
778
        } else {
372
778
            if (!ReadHeader()) {
373
0
                return read_num;
374
0
            }
375
778
        }
376
1.55k
    }
377
779
    return read_num;
378
779
}
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.47M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.47M
    DCHECK(bit_reader_.is_initialized());
401
402
1.47M
    size_t set_count = 0;
403
2.61M
    while (to_skip > 0) {
404
1.14M
        bool result = ReadHeader();
405
1.14M
        DCHECK(result);
406
407
1.14M
        if (repeat_count_ > 0) [[likely]] {
408
547k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
547k
            repeat_count_ -= nskip;
410
547k
            to_skip -= nskip;
411
547k
            if (current_value_ != 0) {
412
465k
                set_count += nskip;
413
465k
            }
414
598k
        } else {
415
598k
            DCHECK(literal_count_ > 0);
416
598k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
598k
            literal_count_ -= nskip;
418
598k
            to_skip -= nskip;
419
28.6M
            for (; nskip > 0; nskip--) {
420
28.0M
                T value = 0;
421
28.0M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
28.0M
                DCHECK(result1);
423
28.0M
                if (value != 0) {
424
17.0M
                    set_count++;
425
17.0M
                }
426
28.0M
            }
427
598k
        }
428
1.14M
    }
429
1.47M
    return set_count;
430
1.47M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
399
298k
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
298k
    DCHECK(bit_reader_.is_initialized());
401
402
298k
    size_t set_count = 0;
403
1.33M
    while (to_skip > 0) {
404
1.03M
        bool result = ReadHeader();
405
1.03M
        DCHECK(result);
406
407
1.03M
        if (repeat_count_ > 0) [[likely]] {
408
511k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
511k
            repeat_count_ -= nskip;
410
511k
            to_skip -= nskip;
411
511k
            if (current_value_ != 0) {
412
451k
                set_count += nskip;
413
451k
            }
414
527k
        } else {
415
527k
            DCHECK(literal_count_ > 0);
416
527k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
527k
            literal_count_ -= nskip;
418
527k
            to_skip -= nskip;
419
10.2M
            for (; nskip > 0; nskip--) {
420
9.68M
                T value = 0;
421
9.68M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
9.68M
                DCHECK(result1);
423
9.68M
                if (value != 0) {
424
7.76M
                    set_count++;
425
7.76M
                }
426
9.68M
            }
427
527k
        }
428
1.03M
    }
429
298k
    return set_count;
430
298k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
399
1.17M
size_t RleDecoder<T>::Skip(size_t to_skip) {
400
1.17M
    DCHECK(bit_reader_.is_initialized());
401
402
1.17M
    size_t set_count = 0;
403
1.28M
    while (to_skip > 0) {
404
107k
        bool result = ReadHeader();
405
107k
        DCHECK(result);
406
407
107k
        if (repeat_count_ > 0) [[likely]] {
408
36.0k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
409
36.0k
            repeat_count_ -= nskip;
410
36.0k
            to_skip -= nskip;
411
36.0k
            if (current_value_ != 0) {
412
13.6k
                set_count += nskip;
413
13.6k
            }
414
71.5k
        } else {
415
71.5k
            DCHECK(literal_count_ > 0);
416
71.5k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
417
71.5k
            literal_count_ -= nskip;
418
71.5k
            to_skip -= nskip;
419
18.4M
            for (; nskip > 0; nskip--) {
420
18.3M
                T value = 0;
421
18.3M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
422
18.3M
                DCHECK(result1);
423
18.3M
                if (value != 0) {
424
9.33M
                    set_count++;
425
9.33M
                }
426
18.3M
            }
427
71.5k
        }
428
107k
    }
429
1.17M
    return set_count;
430
1.17M
}
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
11.9M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
11.9M
    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
11.9M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
1.11M
        repeat_count_ += run_length;
442
1.11M
        return;
443
1.11M
    }
444
445
    // Handle run_length > 1 more efficiently
446
25.4M
    while (run_length > 0) {
447
15.9M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
7.37M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
33.7M
            for (size_t i = 0; i < to_buffer; ++i) {
451
26.3M
                buffered_values_[num_buffered_values_++] = value;
452
26.3M
                ++repeat_count_;
453
26.3M
            }
454
7.37M
            run_length -= to_buffer;
455
7.37M
            if (num_buffered_values_ == 8) {
456
3.49M
                DCHECK_EQ(literal_count_ % 8, 0);
457
3.49M
                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
3.49M
                if (repeat_count_ >= 8 && run_length > 0) {
461
1.31M
                    repeat_count_ += run_length;
462
1.31M
                    return;
463
1.31M
                }
464
3.49M
            }
465
8.55M
        } else {
466
            // Value changed
467
8.55M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
1.27M
                DCHECK_EQ(literal_count_, 0);
471
1.27M
                FlushRepeatedRun();
472
1.27M
            }
473
8.55M
            repeat_count_ = 1;
474
8.55M
            current_value_ = value;
475
476
8.55M
            buffered_values_[num_buffered_values_++] = value;
477
8.55M
            --run_length;
478
8.55M
            if (num_buffered_values_ == 8) {
479
766k
                DCHECK_EQ(literal_count_ % 8, 0);
480
766k
                FlushBufferedValues(false);
481
766k
            }
482
8.55M
        }
483
15.9M
    }
484
10.8M
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
435
4.09M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
4.09M
    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.09M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
462k
        repeat_count_ += run_length;
442
462k
        return;
443
462k
    }
444
445
    // Handle run_length > 1 more efficiently
446
7.25M
    while (run_length > 0) {
447
3.62M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
1.81M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
3.63M
            for (size_t i = 0; i < to_buffer; ++i) {
451
1.81M
                buffered_values_[num_buffered_values_++] = value;
452
1.81M
                ++repeat_count_;
453
1.81M
            }
454
1.81M
            run_length -= to_buffer;
455
1.81M
            if (num_buffered_values_ == 8) {
456
227k
                DCHECK_EQ(literal_count_ % 8, 0);
457
227k
                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
227k
                if (repeat_count_ >= 8 && run_length > 0) {
461
3
                    repeat_count_ += run_length;
462
3
                    return;
463
3
                }
464
227k
            }
465
1.81M
        } else {
466
            // Value changed
467
1.81M
            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.77k
                DCHECK_EQ(literal_count_, 0);
471
5.77k
                FlushRepeatedRun();
472
5.77k
            }
473
1.81M
            repeat_count_ = 1;
474
1.81M
            current_value_ = value;
475
476
1.81M
            buffered_values_[num_buffered_values_++] = value;
477
1.81M
            --run_length;
478
1.81M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
223k
                FlushBufferedValues(false);
481
223k
            }
482
1.81M
        }
483
3.62M
    }
484
3.62M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
435
7.82M
void RleEncoder<T>::Put(T value, size_t run_length) {
436
7.82M
    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
7.82M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
441
650k
        repeat_count_ += run_length;
442
650k
        return;
443
650k
    }
444
445
    // Handle run_length > 1 more efficiently
446
18.1M
    while (run_length > 0) {
447
12.3M
        if (current_value_ == value) [[likely]] {
448
            // Need to buffer values until we reach 8
449
5.55M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
450
30.1M
            for (size_t i = 0; i < to_buffer; ++i) {
451
24.5M
                buffered_values_[num_buffered_values_++] = value;
452
24.5M
                ++repeat_count_;
453
24.5M
            }
454
5.55M
            run_length -= to_buffer;
455
5.55M
            if (num_buffered_values_ == 8) {
456
3.27M
                DCHECK_EQ(literal_count_ % 8, 0);
457
3.27M
                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
3.27M
                if (repeat_count_ >= 8 && run_length > 0) {
461
1.31M
                    repeat_count_ += run_length;
462
1.31M
                    return;
463
1.31M
                }
464
3.27M
            }
465
6.74M
        } else {
466
            // Value changed
467
6.74M
            if (repeat_count_ >= 8) {
468
                // We had a run that was long enough but it has ended.  Flush the
469
                // current repeated run.
470
1.26M
                DCHECK_EQ(literal_count_, 0);
471
1.26M
                FlushRepeatedRun();
472
1.26M
            }
473
6.74M
            repeat_count_ = 1;
474
6.74M
            current_value_ = value;
475
476
6.74M
            buffered_values_[num_buffered_values_++] = value;
477
6.74M
            --run_length;
478
6.74M
            if (num_buffered_values_ == 8) {
479
                DCHECK_EQ(literal_count_ % 8, 0);
480
543k
                FlushBufferedValues(false);
481
543k
            }
482
6.74M
        }
483
12.3M
    }
484
7.17M
}
485
486
template <typename T>
487
4.13M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
4.13M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
1.29M
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
1.29M
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
1.29M
    }
493
494
    // Write all the buffered values as bit packed literals
495
27.0M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
22.9M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
22.9M
    }
498
4.13M
    num_buffered_values_ = 0;
499
500
4.13M
    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
1.29M
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
1.29M
        int32_t indicator_value = (num_groups << 1) | 1;
507
1.29M
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
1.29M
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
1.29M
                cast_set<uint8_t>(indicator_value);
510
1.29M
        literal_indicator_byte_idx_ = -1;
511
1.29M
        literal_count_ = 0;
512
1.29M
    }
513
4.13M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
487
451k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
451k
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
12.6k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
12.6k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
12.6k
    }
493
494
    // Write all the buffered values as bit packed literals
495
4.01M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
3.56M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
3.56M
    }
498
451k
    num_buffered_values_ = 0;
499
500
451k
    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.6k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
12.6k
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
12.6k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
12.6k
                cast_set<uint8_t>(indicator_value);
510
12.6k
        literal_indicator_byte_idx_ = -1;
511
12.6k
        literal_count_ = 0;
512
12.6k
    }
513
451k
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
487
3.68M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
488
3.68M
    if (literal_indicator_byte_idx_ < 0) {
489
        // The literal indicator byte has not been reserved yet, get one now.
490
1.27M
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
491
1.27M
        DCHECK_GE(literal_indicator_byte_idx_, 0);
492
1.27M
    }
493
494
    // Write all the buffered values as bit packed literals
495
23.0M
    for (int i = 0; i < num_buffered_values_; ++i) {
496
19.3M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
497
19.3M
    }
498
3.68M
    num_buffered_values_ = 0;
499
500
3.68M
    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
1.27M
        int num_groups = BitUtil::Ceil(literal_count_, 8);
506
1.27M
        int32_t indicator_value = (num_groups << 1) | 1;
507
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
508
1.27M
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
509
1.27M
                cast_set<uint8_t>(indicator_value);
510
1.27M
        literal_indicator_byte_idx_ = -1;
511
1.27M
        literal_count_ = 0;
512
1.27M
    }
513
3.68M
}
514
515
template <typename T>
516
1.37M
void RleEncoder<T>::FlushRepeatedRun() {
517
1.37M
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
1.37M
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
1.37M
    bit_writer_.PutVlqInt(indicator_value);
521
1.37M
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
1.37M
    num_buffered_values_ = 0;
523
1.37M
    repeat_count_ = 0;
524
1.37M
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
516
14.4k
void RleEncoder<T>::FlushRepeatedRun() {
517
14.4k
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
14.4k
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
14.4k
    bit_writer_.PutVlqInt(indicator_value);
521
14.4k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
14.4k
    num_buffered_values_ = 0;
523
14.4k
    repeat_count_ = 0;
524
14.4k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
516
1.36M
void RleEncoder<T>::FlushRepeatedRun() {
517
1.36M
    DCHECK_GT(repeat_count_, 0);
518
    // The lsb of 0 indicates this is a repeated run
519
1.36M
    int32_t indicator_value = repeat_count_ << 1 | 0;
520
1.36M
    bit_writer_.PutVlqInt(indicator_value);
521
1.36M
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
522
1.36M
    num_buffered_values_ = 0;
523
1.36M
    repeat_count_ = 0;
524
1.36M
}
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
4.25M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
4.25M
    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
1.40M
        num_buffered_values_ = 0;
534
1.40M
        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
1.24M
            DCHECK_EQ(literal_count_ % 8, 0);
538
1.24M
            DCHECK_EQ(repeat_count_, 8);
539
1.24M
            FlushLiteralRun(true);
540
1.24M
        }
541
1.40M
        DCHECK_EQ(literal_count_, 0);
542
1.40M
        return;
543
1.40M
    }
544
545
2.85M
    literal_count_ += num_buffered_values_;
546
2.85M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
2.85M
    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
8.84k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
8.84k
        FlushLiteralRun(true);
552
2.84M
    } else {
553
2.84M
        FlushLiteralRun(done);
554
2.84M
    }
555
2.85M
    repeat_count_ = 0;
556
2.85M
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
529
451k
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
451k
    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
7.12k
        num_buffered_values_ = 0;
534
7.12k
        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
4.53k
            DCHECK_EQ(literal_count_ % 8, 0);
538
4.53k
            DCHECK_EQ(repeat_count_, 8);
539
4.53k
            FlushLiteralRun(true);
540
4.53k
        }
541
7.12k
        DCHECK_EQ(literal_count_, 0);
542
7.12k
        return;
543
7.12k
    }
544
545
444k
    literal_count_ += num_buffered_values_;
546
444k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
444k
    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
438k
    } else {
553
438k
        FlushLiteralRun(done);
554
438k
    }
555
444k
    repeat_count_ = 0;
556
444k
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
529
3.80M
void RleEncoder<T>::FlushBufferedValues(bool done) {
530
3.80M
    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
1.39M
        num_buffered_values_ = 0;
534
1.39M
        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
1.23M
            DCHECK_EQ(literal_count_ % 8, 0);
538
1.23M
            DCHECK_EQ(repeat_count_, 8);
539
1.23M
            FlushLiteralRun(true);
540
1.23M
        }
541
1.39M
        DCHECK_EQ(literal_count_, 0);
542
1.39M
        return;
543
1.39M
    }
544
545
2.40M
    literal_count_ += num_buffered_values_;
546
2.40M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
547
2.40M
    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
3.59k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
551
3.59k
        FlushLiteralRun(true);
552
2.40M
    } else {
553
2.40M
        FlushLiteralRun(done);
554
2.40M
    }
555
2.40M
    repeat_count_ = 0;
556
2.40M
}
557
558
template <typename T>
559
25.3k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
560
126k
    for (int i = 0; i < num_bytes; ++i) {
561
101k
        bit_writer_.PutValue(val, 8);
562
101k
    }
563
25.3k
}
564
565
template <typename T>
566
149k
int RleEncoder<T>::Flush() {
567
149k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
148k
        bool all_repeat = literal_count_ == 0 &&
569
148k
                          (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
148k
        if (repeat_count_ > 0 && all_repeat) {
572
107k
            FlushRepeatedRun();
573
107k
        } else {
574
41.1k
            literal_count_ += num_buffered_values_;
575
41.1k
            FlushLiteralRun(true);
576
41.1k
            repeat_count_ = 0;
577
41.1k
        }
578
148k
    }
579
149k
    bit_writer_.Flush();
580
149k
    DCHECK_EQ(num_buffered_values_, 0);
581
149k
    DCHECK_EQ(literal_count_, 0);
582
149k
    DCHECK_EQ(repeat_count_, 0);
583
149k
    return bit_writer_.bytes_written();
584
149k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
566
12.1k
int RleEncoder<T>::Flush() {
567
12.1k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
11.5k
        bool all_repeat = literal_count_ == 0 &&
569
11.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
11.5k
        if (repeat_count_ > 0 && all_repeat) {
572
8.66k
            FlushRepeatedRun();
573
8.66k
        } else {
574
2.85k
            literal_count_ += num_buffered_values_;
575
2.85k
            FlushLiteralRun(true);
576
2.85k
            repeat_count_ = 0;
577
2.85k
        }
578
11.5k
    }
579
12.1k
    bit_writer_.Flush();
580
12.1k
    DCHECK_EQ(num_buffered_values_, 0);
581
12.1k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
12.1k
    return bit_writer_.bytes_written();
584
12.1k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
566
136k
int RleEncoder<T>::Flush() {
567
136k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
568
136k
        bool all_repeat = literal_count_ == 0 &&
569
136k
                          (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
136k
        if (repeat_count_ > 0 && all_repeat) {
572
98.5k
            FlushRepeatedRun();
573
98.5k
        } else {
574
38.3k
            literal_count_ += num_buffered_values_;
575
38.3k
            FlushLiteralRun(true);
576
38.3k
            repeat_count_ = 0;
577
38.3k
        }
578
136k
    }
579
136k
    bit_writer_.Flush();
580
136k
    DCHECK_EQ(num_buffered_values_, 0);
581
136k
    DCHECK_EQ(literal_count_, 0);
582
    DCHECK_EQ(repeat_count_, 0);
583
136k
    return bit_writer_.bytes_written();
584
136k
}
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
38.5k
void RleEncoder<T>::Clear() {
588
38.5k
    current_value_ = 0;
589
38.5k
    repeat_count_ = 0;
590
38.5k
    num_buffered_values_ = 0;
591
38.5k
    literal_count_ = 0;
592
38.5k
    literal_indicator_byte_idx_ = -1;
593
38.5k
    bit_writer_.Clear();
594
38.5k
}
_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
466k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
669
466k
        Reset(buffer, buffer_len, bit_width);
670
466k
    }
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.54M
    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.38M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
751
3.38M
    int32_t num_to_output =
752
3.38M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
753
3.38M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
754
3.38M
    literal_buffer_pos_ += num_to_output;
755
3.38M
    literal_count_ -= num_to_output;
756
3.38M
    return num_to_output;
757
3.38M
}
758
759
template <typename T>
760
466k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
761
466k
    bit_reader_.Reset(buffer, buffer_len);
762
466k
    bit_width_ = bit_width;
763
466k
    repeat_count_ = 0;
764
466k
    literal_count_ = 0;
765
466k
    num_buffered_literals_ = 0;
766
466k
    literal_buffer_pos_ = 0;
767
466k
}
768
769
template <typename T>
770
5.79M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
771
5.79M
    if (repeat_count_ > 0) return repeat_count_;
772
5.78M
    if (literal_count_ == 0) NextCounts();
773
5.78M
    return repeat_count_;
774
5.79M
}
775
776
template <typename T>
777
5.66M
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.66M
    uint32_t indicator_value = 0;
781
5.66M
    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.66M
    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.66M
    uint32_t run_len = indicator_value >> 1;
791
5.66M
    if (is_literal) {
792
        // Use int64_t to avoid overflowing multiplication.
793
3.42M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
794
3.42M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
795
3.42M
        literal_count_ = cast_set<int32_t>(literal_count);
796
3.42M
    } else {
797
2.23M
        if (UNLIKELY(run_len == 0)) return;
798
2.23M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
799
2.23M
        if (UNLIKELY(!result)) return;
800
2.23M
        repeat_count_ = run_len;
801
2.23M
    }
802
5.66M
}
803
804
template <typename T>
805
2.24M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
806
2.24M
    repeat_count_ -= num_repeats_to_consume;
807
2.24M
    return repeated_value_;
808
2.24M
}
809
810
template <typename T>
811
3.54M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
812
3.54M
    if (literal_count_ > 0) return literal_count_;
813
18.4E
    if (repeat_count_ == 0) NextCounts();
814
18.4E
    return literal_count_;
815
3.54M
}
816
817
template <typename T>
818
3.54M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
819
3.54M
    int32_t num_consumed = 0;
820
    // Copy any buffered literals left over from previous calls.
821
3.54M
    if (HaveBufferedLiterals()) {
822
96.6k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
823
96.6k
    }
824
825
3.54M
    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.54M
    int32_t num_to_bypass =
830
3.54M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
831
3.54M
    if (num_to_bypass > 0) {
832
2.40M
        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.40M
        if (num_read < num_to_bypass) return false;
835
2.40M
        literal_count_ -= num_to_bypass;
836
2.40M
        num_consumed += num_to_bypass;
837
2.40M
        num_remaining = num_literals_to_consume - num_consumed;
838
2.40M
    }
839
840
3.54M
    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.28M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
844
3.28M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
845
3.28M
    }
846
3.54M
    return true;
847
3.54M
}
848
849
template <typename T>
850
3.28M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
851
3.28M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
852
3.28M
    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.28M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
855
3.28M
    literal_buffer_pos_ = 0;
856
3.28M
    return true;
857
3.28M
}
858
859
template <typename T>
860
615k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
861
615k
    uint32_t num_consumed = 0;
862
6.40M
    while (num_consumed < batch_num) {
863
        // Add RLE encoded values by repeating the current value this number of times.
864
5.79M
        uint32_t num_repeats = NextNumRepeats();
865
5.79M
        if (num_repeats > 0) {
866
2.24M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
867
2.24M
            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.24M
            num_consumed += num_repeats_to_set;
872
2.24M
            continue;
873
2.24M
        }
874
875
        // Add remaining literal values, if any.
876
3.54M
        uint32_t num_literals = NextNumLiterals();
877
3.54M
        if (num_literals == 0) {
878
0
            break;
879
0
        }
880
3.54M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
881
3.54M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
882
0
            return 0;
883
0
        }
884
3.54M
        num_consumed += num_literals_to_set;
885
3.54M
    }
886
615k
    return num_consumed;
887
615k
}
888
#include "common/compile_check_end.h"
889
} // namespace doris