Coverage Report

Created: 2026-07-24 15:34

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
340k
            : bit_reader_(buffer, buffer_len),
89
340k
              bit_width_(bit_width),
90
340k
              current_value_(0),
91
340k
              repeat_count_(0),
92
340k
              literal_count_(0),
93
340k
              rewind_state_(CANT_REWIND) {
94
340k
        DCHECK_GE(bit_width_, 1);
95
340k
        DCHECK_LE(bit_width_, 64);
96
340k
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
88
205k
            : bit_reader_(buffer, buffer_len),
89
205k
              bit_width_(bit_width),
90
205k
              current_value_(0),
91
205k
              repeat_count_(0),
92
205k
              literal_count_(0),
93
205k
              rewind_state_(CANT_REWIND) {
94
205k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
205k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
88
30.9k
            : bit_reader_(buffer, buffer_len),
89
30.9k
              bit_width_(bit_width),
90
30.9k
              current_value_(0),
91
30.9k
              repeat_count_(0),
92
30.9k
              literal_count_(0),
93
30.9k
              rewind_state_(CANT_REWIND) {
94
30.9k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
30.9k
    }
_ZN5doris10RleDecoderIsEC2EPKhii
Line
Count
Source
88
103k
            : bit_reader_(buffer, buffer_len),
89
103k
              bit_width_(bit_width),
90
103k
              current_value_(0),
91
103k
              repeat_count_(0),
92
103k
              literal_count_(0),
93
103k
              rewind_state_(CANT_REWIND) {
94
103k
        DCHECK_GE(bit_width_, 1);
95
        DCHECK_LE(bit_width_, 64);
96
103k
    }
97
98
38.1M
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
98
38.0M
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
98
29.0k
    RleDecoder() {}
_ZN5doris10RleDecoderIsEC2Ev
Line
Count
Source
98
55.8k
    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
634k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
634k
        DCHECK_GE(bit_width_, 1);
155
634k
        DCHECK_LE(bit_width_, 64);
156
634k
        Clear();
157
634k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
153
14.0k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
14.0k
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
14.0k
        Clear();
157
14.0k
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
153
620k
            : bit_width_(bit_width), bit_writer_(buffer) {
154
620k
        DCHECK_GE(bit_width_, 1);
155
        DCHECK_LE(bit_width_, 64);
156
620k
        Clear();
157
620k
    }
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
119k
    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
13.6M
bool RleDecoder<T>::ReadHeader() {
232
13.6M
    DCHECK(bit_reader_.is_initialized());
233
13.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
656k
        uint32_t indicator_value = 0;
237
656k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
656k
        if (!result) [[unlikely]] {
239
11.8k
            return false;
240
11.8k
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
644k
        bool is_literal = indicator_value & 1;
244
644k
        if (is_literal) {
245
291k
            literal_count_ = (indicator_value >> 1) * 8;
246
291k
            DCHECK_GT(literal_count_, 0);
247
352k
        } else {
248
352k
            repeat_count_ = indicator_value >> 1;
249
352k
            DCHECK_GT(repeat_count_, 0);
250
352k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
352k
                                                     reinterpret_cast<T*>(&current_value_));
252
352k
            DCHECK(result1);
253
352k
        }
254
644k
    }
255
13.6M
    return true;
256
13.6M
}
_ZN5doris10RleDecoderIsE10ReadHeaderEv
Line
Count
Source
231
1.22M
bool RleDecoder<T>::ReadHeader() {
232
1.22M
    DCHECK(bit_reader_.is_initialized());
233
1.22M
    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
166k
        uint32_t indicator_value = 0;
237
166k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
166k
        if (!result) [[unlikely]] {
239
8
            return false;
240
8
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
166k
        bool is_literal = indicator_value & 1;
244
166k
        if (is_literal) {
245
67.7k
            literal_count_ = (indicator_value >> 1) * 8;
246
67.7k
            DCHECK_GT(literal_count_, 0);
247
98.9k
        } else {
248
98.9k
            repeat_count_ = indicator_value >> 1;
249
98.9k
            DCHECK_GT(repeat_count_, 0);
250
98.9k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
98.9k
                                                     reinterpret_cast<T*>(&current_value_));
252
98.9k
            DCHECK(result1);
253
98.9k
        }
254
166k
    }
255
1.22M
    return true;
256
1.22M
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
231
4.46M
bool RleDecoder<T>::ReadHeader() {
232
4.46M
    DCHECK(bit_reader_.is_initialized());
233
4.46M
    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
374k
        uint32_t indicator_value = 0;
237
374k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
374k
        if (!result) [[unlikely]] {
239
11.8k
            return false;
240
11.8k
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
362k
        bool is_literal = indicator_value & 1;
244
362k
        if (is_literal) {
245
151k
            literal_count_ = (indicator_value >> 1) * 8;
246
151k
            DCHECK_GT(literal_count_, 0);
247
211k
        } else {
248
211k
            repeat_count_ = indicator_value >> 1;
249
211k
            DCHECK_GT(repeat_count_, 0);
250
211k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
211k
                                                     reinterpret_cast<T*>(&current_value_));
252
211k
            DCHECK(result1);
253
211k
        }
254
362k
    }
255
4.45M
    return true;
256
4.46M
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
231
7.96M
bool RleDecoder<T>::ReadHeader() {
232
7.96M
    DCHECK(bit_reader_.is_initialized());
233
7.96M
    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
114k
        uint32_t indicator_value = 0;
237
114k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
238
114k
        if (!result) [[unlikely]] {
239
0
            return false;
240
0
        }
241
242
        // lsb indicates if it is a literal run or repeated run
243
114k
        bool is_literal = indicator_value & 1;
244
114k
        if (is_literal) {
245
72.3k
            literal_count_ = (indicator_value >> 1) * 8;
246
72.3k
            DCHECK_GT(literal_count_, 0);
247
72.3k
        } else {
248
42.5k
            repeat_count_ = indicator_value >> 1;
249
42.5k
            DCHECK_GT(repeat_count_, 0);
250
42.5k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
251
42.5k
                                                     reinterpret_cast<T*>(&current_value_));
252
42.5k
            DCHECK(result1);
253
42.5k
        }
254
114k
    }
255
7.96M
    return true;
