Coverage Report

Created: 2026-07-17 15:05

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