256
7.96M
}
257
258
template <typename T>
259
8.62M
bool RleDecoder<T>::Get(T* val) {
260
8.62M
    DCHECK(bit_reader_.is_initialized());
261
8.62M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
8.62M
    if (repeat_count_ > 0) [[likely]] {
266
1.12M
        *val = cast_set<T>(current_value_);
267
1.12M
        --repeat_count_;
268
1.12M
        rewind_state_ = REWIND_RUN;
269
7.50M
    } else {
270
7.50M
        DCHECK(literal_count_ > 0);
271
7.50M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
7.50M
        DCHECK(result);
273
7.50M
        --literal_count_;
274
7.50M
        rewind_state_ = REWIND_LITERAL;
275
7.50M
    }
276
277
8.62M
    return true;
278
8.62M
}
_ZN5doris10RleDecoderIsE3GetEPs
Line
Count
Source
259
1.01M
bool RleDecoder<T>::Get(T* val) {
260
1.01M
    DCHECK(bit_reader_.is_initialized());
261
1.01M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
1.01M
    if (repeat_count_ > 0) [[likely]] {
266
900k
        *val = cast_set<T>(current_value_);
267
900k
        --repeat_count_;
268
900k
        rewind_state_ = REWIND_RUN;
269
900k
    } else {
270
111k
        DCHECK(literal_count_ > 0);
271
111k
        bool result = bit_reader_.GetValue(bit_width_, val);
272
111k
        DCHECK(result);
273
111k
        --literal_count_;
274
111k
        rewind_state_ = REWIND_LITERAL;
275
111k
    }
276
277
1.01M
    return true;
278
1.01M
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
259
7.61M
bool RleDecoder<T>::Get(T* val) {
260
7.61M
    DCHECK(bit_reader_.is_initialized());
261
7.61M
    if (!ReadHeader()) [[unlikely]] {
262
0
        return false;
263
0
    }
264
265
7.61M
    if (repeat_count_ > 0) [[likely]] {
266
226k
        *val = cast_set<T>(current_value_);
267
226k
        --repeat_count_;
268
226k
        rewind_state_ = REWIND_RUN;
269
7.39M
    } else {
270
7.39M
        DCHECK(literal_count_ > 0);
271
7.39M
        bool result = bit_reader_.GetValue(bit_width_, val);
272
7.39M
        DCHECK(result);
273
7.39M
        --literal_count_;
274
7.39M
        rewind_state_ = REWIND_LITERAL;
275
7.39M
    }
276
277
7.61M
    return true;
278
7.61M
}
279
280
template <typename T>
281
3.04k
void RleDecoder<T>::RewindOne() {
282
3.04k
    DCHECK(bit_reader_.is_initialized());
283
284
3.04k
    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
650
    case REWIND_RUN:
289
650
        ++repeat_count_;
290
650
        break;
291
2.39k
    case REWIND_LITERAL: {
292
2.39k
        bit_reader_.Rewind(bit_width_);
293
2.39k
        ++literal_count_;
294
2.39k
        break;
295
0
    }
296
3.04k
    }
297
298
3.04k
    rewind_state_ = CANT_REWIND;
299
3.04k
}
300
301
template <typename T>
302
4.36M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
4.36M
    DCHECK(bit_reader_.is_initialized());
304
4.36M
    DCHECK_GT(max_run, 0);
305
4.36M
    size_t ret = 0;
306
4.36M
    size_t rem = max_run;
307
4.55M
    while (ReadHeader()) {
308
4.53M
        if (repeat_count_ > 0) [[likely]] {
309
3.54M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
31.7k
                return ret;
311
31.7k
            }
312
3.50M
            *val = cast_set<T>(current_value_);
313
3.50M
            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.40M
                repeat_count_ -= rem;
317
3.40M
                ret += rem;
318
3.40M
                return ret;
319
3.40M
            }
320
102k
            ret += repeat_count_;
321
102k
            rem -= repeat_count_;
322
102k
            repeat_count_ = 0;
323
995k
        } else {
324
995k
            DCHECK(literal_count_ > 0);
325
995k
            if (ret == 0) {
326
918k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
918k
                DCHECK(has_more);
328
918k
                literal_count_--;
329
918k
                ret++;
330
918k
                rem--;
331
918k
            }
332
333
2.33M
            while (literal_count_ > 0) {
334
2.24M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
2.24M
                DCHECK(result);
336
2.24M
                if (current_value_ != *val || rem == 0) {
337
906k
                    bit_reader_.Rewind(bit_width_);
338
906k
                    return ret;
339
906k
                }
340
1.33M
                ret++;
341
1.33M
                rem--;
342
1.33M
                literal_count_--;
343
1.33M
            }
344
995k
        }
345
4.53M
    }
346
16.8k
    return ret;
347
4.36M
}
_ZN5doris10RleDecoderIsE10GetNextRunEPsm
Line
Count
Source
302
160k
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
160k
    DCHECK(bit_reader_.is_initialized());
304
160k
    DCHECK_GT(max_run, 0);
305
160k
    size_t ret = 0;
306
160k
    size_t rem = max_run;
307
203k
    while (ReadHeader()) {
308
203k
        if (repeat_count_ > 0) [[likely]] {
309
94.0k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
934
                return ret;
311
934
            }
312
93.1k
            *val = cast_set<T>(current_value_);
313
93.1k
            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
71.5k
                repeat_count_ -= rem;
317
71.5k
                ret += rem;
318
71.5k
                return ret;
319
71.5k
            }
320
21.5k
            ret += repeat_count_;
321
21.5k
            rem -= repeat_count_;
322
21.5k
            repeat_count_ = 0;
323
109k
        } else {
324
109k
            DCHECK(literal_count_ > 0);
325
109k
            if (ret == 0) {
326
87.5k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
87.5k
                DCHECK(has_more);
328
87.5k
                literal_count_--;
329
87.5k
                ret++;
330
87.5k
                rem--;
331
87.5k
            }
332
333
395k
            while (literal_count_ > 0) {
334
374k
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
374k
                DCHECK(result);
336
374k
                if (current_value_ != *val || rem == 0) {
337
87.5k
                    bit_reader_.Rewind(bit_width_);
338
87.5k
                    return ret;
339
87.5k
                }
340
286k
                ret++;
341
286k
                rem--;
342
286k
                literal_count_--;
343
286k
            }
344
109k
        }
345
203k
    }
346
8
    return ret;
347
160k
}
_ZN5doris10RleDecoderIbE10GetNextRunEPbm
Line
Count
Source
302
4.20M
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
303
4.20M
    DCHECK(bit_reader_.is_initialized());
304
4.20M
    DCHECK_GT(max_run, 0);
305
4.20M
    size_t ret = 0;
306
4.20M
    size_t rem = max_run;
307
4.35M
    while (ReadHeader()) {
308
4.33M
        if (repeat_count_ > 0) [[likely]] {
309
3.44M
            if (ret > 0 && *val != current_value_) [[unlikely]] {
310
30.8k
                return ret;
311
30.8k
            }
312
3.41M
            *val = cast_set<T>(current_value_);
313
3.41M
            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.33M
                repeat_count_ -= rem;
317
3.33M
                ret += rem;
318
3.33M
                return ret;
319
3.33M
            }
320
80.5k
            ret += repeat_count_;
321
80.5k
            rem -= repeat_count_;
322
80.5k
            repeat_count_ = 0;
323
886k
        } else {
324
886k
            DCHECK(literal_count_ > 0);
325
886k
            if (ret == 0) {
326
831k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
327
831k
                DCHECK(has_more);
328
831k
                literal_count_--;
329
831k
                ret++;
330
831k
                rem--;
331
831k
            }
332
333
1.93M
            while (literal_count_ > 0) {
334
1.86M
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
335
1.86M
                DCHECK(result);
336
1.86M
                if (current_value_ != *val || rem == 0) {
337
819k
                    bit_reader_.Rewind(bit_width_);
338
819k
                    return ret;
339
819k
                }
340
1.04M
                ret++;
341
1.04M
                rem--;
342
1.04M
                literal_count_--;
343
1.04M
            }
344
886k
        }
345
4.33M
    }
346
16.8k
    return ret;
347
4.20M
}
348
349
template <typename T>
350
16.5k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
16.5k
    size_t read_num = 0;
352
42.7k
    while (read_num < num_values) {
353
26.2k
        size_t read_this_time = num_values - read_num;
354
355
26.2k
        if (LIKELY(repeat_count_ > 0)) {
356
9.66k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
9.66k
            std::fill(values, values + read_this_time, current_value_);
358
9.66k
            values += read_this_time;
359
9.66k
            repeat_count_ -= read_this_time;
360
9.66k
            read_num += read_this_time;
361
16.5k
        } else if (literal_count_ > 0) {
362
5.06k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
18.8k
            for (int i = 0; i < read_this_time; ++i) {
364
13.8k
                bool result = bit_reader_.GetValue(bit_width_, values);
365
13.8k
                DCHECK(result);
366
13.8k
                values++;
367
13.8k
            }
368
5.06k
            literal_count_ -= read_this_time;
369
5.06k
            read_num += read_this_time;
370
11.5k
        } else {
371
11.5k
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
11.5k
        }
375
26.2k
    }
376
16.5k
    return read_num;
377
16.5k
}
_ZN5doris10RleDecoderIsE10get_valuesEPsm
Line
Count
Source
350
16.5k
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
351
16.5k
    size_t read_num = 0;
352
42.7k
    while (read_num < num_values) {
353
26.2k
        size_t read_this_time = num_values - read_num;
354
355
26.2k
        if (LIKELY(repeat_count_ > 0)) {
356
9.66k
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
357
9.66k
            std::fill(values, values + read_this_time, current_value_);
358
9.66k
            values += read_this_time;
359
9.66k
            repeat_count_ -= read_this_time;
360
9.66k
            read_num += read_this_time;
361
16.5k
        } else if (literal_count_ > 0) {
362
5.05k
            read_this_time = std::min((size_t)literal_count_, read_this_time);
363
18.8k
            for (int i = 0; i < read_this_time; ++i) {
364
13.8k
                bool result = bit_reader_.GetValue(bit_width_, values);
365
13.8k
                DCHECK(result);
366
13.8k
                values++;
367
13.8k
            }
368
5.05k
            literal_count_ -= read_this_time;
369
5.05k
            read_num += read_this_time;
370
11.4k
        } else {
371
11.4k
            if (!ReadHeader()) {
372
0
                return read_num;
373
0
            }
374
11.4k
        }
375
26.2k
    }
376
16.5k
    return read_num;
377
16.5k
}
_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.25M
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.25M
    DCHECK(bit_reader_.is_initialized());
400
401
1.25M
    size_t set_count = 0;
402
1.68M
    while (to_skip > 0) {
403
422k
        bool result = ReadHeader();
404
422k
        DCHECK(result);
405
406
422k
        if (repeat_count_ > 0) [[likely]] {
407
99.3k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
99.3k
            repeat_count_ -= nskip;
409
99.3k
            to_skip -= nskip;
410
99.3k
            if (current_value_ != 0) {
411
32.8k
                set_count += nskip;
412
32.8k
            }
413
322k
        } else {
414
322k
            DCHECK(literal_count_ > 0);
415
322k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
322k
            literal_count_ -= nskip;
417
322k
            to_skip -= nskip;
418
16.4M
            for (; nskip > 0; nskip--) {
419
16.1M
                T value = 0;
420
16.1M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
16.1M
                DCHECK(result1);
422
16.1M
                if (value != 0) {
423
8.32M
                    set_count++;
424
8.32M
                }
425
16.1M
            }
426
322k
        }
427
422k
    }
428
1.25M
    return set_count;
429
1.25M
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
398
108k
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
108k
    DCHECK(bit_reader_.is_initialized());
400
401
108k
    size_t set_count = 0;
402
218k
    while (to_skip > 0) {
403
110k
        bool result = ReadHeader();
404
110k
        DCHECK(result);
405
406
110k
        if (repeat_count_ > 0) [[likely]] {
407
76.3k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
76.3k
            repeat_count_ -= nskip;
409
76.3k
            to_skip -= nskip;
410
76.3k
            if (current_value_ != 0) {
411
21.7k
                set_count += nskip;
412
21.7k
            }
413
76.3k
        } else {
414
34.1k
            DCHECK(literal_count_ > 0);
415
34.1k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
34.1k
            literal_count_ -= nskip;
417
34.1k
            to_skip -= nskip;
418
148k
            for (; nskip > 0; nskip--) {
419
114k
                T value = 0;
420
114k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
114k
                DCHECK(result1);
422
114k
                if (value != 0) {
423
38.4k
                    set_count++;
424
38.4k
                }
425
114k
            }
426
34.1k
        }
427
110k
    }
428
108k
    return set_count;
429
108k
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
398
1.15M
size_t RleDecoder<T>::Skip(size_t to_skip) {
399
1.15M
    DCHECK(bit_reader_.is_initialized());
400
401
1.15M
    size_t set_count = 0;
402
1.46M
    while (to_skip > 0) {
403
311k
        bool result = ReadHeader();
404
311k
        DCHECK(result);
405
406
311k
        if (repeat_count_ > 0) [[likely]] {
407
23.0k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
408
23.0k
            repeat_count_ -= nskip;
409
23.0k
            to_skip -= nskip;
410
23.0k
            if (current_value_ != 0) {
411
11.0k
                set_count += nskip;
412
11.0k
            }
413
288k
        } else {
414
288k
            DCHECK(literal_count_ > 0);
415
288k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
416
288k
            literal_count_ -= nskip;
417
288k
            to_skip -= nskip;
418
16.3M
            for (; nskip > 0; nskip--) {
419
16.0M
                T value = 0;
420
16.0M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
421
16.0M
                DCHECK(result1);
422
16.0M
                if (value != 0) {
423
8.28M
                    set_count++;
424
8.28M
                }
425
16.0M
            }
426
288k
        }
427
311k
    }
428
1.15M
    return set_count;
429
1.15M
}
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
8.84M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
8.84M
    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
8.84M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
1.02M
        repeat_count_ += run_length;
441
1.02M
        return;
442
1.02M
    }
443
444
    // Handle run_length > 1 more efficiently
445
17.6M
    while (run_length > 0) {
446
10.6M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
5.18M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
21.8M
            for (size_t i = 0; i < to_buffer; ++i) {
450
16.6M
                buffered_values_[num_buffered_values_++] = value;
451
16.6M
                ++repeat_count_;
452
16.6M
            }
453
5.18M
            run_length -= to_buffer;
454
5.18M
            if (num_buffered_values_ == 8) {
455
2.14M
                DCHECK_EQ(literal_count_ % 8, 0);
456
2.14M
                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
2.14M
                if (repeat_count_ >= 8 && run_length > 0) {
460
842k
                    repeat_count_ += run_length;
461
842k
                    return;
462
842k
                }
463
2.14M
            }
464
5.51M
        } else {
465
            // Value changed
466
5.51M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
707k
                DCHECK_EQ(literal_count_, 0);
470
707k
                FlushRepeatedRun();
471
707k
            }
472
5.51M
            repeat_count_ = 1;
473
5.51M
            current_value_ = value;
474
475
5.51M
            buffered_values_[num_buffered_values_++] = value;
476
5.51M
            --run_length;
477
5.51M
            if (num_buffered_values_ == 8) {
478
507k
                DCHECK_EQ(literal_count_ % 8, 0);
479
507k
                FlushBufferedValues(false);
480
507k
            }
481
5.51M
        }
482
10.6M
    }
483
7.81M
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
434
4.04M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
4.04M
    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.04M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
371k
        repeat_count_ += run_length;
441
371k
        return;
442
371k
    }
443
444
    // Handle run_length > 1 more efficiently
445
7.33M
    while (run_length > 0) {
446
3.67M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
1.83M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
3.66M
            for (size_t i = 0; i < to_buffer; ++i) {
450
1.83M
                buffered_values_[num_buffered_values_++] = value;
451
1.83M
                ++repeat_count_;
452
1.83M
            }
453
1.83M
            run_length -= to_buffer;
454
1.83M
            if (num_buffered_values_ == 8) {
455
230k
                DCHECK_EQ(literal_count_ % 8, 0);
456
230k
                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
230k
                if (repeat_count_ >= 8 && run_length > 0) {
460
3
                    repeat_count_ += run_length;
461
3
                    return;
462
3
                }
463
230k
            }
464
1.83M
        } else {
465
            // Value changed
466
1.83M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
5.40k
                DCHECK_EQ(literal_count_, 0);
470
5.40k
                FlushRepeatedRun();
471
5.40k
            }
472
1.83M
            repeat_count_ = 1;
473
1.83M
            current_value_ = value;
474
475
1.83M
            buffered_values_[num_buffered_values_++] = value;
476
1.83M
            --run_length;
477
1.83M
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
226k
                FlushBufferedValues(false);
480
226k
            }
481
1.83M
        }
482
3.67M
    }
483
3.66M
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
434
4.79M
void RleEncoder<T>::Put(T value, size_t run_length) {
435
4.79M
    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.79M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
440
651k
        repeat_count_ += run_length;
441
651k
        return;
442
651k
    }
443
444
    // Handle run_length > 1 more efficiently
445
10.3M
    while (run_length > 0) {
446
7.02M
        if (current_value_ == value) [[likely]] {
447
            // Need to buffer values until we reach 8
448
3.34M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
449
18.1M
            for (size_t i = 0; i < to_buffer; ++i) {
450
14.8M
                buffered_values_[num_buffered_values_++] = value;
451
14.8M
                ++repeat_count_;
452
14.8M
            }
453
3.34M
            run_length -= to_buffer;
454
3.34M
            if (num_buffered_values_ == 8) {
455
1.91M
                DCHECK_EQ(literal_count_ % 8, 0);
456
1.91M
                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
1.91M
                if (repeat_count_ >= 8 && run_length > 0) {
460
842k
                    repeat_count_ += run_length;
461
842k
                    return;
462
842k
                }
463
1.91M
            }
464
3.67M
        } else {
465
            // Value changed
466
3.67M
            if (repeat_count_ >= 8) {
467
                // We had a run that was long enough but it has ended.  Flush the
468
                // current repeated run.
469
702k
                DCHECK_EQ(literal_count_, 0);
470
702k
                FlushRepeatedRun();
471
702k
            }
472
3.67M
            repeat_count_ = 1;
473
3.67M
            current_value_ = value;
474
475
3.67M
            buffered_values_[num_buffered_values_++] = value;
476
3.67M
            --run_length;
477
3.67M
            if (num_buffered_values_ == 8) {
478
                DCHECK_EQ(literal_count_ % 8, 0);
479
281k
                FlushBufferedValues(false);
480
281k
            }
481
3.67M
        }
482
7.02M
    }
483
4.14M
}
484
485
template <typename T>
486
2.47M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
2.47M
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
732k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
732k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
732k
    }
492
493
    // Write all the buffered values as bit packed literals
494
16.5M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
14.0M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
14.0M
    }
497
2.47M
    num_buffered_values_ = 0;
498
499
2.47M
    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
733k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
733k
        int32_t indicator_value = (num_groups << 1) | 1;
506
733k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
733k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
733k
                cast_set<uint8_t>(indicator_value);
509
733k
        literal_indicator_byte_idx_ = -1;
510
733k
        literal_count_ = 0;
511
733k
    }
512
2.47M
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
486
456k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
456k
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
12.7k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
12.7k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
12.7k
    }
492
493
    // Write all the buffered values as bit packed literals
494
4.05M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
3.60M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
3.60M
    }
497
456k
    num_buffered_values_ = 0;
498
499
456k
    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
12.7k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
12.7k
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
12.7k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
12.7k
                cast_set<uint8_t>(indicator_value);
509
12.7k
        literal_indicator_byte_idx_ = -1;
510
12.7k
        literal_count_ = 0;
511
12.7k
    }
512
456k
}
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
486
2.01M
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
487
2.01M
    if (literal_indicator_byte_idx_ < 0) {
488
        // The literal indicator byte has not been reserved yet, get one now.
489
719k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
490
719k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
491
719k
    }
492
493
    // Write all the buffered values as bit packed literals
494
12.5M
    for (int i = 0; i < num_buffered_values_; ++i) {
495
10.4M
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
496
10.4M
    }
497
2.01M
    num_buffered_values_ = 0;
498
499
2.01M
    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
720k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
505
720k
        int32_t indicator_value = (num_groups << 1) | 1;
506
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
507
720k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
508
720k
                cast_set<uint8_t>(indicator_value);
509
720k
        literal_indicator_byte_idx_ = -1;
510
720k
        literal_count_ = 0;
511
720k
    }
512
2.01M
}
513
514
template <typename T>
515
860k
void RleEncoder<T>::FlushRepeatedRun() {
516
860k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
860k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
860k
    bit_writer_.PutVlqInt(indicator_value);
520
860k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
860k
    num_buffered_values_ = 0;
522
860k
    repeat_count_ = 0;
523
860k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
515
14.5k
void RleEncoder<T>::FlushRepeatedRun() {
516
14.5k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
14.5k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
14.5k
    bit_writer_.PutVlqInt(indicator_value);
520
14.5k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
14.5k
    num_buffered_values_ = 0;
522
14.5k
    repeat_count_ = 0;
523
14.5k
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
515
845k
void RleEncoder<T>::FlushRepeatedRun() {
516
845k
    DCHECK_GT(repeat_count_, 0);
517
    // The lsb of 0 indicates this is a repeated run
518
845k
    int32_t indicator_value = repeat_count_ << 1 | 0;
519
845k
    bit_writer_.PutVlqInt(indicator_value);
520
845k
    bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
521
845k
    num_buffered_values_ = 0;
522
845k
    repeat_count_ = 0;
523
845k
}
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
2.65M
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
2.65M
    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
907k
        num_buffered_values_ = 0;
533
907k
        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
673k
            DCHECK_EQ(literal_count_ % 8, 0);
537
673k
            DCHECK_EQ(repeat_count_, 8);
538
673k
            FlushLiteralRun(true);
539
673k
        }
540
907k
        DCHECK_EQ(literal_count_, 0);
541
907k
        return;
542
907k
    }
543
544
1.74M
    literal_count_ += num_buffered_values_;
545
1.74M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
1.74M
    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
6.14k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
6.14k
        FlushLiteralRun(true);
551
1.73M
    } else {
552
1.73M
        FlushLiteralRun(done);
553
1.73M
    }
554
1.74M
    repeat_count_ = 0;
555
1.74M
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
528
456k
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
456k
    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
6.87k
        num_buffered_values_ = 0;
533
6.87k
        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
4.14k
            DCHECK_EQ(literal_count_ % 8, 0);
537
4.14k
            DCHECK_EQ(repeat_count_, 8);
538
4.14k
            FlushLiteralRun(true);
539
4.14k
        }
540
6.87k
        DCHECK_EQ(literal_count_, 0);
541
6.87k
        return;
542
6.87k
    }
543
544
449k
    literal_count_ += num_buffered_values_;
545
449k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
449k
    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.31k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
5.31k
        FlushLiteralRun(true);
551
443k
    } else {
552
443k
        FlushLiteralRun(done);
553
443k
    }
554
449k
    repeat_count_ = 0;
555
449k
}
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
528
2.19M
void RleEncoder<T>::FlushBufferedValues(bool done) {
529
2.19M
    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
900k
        num_buffered_values_ = 0;
533
900k
        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
669k
            DCHECK_EQ(literal_count_ % 8, 0);
537
669k
            DCHECK_EQ(repeat_count_, 8);
538
669k
            FlushLiteralRun(true);
539
669k
        }
540
900k
        DCHECK_EQ(literal_count_, 0);
541
900k
        return;
542
900k
    }
543
544
1.29M
    literal_count_ += num_buffered_values_;
545
1.29M
    int num_groups = BitUtil::Ceil(literal_count_, 8);
546
1.29M
    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
825
        DCHECK_GE(literal_indicator_byte_idx_, 0);
550
825
        FlushLiteralRun(true);
551
1.29M
    } else {
552
1.29M
        FlushLiteralRun(done);
553
1.29M
    }
554
1.29M
    repeat_count_ = 0;
555
1.29M
}
556
557
template <typename T>
558
27.1k
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
559
135k
    for (int i = 0; i < num_bytes; ++i) {
560
108k
        bit_writer_.PutValue(val, 8);
561
108k
    }
562
27.1k
}
563
564
template <typename T>
565
206k
int RleEncoder<T>::Flush() {
566
206k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
205k
        bool all_repeat = literal_count_ == 0 &&
568
205k
                          (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
205k
        if (repeat_count_ > 0 && all_repeat) {
571
152k
            FlushRepeatedRun();
572
152k
        } else {
573
53.0k
            literal_count_ += num_buffered_values_;
574
53.0k
            FlushLiteralRun(true);
575
53.0k
            repeat_count_ = 0;
576
53.0k
        }
577
205k
    }
578
206k
    bit_writer_.Flush();
579
206k
    DCHECK_EQ(num_buffered_values_, 0);
580
206k
    DCHECK_EQ(literal_count_, 0);
581
206k
    DCHECK_EQ(repeat_count_, 0);
582
206k
    return bit_writer_.bytes_written();
583
206k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
565
13.0k
int RleEncoder<T>::Flush() {
566
13.0k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
12.4k
        bool all_repeat = literal_count_ == 0 &&
568
12.4k
                          (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
12.4k
        if (repeat_count_ > 0 && all_repeat) {
571
9.14k
            FlushRepeatedRun();
572
9.14k
        } else {
573
3.29k
            literal_count_ += num_buffered_values_;
574
3.29k
            FlushLiteralRun(true);
575
3.29k
            repeat_count_ = 0;
576
3.29k
        }
577
12.4k
    }
578
13.0k
    bit_writer_.Flush();
579
13.0k
    DCHECK_EQ(num_buffered_values_, 0);
580
13.0k
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
13.0k
    return bit_writer_.bytes_written();
583
13.0k
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
565
193k
int RleEncoder<T>::Flush() {
566
193k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
567
193k
        bool all_repeat = literal_count_ == 0 &&
568
193k
                          (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
193k
        if (repeat_count_ > 0 && all_repeat) {
571
143k
            FlushRepeatedRun();
572
143k
        } else {
573
49.7k
            literal_count_ += num_buffered_values_;
574
49.7k
            FlushLiteralRun(true);
575
49.7k
            repeat_count_ = 0;
576
49.7k
        }
577
193k
    }
578
193k
    bit_writer_.Flush();
579
193k
    DCHECK_EQ(num_buffered_values_, 0);
580
193k
    DCHECK_EQ(literal_count_, 0);
581
    DCHECK_EQ(repeat_count_, 0);
582
193k
    return bit_writer_.bytes_written();
583
193k
}
584
585
template <typename T>
586
1.28M
void RleEncoder<T>::Clear() {
587
1.28M
    current_value_ = 0;
588
1.28M
    repeat_count_ = 0;
589
1.28M
    num_buffered_values_ = 0;
590
1.28M
    literal_count_ = 0;
591
1.28M
    literal_indicator_byte_idx_ = -1;
592
1.28M
    bit_writer_.Clear();
593
1.28M
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
586
41.1k
void RleEncoder<T>::Clear() {
587
41.1k
    current_value_ = 0;
588
41.1k
    repeat_count_ = 0;
589
41.1k
    num_buffered_values_ = 0;
590
41.1k
    literal_count_ = 0;
591
41.1k
    literal_indicator_byte_idx_ = -1;
592
41.1k
    bit_writer_.Clear();
593
41.1k
}
_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
834k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
834k
        Reset(buffer, buffer_len, bit_width);
669
834k
    }
_ZN5doris15RleBatchDecoderItEC2EPhii
Line
Count
Source
667
536k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
536k
        Reset(buffer, buffer_len, bit_width);
669
536k
    }
_ZN5doris15RleBatchDecoderIjEC2EPhii
Line
Count
Source
667
298k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
298k
        Reset(buffer, buffer_len, bit_width);
669
298k
    }
_ZN5doris15RleBatchDecoderIhEC2EPhii
Line
Count
Source
667
344
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
668
344
        Reset(buffer, buffer_len, bit_width);
669
344
    }
670
671
273k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderItEC2Ev
Line
Count
Source
671
273k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderIhEC2Ev
Line
Count
Source
671
346
    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
102M
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIjE20HaveBufferedLiteralsEv
Line
Count
Source
712
37.8M
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderItE20HaveBufferedLiteralsEv
Line
Count
Source
712
64.5M
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIhE20HaveBufferedLiteralsEv
Line
Count
Source
712
397
    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
106M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
106M
    int32_t num_to_output =
751
106M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
106M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
106M
    literal_buffer_pos_ += num_to_output;
754
106M
    literal_count_ -= num_to_output;
755
106M
    return num_to_output;
756
106M
}
_ZN5doris15RleBatchDecoderIjE22OutputBufferedLiteralsEiPj
Line
Count
Source
749
42.3M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
42.3M
    int32_t num_to_output =
751
42.3M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
42.3M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
42.3M
    literal_buffer_pos_ += num_to_output;
754
42.3M
    literal_count_ -= num_to_output;
755
42.3M
    return num_to_output;
756
42.3M
}
_ZN5doris15RleBatchDecoderItE22OutputBufferedLiteralsEiPt
Line
Count
Source
749
64.4M
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
64.4M
    int32_t num_to_output =
751
64.4M
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
64.4M
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
64.4M
    literal_buffer_pos_ += num_to_output;
754
64.4M
    literal_count_ -= num_to_output;
755
64.4M
    return num_to_output;
756
64.4M
}
_ZN5doris15RleBatchDecoderIhE22OutputBufferedLiteralsEiPh
Line
Count
Source
749
394
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
750
394
    int32_t num_to_output =
751
394
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
752
394
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
753
394
    literal_buffer_pos_ += num_to_output;
754
394
    literal_count_ -= num_to_output;
755
394
    return num_to_output;
756
394
}
757
758
template <typename T>
759
834k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
834k
    bit_reader_.Reset(buffer, buffer_len);
761
834k
    bit_width_ = bit_width;
762
834k
    repeat_count_ = 0;
763
834k
    literal_count_ = 0;
764
834k
    num_buffered_literals_ = 0;
765
834k
    literal_buffer_pos_ = 0;
766
834k
}
_ZN5doris15RleBatchDecoderIjE5ResetEPhii
Line
Count
Source
759
298k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
298k
    bit_reader_.Reset(buffer, buffer_len);
761
298k
    bit_width_ = bit_width;
762
298k
    repeat_count_ = 0;
763
298k
    literal_count_ = 0;
764
298k
    num_buffered_literals_ = 0;
765
298k
    literal_buffer_pos_ = 0;
766
298k
}
_ZN5doris15RleBatchDecoderItE5ResetEPhii
Line
Count
Source
759
536k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
536k
    bit_reader_.Reset(buffer, buffer_len);
761
536k
    bit_width_ = bit_width;
762
536k
    repeat_count_ = 0;
763
536k
    literal_count_ = 0;
764
536k
    num_buffered_literals_ = 0;
765
536k
    literal_buffer_pos_ = 0;
766
536k
}
_ZN5doris15RleBatchDecoderIhE5ResetEPhii
Line
Count
Source
759
342
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
760
342
    bit_reader_.Reset(buffer, buffer_len);
761
342
    bit_width_ = bit_width;
762
342
    repeat_count_ = 0;
763
342
    literal_count_ = 0;
764
342
    num_buffered_literals_ = 0;
765
342
    literal_buffer_pos_ = 0;
766
342
}
767
768
template <typename T>
769
170M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
170M
    if (repeat_count_ > 0) return repeat_count_;
771
108M
    if (literal_count_ == 0) NextCounts();
772
108M
    return repeat_count_;
773
170M
}
_ZN5doris15RleBatchDecoderIjE14NextNumRepeatsEv
Line
Count
Source
769
42.1M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
42.1M
    if (repeat_count_ > 0) return repeat_count_;
771
41.2M
    if (literal_count_ == 0) NextCounts();
772
41.2M
    return repeat_count_;
773
42.1M
}
_ZN5doris15RleBatchDecoderItE14NextNumRepeatsEv
Line
Count
Source
769
128M
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
128M
    if (repeat_count_ > 0) return repeat_count_;
771
67.4M
    if (literal_count_ == 0) NextCounts();
772
67.4M
    return repeat_count_;
773
128M
}
_ZN5doris15RleBatchDecoderIhE14NextNumRepeatsEv
Line
Count
Source
769
691
int32_t RleBatchDecoder<T>::NextNumRepeats() {
770
691
    if (repeat_count_ > 0) return repeat_count_;
771
436
    if (literal_count_ == 0) NextCounts();
772
436
    return repeat_count_;
773
691
}
774
775
template <typename T>
776
13.2M
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
13.2M
    uint32_t indicator_value = 0;
780
13.2M
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
781
2
        return;
782
2
    }
783
784
    // lsb indicates if it is a literal run or repeated run
785
13.2M
    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
13.2M
    uint32_t run_len = indicator_value >> 1;
790
13.2M
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
6.80M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
6.80M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
6.80M
        literal_count_ = cast_set<int32_t>(literal_count);
795
6.80M
    } else {
796
6.41M
        if (UNLIKELY(run_len == 0)) return;
797
6.41M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
6.41M
        if (UNLIKELY(!result)) return;
799
6.41M
        repeat_count_ = run_len;
800
6.41M
    }
801
13.2M
}
_ZN5doris15RleBatchDecoderIjE10NextCountsEv
Line
Count
Source
776
7.28M
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
7.28M
    uint32_t indicator_value = 0;
780
7.28M
    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
7.28M
    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
7.28M
    uint32_t run_len = indicator_value >> 1;
790
7.28M
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
3.93M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
3.93M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
3.93M
        literal_count_ = cast_set<int32_t>(literal_count);
795
3.93M
    } else {
796
3.34M
        if (UNLIKELY(run_len == 0)) return;
797
3.34M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
3.34M
        if (UNLIKELY(!result)) return;
799
3.34M
        repeat_count_ = run_len;
800
3.34M
    }
801
7.28M
}
_ZN5doris15RleBatchDecoderItE10NextCountsEv
Line
Count
Source
776
5.92M
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
5.92M
    uint32_t indicator_value = 0;
780
5.92M
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
781
2
        return;
782
2
    }
783
784
    // lsb indicates if it is a literal run or repeated run
785
5.92M
    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
5.92M
    uint32_t run_len = indicator_value >> 1;
790
5.92M
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
2.86M
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
2.86M
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
2.86M
        literal_count_ = cast_set<int32_t>(literal_count);
795
3.06M
    } else {
796
3.06M
        if (UNLIKELY(run_len == 0)) return;
797
3.06M
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
3.06M
        if (UNLIKELY(!result)) return;
799
3.06M
        repeat_count_ = run_len;
800
3.06M
    }
801
5.92M
}
_ZN5doris15RleBatchDecoderIhE10NextCountsEv
Line
Count
Source
776
348
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
348
    uint32_t indicator_value = 0;
780
348
    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
348
    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
348
    uint32_t run_len = indicator_value >> 1;
790
348
    if (is_literal) {
791
        // Use int64_t to avoid overflowing multiplication.
792
309
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
793
309
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
794
309
        literal_count_ = cast_set<int32_t>(literal_count);
795
309
    } else {
796
39
        if (UNLIKELY(run_len == 0)) return;
797
39
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
798
39
        if (UNLIKELY(!result)) return;
799
39
        repeat_count_ = run_len;
800
39
    }
801
348
}
802
803
template <typename T>
804
68.8M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
68.8M
    repeat_count_ -= num_repeats_to_consume;
806
68.8M
    return repeated_value_;
807
68.8M
}
_ZN5doris15RleBatchDecoderIjE16GetRepeatedValueEi
Line
Count
Source
804
4.24M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
4.24M
    repeat_count_ -= num_repeats_to_consume;
806
4.24M
    return repeated_value_;
807
4.24M
}
_ZN5doris15RleBatchDecoderItE16GetRepeatedValueEi
Line
Count
Source
804
64.5M
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
64.5M
    repeat_count_ -= num_repeats_to_consume;
806
64.5M
    return repeated_value_;
807
64.5M
}
_ZN5doris15RleBatchDecoderIhE16GetRepeatedValueEi
Line
Count
Source
804
294
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
805
294
    repeat_count_ -= num_repeats_to_consume;
806
294
    return repeated_value_;
807
294
}
808
809
template <typename T>
810
102M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
102M
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
102M
}
_ZN5doris15RleBatchDecoderIjE15NextNumLiteralsEv
Line
Count
Source
810
37.8M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
37.8M
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
37.8M
}
_ZN5doris15RleBatchDecoderItE15NextNumLiteralsEv
Line
Count
Source
810
64.5M
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
64.5M
    if (literal_count_ > 0) return literal_count_;
812
18.4E
    if (repeat_count_ == 0) NextCounts();
813
18.4E
    return literal_count_;
814
64.5M
}
_ZN5doris15RleBatchDecoderIhE15NextNumLiteralsEv
Line
Count
Source
810
397
int32_t RleBatchDecoder<T>::NextNumLiterals() {
811
397
    if (literal_count_ > 0) return literal_count_;
812
0
    if (repeat_count_ == 0) NextCounts();
813
0
    return literal_count_;
814
397
}
815
816
template <typename T>
817
102M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
102M
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
102M
    if (HaveBufferedLiterals()) {
821
93.0M
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
93.0M
    }
823
824
102M
    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
102M
    int32_t num_to_bypass =
829
102M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
102M
    if (num_to_bypass > 0) {
831
3.27M
        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
3.27M
        if (num_read < num_to_bypass) return false;
834
3.27M
        literal_count_ -= num_to_bypass;
835
3.27M
        num_consumed += num_to_bypass;
836
3.27M
        num_remaining = num_literals_to_consume - num_consumed;
837
3.27M
    }
838
839
102M
    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
13.8M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
13.8M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
13.8M
    }
845
102M
    return true;
846
102M
}
_ZN5doris15RleBatchDecoderIjE16GetLiteralValuesEiPj
Line
Count
Source
817
37.8M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
37.8M
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
37.8M
    if (HaveBufferedLiterals()) {
821
32.8M
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
32.8M
    }
823
824
37.8M
    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
37.8M
    int32_t num_to_bypass =
829
37.8M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
37.8M
    if (num_to_bypass > 0) {
831
3.14M
        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
3.14M
        if (num_read < num_to_bypass) return false;
834
3.14M
        literal_count_ -= num_to_bypass;
835
3.14M
        num_consumed += num_to_bypass;
836
3.14M
        num_remaining = num_literals_to_consume - num_consumed;
837
3.14M
    }
838
839
37.8M
    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
9.57M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
9.57M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
9.57M
    }
845
37.8M
    return true;
846
37.8M
}
_ZN5doris15RleBatchDecoderItE16GetLiteralValuesEiPt
Line
Count
Source
817
64.5M
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
64.5M
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
64.5M
    if (HaveBufferedLiterals()) {
821
60.2M
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
60.2M
    }
823
824
64.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
64.5M
    int32_t num_to_bypass =
829
64.5M
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
64.5M
    if (num_to_bypass > 0) {
831
133k
        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
133k
        if (num_read < num_to_bypass) return false;
834
133k
        literal_count_ -= num_to_bypass;
835
133k
        num_consumed += num_to_bypass;
836
133k
        num_remaining = num_literals_to_consume - num_consumed;
837
133k
    }
838
839
64.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
4.23M
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
4.23M
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
4.23M
    }
845
64.5M
    return true;
846
64.5M
}
_ZN5doris15RleBatchDecoderIhE16GetLiteralValuesEiPh
Line
Count
Source
817
397
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
818
397
    int32_t num_consumed = 0;
819
    // Copy any buffered literals left over from previous calls.
820
397
    if (HaveBufferedLiterals()) {
821
88
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
822
88
    }
823
824
397
    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
397
    int32_t num_to_bypass =
829
397
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
830
397
    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
397
    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
309
        if (UNLIKELY(!FillLiteralBuffer())) return false;
843
306
        OutputBufferedLiterals(num_remaining, values + num_consumed);
844
306
    }
845
394
    return true;
846
397
}
847
848
template <typename T>
849
13.8M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
13.8M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
13.8M
    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
13.8M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
13.8M
    literal_buffer_pos_ = 0;
855
13.8M
    return true;
856
13.8M
}
_ZN5doris15RleBatchDecoderIjE17FillLiteralBufferEv
Line
Count
Source
849
9.57M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
9.57M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
9.57M
    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
9.57M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
9.57M
    literal_buffer_pos_ = 0;
855
9.57M
    return true;
856
9.57M
}
_ZN5doris15RleBatchDecoderItE17FillLiteralBufferEv
Line
Count
Source
849
4.23M
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
4.23M
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
4.23M
    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.23M
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
4.23M
    literal_buffer_pos_ = 0;
855
4.23M
    return true;
856
4.23M
}
_ZN5doris15RleBatchDecoderIhE17FillLiteralBufferEv
Line
Count
Source
849
309
bool RleBatchDecoder<T>::FillLiteralBuffer() {
850
309
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
851
309
    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
309
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
854
306
    literal_buffer_pos_ = 0;
855
306
    return true;
856
309
}
857
858
template <typename T>
859
134M
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
134M
    uint32_t num_consumed = 0;
861
272M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
137M
        uint32_t num_repeats = NextNumRepeats();
864
137M
        if (num_repeats > 0) {
865
63.5M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
63.5M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
234M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
171M
                values[num_consumed + i] = repeated_value;
869
171M
            }
870
63.5M
            num_consumed += num_repeats_to_set;
871
63.5M
            continue;
872
63.5M
        }
873
874
        // Add remaining literal values, if any.
875
74.2M
        uint32_t num_literals = NextNumLiterals();
876
74.2M
        if (num_literals == 0) {
877
1
            break;
878
1
        }
879
74.2M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
74.2M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
4
            return 0;
882
4
        }
883
74.2M
        num_consumed += num_literals_to_set;
884
74.2M
    }
885
134M
    return num_consumed;
886
134M
}
_ZN5doris15RleBatchDecoderIjE8GetBatchEPjj
Line
Count
Source
859
17.2M
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
17.2M
    uint32_t num_consumed = 0;
861
35.1M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
17.8M
        uint32_t num_repeats = NextNumRepeats();
864
17.8M
        if (num_repeats > 0) {
865
774k
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
774k
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
9.87M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
9.09M
                values[num_consumed + i] = repeated_value;
869
9.09M
            }
870
774k
            num_consumed += num_repeats_to_set;
871
774k
            continue;
872
774k
        }
873
874
        // Add remaining literal values, if any.
875
17.1M
        uint32_t num_literals = NextNumLiterals();
876
17.1M
        if (num_literals == 0) {
877
0
            break;
878
0
        }
879
17.1M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
17.1M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
0
            return 0;
882
0
        }
883
17.1M
        num_consumed += num_literals_to_set;
884
17.1M
    }
885
17.2M
    return num_consumed;
886
17.2M
}
_ZN5doris15RleBatchDecoderItE8GetBatchEPtj
Line
Count
Source
859
117M
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
117M
    uint32_t num_consumed = 0;
861
237M
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
119M
        uint32_t num_repeats = NextNumRepeats();
864
119M
        if (num_repeats > 0) {
865
62.7M
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
62.7M
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
867
223M
            for (int i = 0; i < num_repeats_to_set; ++i) {
868
160M
                values[num_consumed + i] = repeated_value;
869
160M
            }
870
62.7M
            num_consumed += num_repeats_to_set;
871
62.7M
            continue;
872
62.7M
        }
873
874
        // Add remaining literal values, if any.
875
57.0M
        uint32_t num_literals = NextNumLiterals();
876
57.0M
        if (num_literals == 0) {
877
1
            break;
878
1
        }
879
57.0M
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
57.0M
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
1
            return 0;
882
1
        }
883
57.0M
        num_consumed += num_literals_to_set;
884
57.0M
    }
885
117M
    return num_consumed;
886
117M
}
_ZN5doris15RleBatchDecoderIhE8GetBatchEPhj
Line
Count
Source
859
689
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
860
689
    uint32_t num_consumed = 0;
861
1.37k
    while (num_consumed < batch_num) {
862
        // Add RLE encoded values by repeating the current value this number of times.
863
691
        uint32_t num_repeats = NextNumRepeats();
864
691
        if (num_repeats > 0) {
865
294
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
866
294
            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
294
            num_consumed += num_repeats_to_set;
871
294
            continue;
872
294
        }
873
874
        // Add remaining literal values, if any.
875
397
        uint32_t num_literals = NextNumLiterals();
876
397
        if (num_literals == 0) {
877
0
            break;
878
0
        }
879
397
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
880
397
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
881
3
            return 0;
882
3
        }
883
394
        num_consumed += num_literals_to_set;
884
394
    }
885
686
    return num_consumed;
886
689
}
887
} // namespace doris