Coverage Report

Created: 2026-11-05 21:23

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 <algorithm>
22
#include <cstdint>
23
#include <limits> // IWYU pragma: keep
24
25
#include "common/cast_set.h"
26
#include "util/bit_stream_utils.inline.h"
27
#include "util/bit_util.h"
28
29
namespace doris {
30
31
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
32
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
33
// (literal encoding).
34
// For both types of runs, there is a byte-aligned indicator which encodes the length
35
// of the run and the type of the run.
36
// This encoding has the benefit that when there aren't any long enough runs, values
37
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
38
// the run length are byte aligned. This allows for very efficient decoding
39
// implementations.
40
// The encoding is:
41
//    encoded-block := run*
42
//    run := literal-run | repeated-run
43
//    literal-run := literal-indicator < literal bytes >
44
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
45
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
46
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
47
//
48
// Each run is preceded by a varint. The varint's least significant bit is
49
// used to indicate whether the run is a literal run or a repeated run. The rest
50
// of the varint is used to determine the length of the run (eg how many times the
51
// value repeats).
52
//
53
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
54
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
55
// on a byte boundary without padding.
56
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
57
// the actual number of encoded ints. (This means that the total number of encoded values
58
// can not be determined from the encoded data, since the number of values in the last
59
// group may not be a multiple of 8).
60
// There is a break-even point when it is more storage efficient to do run length
61
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
62
// for both the repeated encoding or the literal encoding.  This value can always
63
// be computed based on the bit-width.
64
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
65
//
66
// Examples with bit-width 1 (eg encoding booleans):
67
// ----------------------------------------
68
// 100 1s followed by 100 0s:
69
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
70
//  - (total 4 bytes)
71
//
72
// alternating 1s and 0s (200 total):
73
// 200 ints = 25 groups of 8
74
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
75
// (total 26 bytes, 1 byte overhead)
76
//
77
78
// Decoder class for RLE encoded data.
79
//
80
// NOTE: the encoded format does not have any length prefix or any other way of
81
// indicating that the encoded sequence ends at a certain point, so the Decoder
82
// methods may return some extra bits at the end before the read methods start
83
// to return 0/false.
84
template <typename T>
85
class RleDecoder {
86
public:
87
    // Create a decoder object. buffer/buffer_len is the decoded data.
88
    // bit_width is the width of each value (before encoding).
89
    RleDecoder(const uint8_t* buffer, int buffer_len, int bit_width)
90
4.43k
            : bit_reader_(buffer, buffer_len),
91
4.43k
              bit_width_(bit_width),
92
4.43k
              current_value_(0),
93
4.43k
              repeat_count_(0),
94
4.43k
              literal_count_(0),
95
4.43k
              rewind_state_(CANT_REWIND) {
96
4.43k
        DCHECK_GE(bit_width_, 1);
97
4.43k
        DCHECK_LE(bit_width_, 64);
98
4.43k
    }
_ZN5doris10RleDecoderIbEC2EPKhii
Line
Count
Source
90
3.10k
            : bit_reader_(buffer, buffer_len),
91
3.10k
              bit_width_(bit_width),
92
3.10k
              current_value_(0),
93
3.10k
              repeat_count_(0),
94
3.10k
              literal_count_(0),
95
3.10k
              rewind_state_(CANT_REWIND) {
96
3.10k
        DCHECK_GE(bit_width_, 1);
97
        DCHECK_LE(bit_width_, 64);
98
3.10k
    }
_ZN5doris10RleDecoderIhEC2EPKhii
Line
Count
Source
90
202
            : bit_reader_(buffer, buffer_len),
91
202
              bit_width_(bit_width),
92
202
              current_value_(0),
93
202
              repeat_count_(0),
94
202
              literal_count_(0),
95
202
              rewind_state_(CANT_REWIND) {
96
202
        DCHECK_GE(bit_width_, 1);
97
        DCHECK_LE(bit_width_, 64);
98
202
    }
_ZN5doris10RleDecoderIiEC2EPKhii
Line
Count
Source
90
302
            : bit_reader_(buffer, buffer_len),
91
302
              bit_width_(bit_width),
92
302
              current_value_(0),
93
302
              repeat_count_(0),
94
302
              literal_count_(0),
95
302
              rewind_state_(CANT_REWIND) {
96
302
        DCHECK_GE(bit_width_, 1);
97
        DCHECK_LE(bit_width_, 64);
98
302
    }
_ZN5doris10RleDecoderImEC2EPKhii
Line
Count
Source
90
824
            : bit_reader_(buffer, buffer_len),
91
824
              bit_width_(bit_width),
92
824
              current_value_(0),
93
824
              repeat_count_(0),
94
824
              literal_count_(0),
95
824
              rewind_state_(CANT_REWIND) {
96
824
        DCHECK_GE(bit_width_, 1);
97
        DCHECK_LE(bit_width_, 64);
98
824
    }
99
100
66.6k
    RleDecoder() {}
_ZN5doris10RleDecoderIbEC2Ev
Line
Count
Source
100
66.5k
    RleDecoder() {}
_ZN5doris10RleDecoderIiEC2Ev
Line
Count
Source
100
6
    RleDecoder() {}
_ZN5doris10RleDecoderIhEC2Ev
Line
Count
Source
100
100
    RleDecoder() {}
101
102
    // Skip n values, and returns the number of non-zero entries skipped.
103
    size_t Skip(size_t to_skip);
104
105
    // Gets the next value.  Returns false if there are no more.
106
    bool Get(T* val);
107
108
    // Seek to the previous value.
109
    void RewindOne();
110
111
    // Gets the next run of the same 'val'. Returns 0 if there is no
112
    // more data to be decoded. Will return a run of at most 'max_run'
113
    // values. If there are more values than this, the next call to
114
    // GetNextRun will return more from the same run.
115
    size_t GetNextRun(T* val, size_t max_run);
116
117
    size_t get_values(T* values, size_t num_values);
118
119
    // Get the count of current repeated value
120
    size_t repeated_count();
121
122
    // Get current repeated value, make sure that count equals repeated_count()
123
    T get_repeated_value(size_t count);
124
125
    const BitReader& bit_reader() const { return bit_reader_; }
126
127
private:
128
    bool ReadHeader();
129
130
    enum RewindState { REWIND_LITERAL, REWIND_RUN, CANT_REWIND };
131
132
    BitReader bit_reader_;
133
    int bit_width_;
134
    uint64_t current_value_;
135
    uint32_t repeat_count_;
136
    uint32_t literal_count_;
137
    RewindState rewind_state_;
138
};
139
140
// Class to incrementally build the rle data.
141
// The encoding has two modes: encoding repeated runs and literal runs.
142
// If the run is sufficiently short, it is more efficient to encode as a literal run.
143
// This class does so by buffering 8 values at a time.  If they are not all the same
144
// they are added to the literal run.  If they are the same, they are added to the
145
// repeated run.  When we switch modes, the previous run is flushed out.
146
template <typename T>
147
class RleEncoder {
148
public:
149
    // buffer: buffer to write bits to.
150
    // bit_width: max number of bits for value.
151
    // TODO: consider adding a min_repeated_run_length so the caller can control
152
    // when values should be encoded as repeated runs.  Currently this is derived
153
    // based on the bit_width, which can determine a storage optimal choice.
154
    explicit RleEncoder(faststring* buffer, int bit_width)
155
22.0M
            : bit_width_(bit_width), bit_writer_(buffer) {
156
22.0M
        DCHECK_GE(bit_width_, 1);
157
22.0M
        DCHECK_LE(bit_width_, 64);
158
22.0M
        Clear();
159
22.0M
    }
_ZN5doris10RleEncoderIjEC2EPNS_10faststringEi
Line
Count
Source
155
32
            : bit_width_(bit_width), bit_writer_(buffer) {
156
32
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
32
        Clear();
159
32
    }
_ZN5doris10RleEncoderIsEC2EPNS_10faststringEi
Line
Count
Source
155
6
            : bit_width_(bit_width), bit_writer_(buffer) {
156
6
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
6
        Clear();
159
6
    }
_ZN5doris10RleEncoderIbEC2EPNS_10faststringEi
Line
Count
Source
155
18.3k
            : bit_width_(bit_width), bit_writer_(buffer) {
156
18.3k
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
18.3k
        Clear();
159
18.3k
    }
_ZN5doris10RleEncoderIhEC2EPNS_10faststringEi
Line
Count
Source
155
408
            : bit_width_(bit_width), bit_writer_(buffer) {
156
408
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
408
        Clear();
159
408
    }
_ZN5doris10RleEncoderIiEC2EPNS_10faststringEi
Line
Count
Source
155
8
            : bit_width_(bit_width), bit_writer_(buffer) {
156
8
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
8
        Clear();
159
8
    }
_ZN5doris10RleEncoderImEC2EPNS_10faststringEi
Line
Count
Source
155
22.0M
            : bit_width_(bit_width), bit_writer_(buffer) {
156
22.0M
        DCHECK_GE(bit_width_, 1);
157
        DCHECK_LE(bit_width_, 64);
158
22.0M
        Clear();
159
22.0M
    }
160
161
    // The largest number of values a single repeated run can describe.
162
    //
163
    // A repeated run is written as a vlq-encoded indicator holding
164
    // `run_length << 1` (see FlushRepeatedRun) and read back into a uint32_t by
165
    // BitReader::GetVlqInt. Capping the run length here keeps the indicator
166
    // inside int32_t's positive range, which is exactly the range every released
167
    // version could already encode, so the output stays byte-identical for any
168
    // run length that used to work.
169
    static constexpr int64_t MAX_VALUES_PER_REPEATED_RUN = (int64_t {1} << 30) - 1;
170
171
    // Reserve 'num_bytes' bytes for a plain encoded header, set each
172
    // byte with 'val': this is used for the RLE-encoded data blocks in
173
    // order to be able to able to store the initial ordinal position
174
    // and number of elements. This is a part of RleEncoder in order to
175
    // maintain the correct offset in 'buffer'.
176
    void Reserve(int num_bytes, uint8_t val);
177
178
    // Encode value. This value must be representable with bit_width_ bits.
179
    void Put(T value, size_t run_length = 1);
180
181
    // Flushes any pending values to the underlying buffer.
182
    // Returns the total number of bytes written
183
    int Flush();
184
185
    // Resets all the state in the encoder.
186
    void Clear();
187
188
1.64k
    int32_t len() const { return bit_writer_.bytes_written(); }
_ZNK5doris10RleEncoderIbE3lenEv
Line
Count
Source
188
612
    int32_t len() const { return bit_writer_.bytes_written(); }
_ZNK5doris10RleEncoderIhE3lenEv
Line
Count
Source
188
1.03k
    int32_t len() const { return bit_writer_.bytes_written(); }
Unexecuted instantiation: _ZNK5doris10RleEncoderIiE3lenEv
189
190
private:
191
    // Flushes any buffered values.  If this is part of a repeated run, this is largely
192
    // a no-op.
193
    // If it is part of a literal run, this will call FlushLiteralRun, which writes
194
    // out the buffered literal values.
195
    // If 'done' is true, the current run would be written even if it would normally
196
    // have been buffered more.  This should only be called at the end, when the
197
    // encoder has received all values even if it would normally continue to be
198
    // buffered.
199
    void FlushBufferedValues(bool done);
200
201
    // Flushes literal values to the underlying buffer.  If update_indicator_byte,
202
    // then the current literal run is complete and the indicator byte is updated.
203
    void FlushLiteralRun(bool update_indicator_byte);
204
205
    // Flushes a repeated run to the underlying buffer.
206
    void FlushRepeatedRun();
207
208
    // Number of bits needed to encode the value.
209
    const int bit_width_;
210
211
    // Underlying buffer.
212
    BitWriter bit_writer_;
213
214
    // We need to buffer at most 8 values for literals.  This happens when the
215
    // bit_width is 1 (so 8 values fit in one byte).
216
    // TODO: generalize this to other bit widths
217
    uint64_t buffered_values_[8];
218
219
    // Number of values in buffered_values_
220
    int num_buffered_values_;
221
222
    // The current (also last) value that was written and the count of how
223
    // many times in a row that value has been seen.  This is maintained even
224
    // if we are in a literal run.  If the repeat_count_ get high enough, we switch
225
    // to encoding repeated runs.
226
    uint64_t current_value_;
227
    int64_t repeat_count_;
228
229
    // Number of literals in the current run.  This does not include the literals
230
    // that might be in buffered_values_.  Only after we've got a group big enough
231
    // can we decide if they should part of the literal_count_ or repeat_count_
232
    int literal_count_;
233
234
    // Index of a byte in the underlying buffer that stores the indicator byte.
235
    // This is reserved as soon as we need a literal run but the value is written
236
    // when the literal run is complete. We maintain an index rather than a pointer
237
    // into the underlying buffer because the pointer value may become invalid if
238
    // the underlying buffer is resized.
239
    int literal_indicator_byte_idx_;
240
};
241
242
template <typename T>
243
926k
bool RleDecoder<T>::ReadHeader() {
244
926k
    DCHECK(bit_reader_.is_initialized());
245
926k
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
246
        // Read the next run's indicator int, it could be a literal or repeated run
247
        // The int is encoded as a vlq-encoded value.
248
67.7k
        uint32_t indicator_value = 0;
249
67.7k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
250
67.7k
        if (!result) [[unlikely]] {
251
876
            return false;
252
876
        }
253
254
        // lsb indicates if it is a literal run or repeated run
255
66.8k
        bool is_literal = indicator_value & 1;
256
66.8k
        if (is_literal) {
257
15.4k
            literal_count_ = (indicator_value >> 1) * 8;
258
15.4k
            DCHECK_GT(literal_count_, 0);
259
51.4k
        } else {
260
51.4k
            repeat_count_ = indicator_value >> 1;
261
51.4k
            DCHECK_GT(repeat_count_, 0);
262
51.4k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
263
51.4k
                                                     reinterpret_cast<T*>(&current_value_));
264
51.4k
            DCHECK(result1);
265
51.4k
        }
266
66.8k
    }
267
925k
    return true;
268
926k
}
_ZN5doris10RleDecoderIbE10ReadHeaderEv
Line
Count
Source
243
337k
bool RleDecoder<T>::ReadHeader() {
244
337k
    DCHECK(bit_reader_.is_initialized());
245
337k
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
246
        // Read the next run's indicator int, it could be a literal or repeated run
247
        // The int is encoded as a vlq-encoded value.
248
56.4k
        uint32_t indicator_value = 0;
249
56.4k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
250
56.4k
        if (!result) [[unlikely]] {
251
870
            return false;
252
870
        }
253
254
        // lsb indicates if it is a literal run or repeated run
255
55.6k
        bool is_literal = indicator_value & 1;
256
55.6k
        if (is_literal) {
257
8.83k
            literal_count_ = (indicator_value >> 1) * 8;
258
8.83k
            DCHECK_GT(literal_count_, 0);
259
46.7k
        } else {
260
46.7k
            repeat_count_ = indicator_value >> 1;
261
46.7k
            DCHECK_GT(repeat_count_, 0);
262
46.7k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
263
46.7k
                                                     reinterpret_cast<T*>(&current_value_));
264
46.7k
            DCHECK(result1);
265
46.7k
        }
266
55.6k
    }
267
336k
    return true;
268
337k
}
_ZN5doris10RleDecoderIhE10ReadHeaderEv
Line
Count
Source
243
28.0k
bool RleDecoder<T>::ReadHeader() {
244
28.0k
    DCHECK(bit_reader_.is_initialized());
245
28.0k
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
246
        // Read the next run's indicator int, it could be a literal or repeated run
247
        // The int is encoded as a vlq-encoded value.
248
2.22k
        uint32_t indicator_value = 0;
249
2.22k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
250
2.22k
        if (!result) [[unlikely]] {
251
0
            return false;
252
0
        }
253
254
        // lsb indicates if it is a literal run or repeated run
255
2.22k
        bool is_literal = indicator_value & 1;
256
2.22k
        if (is_literal) {
257
1.59k
            literal_count_ = (indicator_value >> 1) * 8;
258
1.59k
            DCHECK_GT(literal_count_, 0);
259
1.59k
        } else {
260
626
            repeat_count_ = indicator_value >> 1;
261
626
            DCHECK_GT(repeat_count_, 0);
262
626
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
263
626
                                                     reinterpret_cast<T*>(&current_value_));
264
626
            DCHECK(result1);
265
626
        }
266
2.22k
    }
267
28.0k
    return true;
268
28.0k
}
_ZN5doris10RleDecoderIiE10ReadHeaderEv
Line
Count
Source
243
63.7k
bool RleDecoder<T>::ReadHeader() {
244
63.7k
    DCHECK(bit_reader_.is_initialized());
245
63.7k
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
246
        // Read the next run's indicator int, it could be a literal or repeated run
247
        // The int is encoded as a vlq-encoded value.
248
2.89k
        uint32_t indicator_value = 0;
249
2.89k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
250
2.89k
        if (!result) [[unlikely]] {
251
0
            return false;
252
0
        }
253
254
        // lsb indicates if it is a literal run or repeated run
255
2.89k
        bool is_literal = indicator_value & 1;
256
2.89k
        if (is_literal) {
257
2.78k
            literal_count_ = (indicator_value >> 1) * 8;
258
2.78k
            DCHECK_GT(literal_count_, 0);
259
2.78k
        } else {
260
102
            repeat_count_ = indicator_value >> 1;
261
102
            DCHECK_GT(repeat_count_, 0);
262
102
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
263
102
                                                     reinterpret_cast<T*>(&current_value_));
264
102
            DCHECK(result1);
265
102
        }
266
2.89k
    }
267
63.7k
    return true;
268
63.7k
}
_ZN5doris10RleDecoderImE10ReadHeaderEv
Line
Count
Source
243
496k
bool RleDecoder<T>::ReadHeader() {
244
496k
    DCHECK(bit_reader_.is_initialized());
245
496k
    if (literal_count_ == 0 && repeat_count_ == 0) [[unlikely]] {
246
        // Read the next run's indicator int, it could be a literal or repeated run
247
        // The int is encoded as a vlq-encoded value.
248
6.15k
        uint32_t indicator_value = 0;
249
6.15k
        bool result = bit_reader_.GetVlqInt(&indicator_value);
250
6.15k
        if (!result) [[unlikely]] {
251
6
            return false;
252
6
        }
253
254
        // lsb indicates if it is a literal run or repeated run
255
6.14k
        bool is_literal = indicator_value & 1;
256
6.14k
        if (is_literal) {
257
2.25k
            literal_count_ = (indicator_value >> 1) * 8;
258
2.25k
            DCHECK_GT(literal_count_, 0);
259
3.88k
        } else {
260
3.88k
            repeat_count_ = indicator_value >> 1;
261
3.88k
            DCHECK_GT(repeat_count_, 0);
262
3.88k
            bool result1 = bit_reader_.GetAligned<T>(BitUtil::Ceil(bit_width_, 8),
263
3.88k
                                                     reinterpret_cast<T*>(&current_value_));
264
3.88k
            DCHECK(result1);
265
3.88k
        }
266
6.14k
    }
267
496k
    return true;
268
496k
}
269
270
template <typename T>
271
603k
bool RleDecoder<T>::Get(T* val) {
272
603k
    DCHECK(bit_reader_.is_initialized());
273
603k
    if (!ReadHeader()) [[unlikely]] {
274
10
        return false;
275
10
    }
276
277
603k
    if (repeat_count_ > 0) [[likely]] {
278
362k
        *val = cast_set<T>(current_value_);
279
362k
        --repeat_count_;
280
362k
        rewind_state_ = REWIND_RUN;
281
362k
    } else {
282
241k
        DCHECK(literal_count_ > 0);
283
241k
        bool result = bit_reader_.GetValue(bit_width_, val);
284
241k
        DCHECK(result);
285
241k
        --literal_count_;
286
241k
        rewind_state_ = REWIND_LITERAL;
287
241k
    }
288
289
603k
    return true;
290
603k
}
_ZN5doris10RleDecoderIbE3GetEPb
Line
Count
Source
271
20.8k
bool RleDecoder<T>::Get(T* val) {
272
20.8k
    DCHECK(bit_reader_.is_initialized());
273
20.8k
    if (!ReadHeader()) [[unlikely]] {
274
4
        return false;
275
4
    }
276
277
20.8k
    if (repeat_count_ > 0) [[likely]] {
278
20.6k
        *val = cast_set<T>(current_value_);
279
20.6k
        --repeat_count_;
280
20.6k
        rewind_state_ = REWIND_RUN;
281
20.6k
    } else {
282
222
        DCHECK(literal_count_ > 0);
283
222
        bool result = bit_reader_.GetValue(bit_width_, val);
284
222
        DCHECK(result);
285
222
        --literal_count_;
286
222
        rewind_state_ = REWIND_LITERAL;
287
222
    }
288
289
20.8k
    return true;
290
20.8k
}
_ZN5doris10RleDecoderIhE3GetEPh
Line
Count
Source
271
25.4k
bool RleDecoder<T>::Get(T* val) {
272
25.4k
    DCHECK(bit_reader_.is_initialized());
273
25.4k
    if (!ReadHeader()) [[unlikely]] {
274
0
        return false;
275
0
    }
276
277
25.4k
    if (repeat_count_ > 0) [[likely]] {
278
2.91k
        *val = cast_set<T>(current_value_);
279
2.91k
        --repeat_count_;
280
2.91k
        rewind_state_ = REWIND_RUN;
281
22.4k
    } else {
282
22.4k
        DCHECK(literal_count_ > 0);
283
22.4k
        bool result = bit_reader_.GetValue(bit_width_, val);
284
22.4k
        DCHECK(result);
285
22.4k
        --literal_count_;
286
22.4k
        rewind_state_ = REWIND_LITERAL;
287
22.4k
    }
288
289
25.4k
    return true;
290
25.4k
}
_ZN5doris10RleDecoderIiE3GetEPi
Line
Count
Source
271
60.6k
bool RleDecoder<T>::Get(T* val) {
272
60.6k
    DCHECK(bit_reader_.is_initialized());
273
60.6k
    if (!ReadHeader()) [[unlikely]] {
274
0
        return false;
275
0
    }
276
277
60.6k
    if (repeat_count_ > 0) [[likely]] {
278
20.2k
        *val = cast_set<T>(current_value_);
279
20.2k
        --repeat_count_;
280
20.2k
        rewind_state_ = REWIND_RUN;
281
40.4k
    } else {
282
40.4k
        DCHECK(literal_count_ > 0);
283
40.4k
        bool result = bit_reader_.GetValue(bit_width_, val);
284
40.4k
        DCHECK(result);
285
40.4k
        --literal_count_;
286
40.4k
        rewind_state_ = REWIND_LITERAL;
287
40.4k
    }
288
289
60.6k
    return true;
290
60.6k
}
_ZN5doris10RleDecoderImE3GetEPm
Line
Count
Source
271
496k
bool RleDecoder<T>::Get(T* val) {
272
496k
    DCHECK(bit_reader_.is_initialized());
273
496k
    if (!ReadHeader()) [[unlikely]] {
274
6
        return false;
275
6
    }
276
277
496k
    if (repeat_count_ > 0) [[likely]] {
278
318k
        *val = cast_set<T>(current_value_);
279
318k
        --repeat_count_;
280
318k
        rewind_state_ = REWIND_RUN;
281
318k
    } else {
282
178k
        DCHECK(literal_count_ > 0);
283
178k
        bool result = bit_reader_.GetValue(bit_width_, val);
284
178k
        DCHECK(result);
285
178k
        --literal_count_;
286
178k
        rewind_state_ = REWIND_LITERAL;
287
178k
    }
288
289
496k
    return true;
290
496k
}
291
292
template <typename T>
293
void RleDecoder<T>::RewindOne() {
294
    DCHECK(bit_reader_.is_initialized());
295
296
    switch (rewind_state_) {
297
    case CANT_REWIND:
298
        throw Exception(Status::FatalError("Can't rewind more than once after each read!"));
299
        break;
300
    case REWIND_RUN:
301
        ++repeat_count_;
302
        break;
303
    case REWIND_LITERAL: {
304
        bit_reader_.Rewind(bit_width_);
305
        ++literal_count_;
306
        break;
307
    }
308
    }
309
310
    rewind_state_ = CANT_REWIND;
311
}
312
313
template <typename T>
314
263k
size_t RleDecoder<T>::GetNextRun(T* val, size_t max_run) {
315
263k
    DCHECK(bit_reader_.is_initialized());
316
263k
    DCHECK_GT(max_run, 0);
317
263k
    size_t ret = 0;
318
263k
    size_t rem = max_run;
319
313k
    while (ReadHeader()) {
320
312k
        if (repeat_count_ > 0) [[likely]] {
321
96.7k
            if (ret > 0 && *val != current_value_) [[unlikely]] {
322
40.5k
                return ret;
323
40.5k
            }
324
56.1k
            *val = cast_set<T>(current_value_);
325
56.1k
            if (repeat_count_ >= rem) {
326
                // The next run is longer than the amount of remaining data
327
                // that the caller wants to read. Only consume it partially.
328
11.7k
                repeat_count_ -= rem;
329
11.7k
                ret += rem;
330
11.7k
                return ret;
331
11.7k
            }
332
44.4k
            ret += repeat_count_;
333
44.4k
            rem -= repeat_count_;
334
44.4k
            repeat_count_ = 0;
335
215k
        } else {
336
215k
            DCHECK(literal_count_ > 0);
337
215k
            if (ret == 0) {
338
211k
                bool has_more = bit_reader_.GetValue(bit_width_, val);
339
211k
                DCHECK(has_more);
340
211k
                literal_count_--;
341
211k
                ret++;
342
211k
                rem--;
343
211k
            }
344
345
544k
            while (literal_count_ > 0) {
346
538k
                bool result = bit_reader_.GetValue(bit_width_, &current_value_);
347
538k
                DCHECK(result);
348
538k
                if (current_value_ != *val || rem == 0) {
349
210k
                    bit_reader_.Rewind(bit_width_);
350
210k
                    return ret;
351
210k
                }
352
328k
                ret++;
353
328k
                rem--;
354
328k
                literal_count_--;
355
328k
            }
356
215k
        }
357
312k
    }
358
866
    return ret;
359
263k
}
360
361
template <typename T>
362
size_t RleDecoder<T>::get_values(T* values, size_t num_values) {
363
    size_t read_num = 0;
364
    while (read_num < num_values) {
365
        size_t read_this_time = num_values - read_num;
366
367
        if (LIKELY(repeat_count_ > 0)) {
368
            read_this_time = std::min((size_t)repeat_count_, read_this_time);
369
            std::fill(values, values + read_this_time, current_value_);
370
            values += read_this_time;
371
            repeat_count_ -= read_this_time;
372
            read_num += read_this_time;
373
        } else if (literal_count_ > 0) {
374
            read_this_time = std::min((size_t)literal_count_, read_this_time);
375
            for (int i = 0; i < read_this_time; ++i) {
376
                bool result = bit_reader_.GetValue(bit_width_, values);
377
                DCHECK(result);
378
                values++;
379
            }
380
            literal_count_ -= read_this_time;
381
            read_num += read_this_time;
382
        } else {
383
            if (!ReadHeader()) {
384
                return read_num;
385
            }
386
        }
387
    }
388
    return read_num;
389
}
390
391
template <typename T>
392
size_t RleDecoder<T>::repeated_count() {
393
    if (repeat_count_ > 0) {
394
        return repeat_count_;
395
    }
396
    if (literal_count_ == 0) {
397
        ReadHeader();
398
    }
399
    return repeat_count_;
400
}
401
402
template <typename T>
403
T RleDecoder<T>::get_repeated_value(size_t count) {
404
    DCHECK_GE(repeat_count_, count);
405
    repeat_count_ -= count;
406
    return current_value_;
407
}
408
409
template <typename T>
410
2.62k
size_t RleDecoder<T>::Skip(size_t to_skip) {
411
2.62k
    DCHECK(bit_reader_.is_initialized());
412
413
2.62k
    size_t set_count = 0;
414
11.7k
    while (to_skip > 0) {
415
9.08k
        bool result = ReadHeader();
416
9.08k
        DCHECK(result);
417
418
9.08k
        if (repeat_count_ > 0) [[likely]] {
419
2.42k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
420
2.42k
            repeat_count_ -= nskip;
421
2.42k
            to_skip -= nskip;
422
2.42k
            if (current_value_ != 0) {
423
592
                set_count += nskip;
424
592
            }
425
6.66k
        } else {
426
6.66k
            DCHECK(literal_count_ > 0);
427
6.66k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
428
6.66k
            literal_count_ -= nskip;
429
6.66k
            to_skip -= nskip;
430
2.33M
            for (; nskip > 0; nskip--) {
431
2.33M
                T value = 0;
432
2.33M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
433
2.33M
                DCHECK(result1);
434
2.33M
                if (value != 0) {
435
1.72M
                    set_count++;
436
1.72M
                }
437
2.33M
            }
438
6.66k
        }
439
9.08k
    }
440
2.62k
    return set_count;
441
2.62k
}
_ZN5doris10RleDecoderIiE4SkipEm
Line
Count
Source
410
600
size_t RleDecoder<T>::Skip(size_t to_skip) {
411
600
    DCHECK(bit_reader_.is_initialized());
412
413
600
    size_t set_count = 0;
414
3.71k
    while (to_skip > 0) {
415
3.11k
        bool result = ReadHeader();
416
3.11k
        DCHECK(result);
417
418
3.11k
        if (repeat_count_ > 0) [[likely]] {
419
200
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
420
200
            repeat_count_ -= nskip;
421
200
            to_skip -= nskip;
422
200
            if (current_value_ != 0) {
423
200
                set_count += nskip;
424
200
            }
425
2.91k
        } else {
426
2.91k
            DCHECK(literal_count_ > 0);
427
2.91k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
428
2.91k
            literal_count_ -= nskip;
429
2.91k
            to_skip -= nskip;
430
1.31M
            for (; nskip > 0; nskip--) {
431
1.30M
                T value = 0;
432
1.30M
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
433
1.30M
                DCHECK(result1);
434
1.30M
                if (value != 0) {
435
1.30M
                    set_count++;
436
1.30M
                }
437
1.30M
            }
438
2.91k
        }
439
3.11k
    }
440
600
    return set_count;
441
600
}
_ZN5doris10RleDecoderIhE4SkipEm
Line
Count
Source
410
1.68k
size_t RleDecoder<T>::Skip(size_t to_skip) {
411
1.68k
    DCHECK(bit_reader_.is_initialized());
412
413
1.68k
    size_t set_count = 0;
414
4.32k
    while (to_skip > 0) {
415
2.63k
        bool result = ReadHeader();
416
2.63k
        DCHECK(result);
417
418
2.63k
        if (repeat_count_ > 0) [[likely]] {
419
740
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
420
740
            repeat_count_ -= nskip;
421
740
            to_skip -= nskip;
422
740
            if (current_value_ != 0) {
423
386
                set_count += nskip;
424
386
            }
425
1.89k
        } else {
426
1.89k
            DCHECK(literal_count_ > 0);
427
1.89k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
428
1.89k
            literal_count_ -= nskip;
429
1.89k
            to_skip -= nskip;
430
633k
            for (; nskip > 0; nskip--) {
431
631k
                T value = 0;
432
631k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
433
631k
                DCHECK(result1);
434
631k
                if (value != 0) {
435
317k
                    set_count++;
436
317k
                }
437
631k
            }
438
1.89k
        }
439
2.63k
    }
440
1.68k
    return set_count;
441
1.68k
}
_ZN5doris10RleDecoderIbE4SkipEm
Line
Count
Source
410
338
size_t RleDecoder<T>::Skip(size_t to_skip) {
411
338
    DCHECK(bit_reader_.is_initialized());
412
413
338
    size_t set_count = 0;
414
3.67k
    while (to_skip > 0) {
415
3.34k
        bool result = ReadHeader();
416
3.34k
        DCHECK(result);
417
418
3.34k
        if (repeat_count_ > 0) [[likely]] {
419
1.48k
            size_t nskip = (repeat_count_ < to_skip) ? repeat_count_ : to_skip;
420
1.48k
            repeat_count_ -= nskip;
421
1.48k
            to_skip -= nskip;
422
1.48k
            if (current_value_ != 0) {
423
6
                set_count += nskip;
424
6
            }
425
1.85k
        } else {
426
1.85k
            DCHECK(literal_count_ > 0);
427
1.85k
            size_t nskip = (literal_count_ < to_skip) ? literal_count_ : to_skip;
428
1.85k
            literal_count_ -= nskip;
429
1.85k
            to_skip -= nskip;
430
392k
            for (; nskip > 0; nskip--) {
431
390k
                T value = 0;
432
390k
                bool result1 = bit_reader_.GetValue(bit_width_, &value);
433
390k
                DCHECK(result1);
434
390k
                if (value != 0) {
435
92.5k
                    set_count++;
436
92.5k
                }
437
390k
            }
438
1.85k
        }
439
3.34k
    }
440
338
    return set_count;
441
338
}
442
443
// This function buffers input values 8 at a time.  After seeing all 8 values,
444
// it decides whether they should be encoded as a literal or repeated run.
445
template <typename T>
446
40.2M
void RleEncoder<T>::Put(T value, size_t run_length) {
447
40.2M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
40.2M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
1.60M
        repeat_count_ += static_cast<int64_t>(run_length);
453
1.60M
        return;
454
1.60M
    }
455
456
    // Handle run_length > 1 more efficiently
457
75.4M
    while (run_length > 0) {
458
74.8M
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
38.3M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
307M
            for (size_t i = 0; i < to_buffer; ++i) {
462
269M
                buffered_values_[num_buffered_values_++] = value;
463
269M
                ++repeat_count_;
464
269M
            }
465
38.3M
            run_length -= to_buffer;
466
38.3M
            if (num_buffered_values_ == 8) {
467
38.1M
                DCHECK_EQ(literal_count_ % 8, 0);
468
38.1M
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
38.1M
                if (repeat_count_ >= 8 && run_length > 0) {
472
38.0M
                    repeat_count_ += static_cast<int64_t>(run_length);
473
38.0M
                    return;
474
38.0M
                }
475
38.1M
            }
476
38.3M
        } else {
477
            // Value changed
478
36.5M
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
16.0M
                DCHECK_EQ(literal_count_, 0);
482
16.0M
                FlushRepeatedRun();
483
16.0M
            }
484
36.5M
            repeat_count_ = 1;
485
36.5M
            current_value_ = value;
486
487
36.5M
            buffered_values_[num_buffered_values_++] = value;
488
36.5M
            --run_length;
489
36.5M
            if (num_buffered_values_ == 8) {
490
55.8k
                DCHECK_EQ(literal_count_ % 8, 0);
491
55.8k
                FlushBufferedValues(false);
492
55.8k
            }
493
36.5M
        }
494
74.8M
    }
495
38.6M
}
_ZN5doris10RleEncoderIjE3PutEjm
Line
Count
Source
446
1.42k
void RleEncoder<T>::Put(T value, size_t run_length) {
447
1.42k
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
1.42k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
224
        repeat_count_ += static_cast<int64_t>(run_length);
453
224
        return;
454
224
    }
455
456
    // Handle run_length > 1 more efficiently
457
2.40k
    while (run_length > 0) {
458
1.20k
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
128
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
256
            for (size_t i = 0; i < to_buffer; ++i) {
462
128
                buffered_values_[num_buffered_values_++] = value;
463
128
                ++repeat_count_;
464
128
            }
465
128
            run_length -= to_buffer;
466
128
            if (num_buffered_values_ == 8) {
467
24
                DCHECK_EQ(literal_count_ % 8, 0);
468
24
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
24
                if (repeat_count_ >= 8 && run_length > 0) {
472
0
                    repeat_count_ += static_cast<int64_t>(run_length);
473
0
                    return;
474
0
                }
475
24
            }
476
1.07k
        } else {
477
            // Value changed
478
1.07k
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
2
                DCHECK_EQ(literal_count_, 0);
482
2
                FlushRepeatedRun();
483
2
            }
484
1.07k
            repeat_count_ = 1;
485
1.07k
            current_value_ = value;
486
487
1.07k
            buffered_values_[num_buffered_values_++] = value;
488
1.07k
            --run_length;
489
1.07k
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
126
                FlushBufferedValues(false);
492
126
            }
493
1.07k
        }
494
1.20k
    }
495
1.20k
}
_ZN5doris10RleEncoderIsE3PutEsm
Line
Count
Source
446
12
void RleEncoder<T>::Put(T value, size_t run_length) {
447
12
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
12
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
0
        repeat_count_ += static_cast<int64_t>(run_length);
453
0
        return;
454
0
    }
455
456
    // Handle run_length > 1 more efficiently
457
24
    while (run_length > 0) {
458
12
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
9
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
18
            for (size_t i = 0; i < to_buffer; ++i) {
462
9
                buffered_values_[num_buffered_values_++] = value;
463
9
                ++repeat_count_;
464
9
            }
465
9
            run_length -= to_buffer;
466
9
            if (num_buffered_values_ == 8) {
467
0
                DCHECK_EQ(literal_count_ % 8, 0);
468
0
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
0
                if (repeat_count_ >= 8 && run_length > 0) {
472
0
                    repeat_count_ += static_cast<int64_t>(run_length);
473
0
                    return;
474
0
                }
475
0
            }
476
9
        } else {
477
            // Value changed
478
3
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
0
                DCHECK_EQ(literal_count_, 0);
482
0
                FlushRepeatedRun();
483
0
            }
484
3
            repeat_count_ = 1;
485
3
            current_value_ = value;
486
487
3
            buffered_values_[num_buffered_values_++] = value;
488
3
            --run_length;
489
3
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
0
                FlushBufferedValues(false);
492
0
            }
493
3
        }
494
12
    }
495
12
}
_ZN5doris10RleEncoderIbE3PutEbm
Line
Count
Source
446
1.64M
void RleEncoder<T>::Put(T value, size_t run_length) {
447
1.64M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
1.64M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
1.27M
        repeat_count_ += static_cast<int64_t>(run_length);
453
1.27M
        return;
454
1.27M
    }
455
456
    // Handle run_length > 1 more efficiently
457
904k
    while (run_length > 0) {
458
586k
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
269k
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
1.21M
            for (size_t i = 0; i < to_buffer; ++i) {
462
941k
                buffered_values_[num_buffered_values_++] = value;
463
941k
                ++repeat_count_;
464
941k
            }
465
269k
            run_length -= to_buffer;
466
269k
            if (num_buffered_values_ == 8) {
467
122k
                DCHECK_EQ(literal_count_ % 8, 0);
468
122k
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
122k
                if (repeat_count_ >= 8 && run_length > 0) {
472
56.7k
                    repeat_count_ += static_cast<int64_t>(run_length);
473
56.7k
                    return;
474
56.7k
                }
475
122k
            }
476
316k
        } else {
477
            // Value changed
478
316k
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
50.2k
                DCHECK_EQ(literal_count_, 0);
482
50.2k
                FlushRepeatedRun();
483
50.2k
            }
484
316k
            repeat_count_ = 1;
485
316k
            current_value_ = value;
486
487
316k
            buffered_values_[num_buffered_values_++] = value;
488
316k
            --run_length;
489
316k
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
30.3k
                FlushBufferedValues(false);
492
30.3k
            }
493
316k
        }
494
586k
    }
495
374k
}
_ZN5doris10RleEncoderIhE3PutEhm
Line
Count
Source
446
27.3k
void RleEncoder<T>::Put(T value, size_t run_length) {
447
27.3k
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
27.3k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
1.34k
        repeat_count_ += static_cast<int64_t>(run_length);
453
1.34k
        return;
454
1.34k
    }
455
456
    // Handle run_length > 1 more efficiently
457
52.0k
    while (run_length > 0) {
458
26.0k
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
13.4k
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
26.9k
            for (size_t i = 0; i < to_buffer; ++i) {
462
13.5k
                buffered_values_[num_buffered_values_++] = value;
463
13.5k
                ++repeat_count_;
464
13.5k
            }
465
13.4k
            run_length -= to_buffer;
466
13.4k
            if (num_buffered_values_ == 8) {
467
1.64k
                DCHECK_EQ(literal_count_ % 8, 0);
468
1.64k
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
1.64k
                if (repeat_count_ >= 8 && run_length > 0) {
472
6
                    repeat_count_ += static_cast<int64_t>(run_length);
473
6
                    return;
474
6
                }
475
1.64k
            }
476
13.4k
        } else {
477
            // Value changed
478
12.5k
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
322
                DCHECK_EQ(literal_count_, 0);
482
322
                FlushRepeatedRun();
483
322
            }
484
12.5k
            repeat_count_ = 1;
485
12.5k
            current_value_ = value;
486
487
12.5k
            buffered_values_[num_buffered_values_++] = value;
488
12.5k
            --run_length;
489
12.5k
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
1.49k
                FlushBufferedValues(false);
492
1.49k
            }
493
12.5k
        }
494
26.0k
    }
495
26.0k
}
_ZN5doris10RleEncoderIiE3PutEim
Line
Count
Source
446
60.2k
void RleEncoder<T>::Put(T value, size_t run_length) {
447
60.2k
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
60.2k
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
20.1k
        repeat_count_ += static_cast<int64_t>(run_length);
453
20.1k
        return;
454
20.1k
    }
455
456
    // Handle run_length > 1 more efficiently
457
80.0k
    while (run_length > 0) {
458
40.0k
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
30
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
60
            for (size_t i = 0; i < to_buffer; ++i) {
462
30
                buffered_values_[num_buffered_values_++] = value;
463
30
                ++repeat_count_;
464
30
            }
465
30
            run_length -= to_buffer;
466
30
            if (num_buffered_values_ == 8) {
467
4
                DCHECK_EQ(literal_count_ % 8, 0);
468
4
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
4
                if (repeat_count_ >= 8 && run_length > 0) {
472
0
                    repeat_count_ += static_cast<int64_t>(run_length);
473
0
                    return;
474
0
                }
475
4
            }
476
40.0k
        } else {
477
            // Value changed
478
40.0k
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
0
                DCHECK_EQ(literal_count_, 0);
482
0
                FlushRepeatedRun();
483
0
            }
484
40.0k
            repeat_count_ = 1;
485
40.0k
            current_value_ = value;
486
487
40.0k
            buffered_values_[num_buffered_values_++] = value;
488
40.0k
            --run_length;
489
40.0k
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
5.00k
                FlushBufferedValues(false);
492
5.00k
            }
493
40.0k
        }
494
40.0k
    }
495
40.0k
}
_ZN5doris10RleEncoderImE3PutEmm
Line
Count
Source
446
38.5M
void RleEncoder<T>::Put(T value, size_t run_length) {
447
38.5M
    DCHECK(bit_width_ == 64 || value < (1LL << bit_width_));
448
449
    // Fast path: if this is a continuation of the current repeated run and
450
    // we've already buffered enough values, just increment repeat_count_
451
38.5M
    if (current_value_ == value && repeat_count_ >= 8 && run_length > 0) [[likely]] {
452
308k
        repeat_count_ += static_cast<int64_t>(run_length);
453
308k
        return;
454
308k
    }
455
456
    // Handle run_length > 1 more efficiently
457
74.4M
    while (run_length > 0) {
458
74.2M
        if (current_value_ == value) [[likely]] {
459
            // Need to buffer values until we reach 8
460
38.0M
            size_t to_buffer = std::min(run_length, size_t(8 - num_buffered_values_));
461
306M
            for (size_t i = 0; i < to_buffer; ++i) {
462
268M
                buffered_values_[num_buffered_values_++] = value;
463
268M
                ++repeat_count_;
464
268M
            }
465
38.0M
            run_length -= to_buffer;
466
38.0M
            if (num_buffered_values_ == 8) {
467
38.0M
                DCHECK_EQ(literal_count_ % 8, 0);
468
38.0M
                FlushBufferedValues(false);
469
                // After flushing, if we still have a repeated run and more values,
470
                // we can add them directly to repeat_count_
471
38.0M
                if (repeat_count_ >= 8 && run_length > 0) {
472
38.0M
                    repeat_count_ += static_cast<int64_t>(run_length);
473
38.0M
                    return;
474
38.0M
                }
475
38.0M
            }
476
38.0M
        } else {
477
            // Value changed
478
36.1M
            if (repeat_count_ >= 8) {
479
                // We had a run that was long enough but it has ended.  Flush the
480
                // current repeated run.
481
16.0M
                DCHECK_EQ(literal_count_, 0);
482
16.0M
                FlushRepeatedRun();
483
16.0M
            }
484
36.1M
            repeat_count_ = 1;
485
36.1M
            current_value_ = value;
486
487
36.1M
            buffered_values_[num_buffered_values_++] = value;
488
36.1M
            --run_length;
489
36.1M
            if (num_buffered_values_ == 8) {
490
                DCHECK_EQ(literal_count_ % 8, 0);
491
18.9k
                FlushBufferedValues(false);
492
18.9k
            }
493
36.1M
        }
494
74.2M
    }
495
38.2M
}
496
497
template <typename T>
498
132k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
132k
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
18.0k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
18.0k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
18.0k
    }
504
505
    // Write all the buffered values as bit packed literals
506
1.07M
    for (int i = 0; i < num_buffered_values_; ++i) {
507
943k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
943k
    }
509
132k
    num_buffered_values_ = 0;
510
511
132k
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
18.0k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
18.0k
        int32_t indicator_value = (num_groups << 1) | 1;
518
18.0k
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
18.0k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
18.0k
                cast_set<uint8_t>(indicator_value);
521
18.0k
        literal_indicator_byte_idx_ = -1;
522
18.0k
        literal_count_ = 0;
523
18.0k
    }
524
132k
}
_ZN5doris10RleEncoderIjE15FlushLiteralRunEb
Line
Count
Source
498
166
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
166
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
24
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
24
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
24
    }
504
505
    // Write all the buffered values as bit packed literals
506
1.30k
    for (int i = 0; i < num_buffered_values_; ++i) {
507
1.13k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
1.13k
    }
509
166
    num_buffered_values_ = 0;
510
511
166
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
24
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
24
        int32_t indicator_value = (num_groups << 1) | 1;
518
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
24
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
24
                cast_set<uint8_t>(indicator_value);
521
24
        literal_indicator_byte_idx_ = -1;
522
24
        literal_count_ = 0;
523
24
    }
524
166
}
Unexecuted instantiation: _ZN5doris10RleEncoderIsE15FlushLiteralRunEb
_ZN5doris10RleEncoderIbE15FlushLiteralRunEb
Line
Count
Source
498
100k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
100k
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
15.3k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
15.3k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
15.3k
    }
504
505
    // Write all the buffered values as bit packed literals
506
800k
    for (int i = 0; i < num_buffered_values_; ++i) {
507
700k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
700k
    }
509
100k
    num_buffered_values_ = 0;
510
511
100k
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
15.3k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
15.3k
        int32_t indicator_value = (num_groups << 1) | 1;
518
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
15.3k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
15.3k
                cast_set<uint8_t>(indicator_value);
521
15.3k
        literal_indicator_byte_idx_ = -1;
522
15.3k
        literal_count_ = 0;
523
15.3k
    }
524
100k
}
_ZN5doris10RleEncoderIhE15FlushLiteralRunEb
Line
Count
Source
498
3.05k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
3.05k
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
284
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
284
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
284
    }
504
505
    // Write all the buffered values as bit packed literals
506
26.1k
    for (int i = 0; i < num_buffered_values_; ++i) {
507
23.1k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
23.1k
    }
509
3.05k
    num_buffered_values_ = 0;
510
511
3.05k
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
284
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
284
        int32_t indicator_value = (num_groups << 1) | 1;
518
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
284
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
284
                cast_set<uint8_t>(indicator_value);
521
284
        literal_indicator_byte_idx_ = -1;
522
284
        literal_count_ = 0;
523
284
    }
524
3.05k
}
_ZN5doris10RleEncoderIiE15FlushLiteralRunEb
Line
Count
Source
498
5.00k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
5.00k
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
80
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
80
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
80
    }
504
505
    // Write all the buffered values as bit packed literals
506
45.0k
    for (int i = 0; i < num_buffered_values_; ++i) {
507
40.0k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
40.0k
    }
509
5.00k
    num_buffered_values_ = 0;
510
511
5.00k
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
80
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
80
        int32_t indicator_value = (num_groups << 1) | 1;
518
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
80
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
80
                cast_set<uint8_t>(indicator_value);
521
80
        literal_indicator_byte_idx_ = -1;
522
80
        literal_count_ = 0;
523
80
    }
524
5.00k
}
_ZN5doris10RleEncoderImE15FlushLiteralRunEb
Line
Count
Source
498
24.2k
void RleEncoder<T>::FlushLiteralRun(bool update_indicator_byte) {
499
24.2k
    if (literal_indicator_byte_idx_ < 0) {
500
        // The literal indicator byte has not been reserved yet, get one now.
501
2.27k
        literal_indicator_byte_idx_ = cast_set<int>(bit_writer_.GetByteIndexAndAdvance(1));
502
2.27k
        DCHECK_GE(literal_indicator_byte_idx_, 0);
503
2.27k
    }
504
505
    // Write all the buffered values as bit packed literals
506
202k
    for (int i = 0; i < num_buffered_values_; ++i) {
507
178k
        bit_writer_.PutValue(buffered_values_[i], bit_width_);
508
178k
    }
509
24.2k
    num_buffered_values_ = 0;
510
511
24.2k
    if (update_indicator_byte) {
512
        // At this point we need to write the indicator byte for the literal run.
513
        // We only reserve one byte, to allow for streaming writes of literal values.
514
        // The logic makes sure we flush literal runs often enough to not overrun
515
        // the 1 byte.
516
2.27k
        int num_groups = BitUtil::Ceil(literal_count_, 8);
517
2.27k
        int32_t indicator_value = (num_groups << 1) | 1;
518
        DCHECK_EQ(indicator_value & 0xFFFFFF00, 0);
519
2.27k
        bit_writer_.buffer()->data()[literal_indicator_byte_idx_] =
520
2.27k
                cast_set<uint8_t>(indicator_value);
521
2.27k
        literal_indicator_byte_idx_ = -1;
522
2.27k
        literal_count_ = 0;
523
2.27k
    }
524
24.2k
}
525
526
template <typename T>
527
38.0M
void RleEncoder<T>::FlushRepeatedRun() {
528
38.0M
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
76.1M
    while (repeat_count_ > 0) {
535
38.0M
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
38.0M
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
38.0M
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
38.0M
        repeat_count_ -= run_length;
540
38.0M
    }
541
38.0M
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
38.0M
    repeat_count_ = 0;
545
38.0M
}
_ZN5doris10RleEncoderIjE16FlushRepeatedRunEv
Line
Count
Source
527
8
void RleEncoder<T>::FlushRepeatedRun() {
528
8
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
16
    while (repeat_count_ > 0) {
535
8
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
8
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
8
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
8
        repeat_count_ -= run_length;
540
8
    }
541
8
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
8
    repeat_count_ = 0;
545
8
}
_ZN5doris10RleEncoderIsE16FlushRepeatedRunEv
Line
Count
Source
527
6
void RleEncoder<T>::FlushRepeatedRun() {
528
6
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
12
    while (repeat_count_ > 0) {
535
6
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
6
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
6
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
6
        repeat_count_ -= run_length;
540
6
    }
541
6
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
6
    repeat_count_ = 0;
545
6
}
_ZN5doris10RleEncoderIbE16FlushRepeatedRunEv
Line
Count
Source
527
51.2k
void RleEncoder<T>::FlushRepeatedRun() {
528
51.2k
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
102k
    while (repeat_count_ > 0) {
535
51.3k
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
51.3k
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
51.3k
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
51.3k
        repeat_count_ -= run_length;
540
51.3k
    }
541
51.2k
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
51.2k
    repeat_count_ = 0;
545
51.2k
}
_ZN5doris10RleEncoderIhE16FlushRepeatedRunEv
Line
Count
Source
527
512
void RleEncoder<T>::FlushRepeatedRun() {
528
512
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
1.02k
    while (repeat_count_ > 0) {
535
512
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
512
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
512
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
512
        repeat_count_ -= run_length;
540
512
    }
541
512
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
512
    repeat_count_ = 0;
545
512
}
_ZN5doris10RleEncoderIiE16FlushRepeatedRunEv
Line
Count
Source
527
4
void RleEncoder<T>::FlushRepeatedRun() {
528
4
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
8
    while (repeat_count_ > 0) {
535
4
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
4
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
4
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
4
        repeat_count_ -= run_length;
540
4
    }
541
4
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
4
    repeat_count_ = 0;
545
4
}
_ZN5doris10RleEncoderImE16FlushRepeatedRunEv
Line
Count
Source
527
38.0M
void RleEncoder<T>::FlushRepeatedRun() {
528
38.0M
    DCHECK_GT(repeat_count_, 0);
529
    // A run longer than MAX_VALUES_PER_REPEATED_RUN cannot be described by a
530
    // single indicator, so emit it as several back-to-back repeated runs of the
531
    // same value. Decoders stitch those back together transparently
532
    // (RleDecoder::GetNextRun merges adjacent runs carrying the same value), so
533
    // splitting is invisible to readers.
534
76.0M
    while (repeat_count_ > 0) {
535
38.0M
        const int64_t run_length = std::min(repeat_count_, MAX_VALUES_PER_REPEATED_RUN);
536
        // The lsb of 0 indicates this is a repeated run
537
38.0M
        bit_writer_.PutVlqInt(static_cast<uint32_t>(run_length) << 1);
538
38.0M
        bit_writer_.PutAligned(current_value_, BitUtil::Ceil(bit_width_, 8));
539
38.0M
        repeat_count_ -= run_length;
540
38.0M
    }
541
38.0M
    num_buffered_values_ = 0;
542
    // The loop above drains repeat_count_; assign it anyway so the postcondition
543
    // holds even if it somehow entered non-positive.
544
38.0M
    repeat_count_ = 0;
545
38.0M
}
546
547
// Flush the values that have been buffered.  At this point we decide whether
548
// we need to switch between the run types or continue the current one.
549
template <typename T>
550
38.1M
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
38.1M
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
38.0M
        num_buffered_values_ = 0;
555
38.0M
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
11.3k
            DCHECK_EQ(literal_count_ % 8, 0);
559
11.3k
            DCHECK_EQ(repeat_count_, 8);
560
11.3k
            FlushLiteralRun(true);
561
11.3k
        }
562
38.0M
        DCHECK_EQ(literal_count_, 0);
563
38.0M
        return;
564
38.0M
    }
565
566
115k
    literal_count_ += num_buffered_values_;
567
115k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
115k
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
636
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
636
        FlushLiteralRun(true);
573
114k
    } else {
574
114k
        FlushLiteralRun(done);
575
114k
    }
576
115k
    repeat_count_ = 0;
577
115k
}
_ZN5doris10RleEncoderIjE19FlushBufferedValuesEb
Line
Count
Source
550
150
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
150
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
8
        num_buffered_values_ = 0;
555
8
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
0
            DCHECK_EQ(literal_count_ % 8, 0);
559
0
            DCHECK_EQ(repeat_count_, 8);
560
0
            FlushLiteralRun(true);
561
0
        }
562
8
        DCHECK_EQ(literal_count_, 0);
563
8
        return;
564
8
    }
565
566
142
    literal_count_ += num_buffered_values_;
567
142
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
142
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
0
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
0
        FlushLiteralRun(true);
573
142
    } else {
574
142
        FlushLiteralRun(done);
575
142
    }
576
142
    repeat_count_ = 0;
577
142
}
Unexecuted instantiation: _ZN5doris10RleEncoderIsE19FlushBufferedValuesEb
_ZN5doris10RleEncoderIbE19FlushBufferedValuesEb
Line
Count
Source
550
152k
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
152k
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
67.6k
        num_buffered_values_ = 0;
555
67.6k
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
9.62k
            DCHECK_EQ(literal_count_ % 8, 0);
559
9.62k
            DCHECK_EQ(repeat_count_, 8);
560
9.62k
            FlushLiteralRun(true);
561
9.62k
        }
562
67.6k
        DCHECK_EQ(literal_count_, 0);
563
67.6k
        return;
564
67.6k
    }
565
566
85.2k
    literal_count_ += num_buffered_values_;
567
85.2k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
85.2k
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
274
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
274
        FlushLiteralRun(true);
573
84.9k
    } else {
574
84.9k
        FlushLiteralRun(done);
575
84.9k
    }
576
85.2k
    repeat_count_ = 0;
577
85.2k
}
_ZN5doris10RleEncoderIhE19FlushBufferedValuesEb
Line
Count
Source
550
3.13k
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
3.13k
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
334
        num_buffered_values_ = 0;
555
334
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
14
            DCHECK_EQ(literal_count_ % 8, 0);
559
14
            DCHECK_EQ(repeat_count_, 8);
560
14
            FlushLiteralRun(true);
561
14
        }
562
334
        DCHECK_EQ(literal_count_, 0);
563
334
        return;
564
334
    }
565
566
2.80k
    literal_count_ += num_buffered_values_;
567
2.80k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
2.80k
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
30
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
30
        FlushLiteralRun(true);
573
2.77k
    } else {
574
2.77k
        FlushLiteralRun(done);
575
2.77k
    }
576
2.80k
    repeat_count_ = 0;
577
2.80k
}
_ZN5doris10RleEncoderIiE19FlushBufferedValuesEb
Line
Count
Source
550
5.00k
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
5.00k
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
4
        num_buffered_values_ = 0;
555
4
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
0
            DCHECK_EQ(literal_count_ % 8, 0);
559
0
            DCHECK_EQ(repeat_count_, 8);
560
0
            FlushLiteralRun(true);
561
0
        }
562
4
        DCHECK_EQ(literal_count_, 0);
563
4
        return;
564
4
    }
565
566
5.00k
    literal_count_ += num_buffered_values_;
567
5.00k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
5.00k
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
76
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
76
        FlushLiteralRun(true);
573
4.92k
    } else {
574
4.92k
        FlushLiteralRun(done);
575
4.92k
    }
576
5.00k
    repeat_count_ = 0;
577
5.00k
}
_ZN5doris10RleEncoderImE19FlushBufferedValuesEb
Line
Count
Source
550
38.0M
void RleEncoder<T>::FlushBufferedValues(bool done) {
551
38.0M
    if (repeat_count_ >= 8) {
552
        // Clear the buffered values.  They are part of the repeated run now and we
553
        // don't want to flush them out as literals.
554
38.0M
        num_buffered_values_ = 0;
555
38.0M
        if (literal_count_ != 0) {
556
            // There was a current literal run.  All the values in it have been flushed
557
            // but we still need to update the indicator byte.
558
1.73k
            DCHECK_EQ(literal_count_ % 8, 0);
559
1.73k
            DCHECK_EQ(repeat_count_, 8);
560
1.73k
            FlushLiteralRun(true);
561
1.73k
        }
562
38.0M
        DCHECK_EQ(literal_count_, 0);
563
38.0M
        return;
564
38.0M
    }
565
566
22.2k
    literal_count_ += num_buffered_values_;
567
22.2k
    int num_groups = BitUtil::Ceil(literal_count_, 8);
568
22.2k
    if (num_groups + 1 >= (1 << 6)) {
569
        // We need to start a new literal run because the indicator byte we've reserved
570
        // cannot store more values.
571
256
        DCHECK_GE(literal_indicator_byte_idx_, 0);
572
256
        FlushLiteralRun(true);
573
21.9k
    } else {
574
21.9k
        FlushLiteralRun(done);
575
21.9k
    }
576
22.2k
    repeat_count_ = 0;
577
22.2k
}
578
579
template <typename T>
580
842
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
581
4.21k
    for (int i = 0; i < num_bytes; ++i) {
582
3.36k
        bit_writer_.PutValue(val, 8);
583
3.36k
    }
584
842
}
_ZN5doris10RleEncoderIhE7ReserveEih
Line
Count
Source
580
834
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
581
4.17k
    for (int i = 0; i < num_bytes; ++i) {
582
3.33k
        bit_writer_.PutValue(val, 8);
583
3.33k
    }
584
834
}
_ZN5doris10RleEncoderIiE7ReserveEih
Line
Count
Source
580
8
void RleEncoder<T>::Reserve(int num_bytes, uint8_t val) {
581
40
    for (int i = 0; i < num_bytes; ++i) {
582
32
        bit_writer_.PutValue(val, 8);
583
32
    }
584
8
}
585
586
template <typename T>
587
22.0M
int RleEncoder<T>::Flush() {
588
22.0M
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
22.0M
        bool all_repeat = literal_count_ == 0 &&
590
22.0M
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
22.0M
        if (repeat_count_ > 0 && all_repeat) {
593
22.0M
            FlushRepeatedRun();
594
22.0M
        } else {
595
6.04k
            literal_count_ += num_buffered_values_;
596
6.04k
            FlushLiteralRun(true);
597
6.04k
            repeat_count_ = 0;
598
6.04k
        }
599
22.0M
    }
600
22.0M
    bit_writer_.Flush();
601
22.0M
    DCHECK_EQ(num_buffered_values_, 0);
602
22.0M
    DCHECK_EQ(literal_count_, 0);
603
22.0M
    DCHECK_EQ(repeat_count_, 0);
604
22.0M
    return bit_writer_.bytes_written();
605
22.0M
}
_ZN5doris10RleEncoderIjE5FlushEv
Line
Count
Source
587
32
int RleEncoder<T>::Flush() {
588
32
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
30
        bool all_repeat = literal_count_ == 0 &&
590
30
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
30
        if (repeat_count_ > 0 && all_repeat) {
593
6
            FlushRepeatedRun();
594
24
        } else {
595
24
            literal_count_ += num_buffered_values_;
596
24
            FlushLiteralRun(true);
597
24
            repeat_count_ = 0;
598
24
        }
599
30
    }
600
32
    bit_writer_.Flush();
601
32
    DCHECK_EQ(num_buffered_values_, 0);
602
32
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
32
    return bit_writer_.bytes_written();
605
32
}
_ZN5doris10RleEncoderIsE5FlushEv
Line
Count
Source
587
6
int RleEncoder<T>::Flush() {
588
6
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
6
        bool all_repeat = literal_count_ == 0 &&
590
6
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
6
        if (repeat_count_ > 0 && all_repeat) {
593
6
            FlushRepeatedRun();
594
6
        } else {
595
0
            literal_count_ += num_buffered_values_;
596
0
            FlushLiteralRun(true);
597
0
            repeat_count_ = 0;
598
0
        }
599
6
    }
600
6
    bit_writer_.Flush();
601
6
    DCHECK_EQ(num_buffered_values_, 0);
602
6
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
6
    return bit_writer_.bytes_written();
605
6
}
_ZN5doris10RleEncoderIbE5FlushEv
Line
Count
Source
587
6.58k
int RleEncoder<T>::Flush() {
588
6.58k
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
6.57k
        bool all_repeat = literal_count_ == 0 &&
590
6.57k
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
6.57k
        if (repeat_count_ > 0 && all_repeat) {
593
1.08k
            FlushRepeatedRun();
594
5.49k
        } else {
595
5.49k
            literal_count_ += num_buffered_values_;
596
5.49k
            FlushLiteralRun(true);
597
5.49k
            repeat_count_ = 0;
598
5.49k
        }
599
6.57k
    }
600
6.58k
    bit_writer_.Flush();
601
6.58k
    DCHECK_EQ(num_buffered_values_, 0);
602
6.58k
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
6.58k
    return bit_writer_.bytes_written();
605
6.58k
}
_ZN5doris10RleEncoderIhE5FlushEv
Line
Count
Source
587
434
int RleEncoder<T>::Flush() {
588
434
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
430
        bool all_repeat = literal_count_ == 0 &&
590
430
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
430
        if (repeat_count_ > 0 && all_repeat) {
593
190
            FlushRepeatedRun();
594
240
        } else {
595
240
            literal_count_ += num_buffered_values_;
596
240
            FlushLiteralRun(true);
597
240
            repeat_count_ = 0;
598
240
        }
599
430
    }
600
434
    bit_writer_.Flush();
601
434
    DCHECK_EQ(num_buffered_values_, 0);
602
434
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
434
    return bit_writer_.bytes_written();
605
434
}
_ZN5doris10RleEncoderIiE5FlushEv
Line
Count
Source
587
8
int RleEncoder<T>::Flush() {
588
8
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
8
        bool all_repeat = literal_count_ == 0 &&
590
8
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
8
        if (repeat_count_ > 0 && all_repeat) {
593
4
            FlushRepeatedRun();
594
4
        } else {
595
4
            literal_count_ += num_buffered_values_;
596
4
            FlushLiteralRun(true);
597
4
            repeat_count_ = 0;
598
4
        }
599
8
    }
600
8
    bit_writer_.Flush();
601
8
    DCHECK_EQ(num_buffered_values_, 0);
602
8
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
8
    return bit_writer_.bytes_written();
605
8
}
_ZN5doris10RleEncoderImE5FlushEv
Line
Count
Source
587
22.0M
int RleEncoder<T>::Flush() {
588
22.0M
    if (literal_count_ > 0 || repeat_count_ > 0 || num_buffered_values_ > 0) {
589
22.0M
        bool all_repeat = literal_count_ == 0 &&
590
22.0M
                          (repeat_count_ == num_buffered_values_ || num_buffered_values_ == 0);
591
        // There is something pending, figure out if it's a repeated or literal run
592
22.0M
        if (repeat_count_ > 0 && all_repeat) {
593
22.0M
            FlushRepeatedRun();
594
22.0M
        } else {
595
278
            literal_count_ += num_buffered_values_;
596
278
            FlushLiteralRun(true);
597
278
            repeat_count_ = 0;
598
278
        }
599
22.0M
    }
600
22.0M
    bit_writer_.Flush();
601
22.0M
    DCHECK_EQ(num_buffered_values_, 0);
602
22.0M
    DCHECK_EQ(literal_count_, 0);
603
    DCHECK_EQ(repeat_count_, 0);
604
22.0M
    return bit_writer_.bytes_written();
605
22.0M
}
606
607
template <typename T>
608
22.0M
void RleEncoder<T>::Clear() {
609
22.0M
    current_value_ = 0;
610
22.0M
    repeat_count_ = 0;
611
22.0M
    num_buffered_values_ = 0;
612
22.0M
    literal_count_ = 0;
613
22.0M
    literal_indicator_byte_idx_ = -1;
614
22.0M
    bit_writer_.Clear();
615
22.0M
}
_ZN5doris10RleEncoderIjE5ClearEv
Line
Count
Source
608
32
void RleEncoder<T>::Clear() {
609
32
    current_value_ = 0;
610
32
    repeat_count_ = 0;
611
32
    num_buffered_values_ = 0;
612
32
    literal_count_ = 0;
613
32
    literal_indicator_byte_idx_ = -1;
614
32
    bit_writer_.Clear();
615
32
}
_ZN5doris10RleEncoderIsE5ClearEv
Line
Count
Source
608
6
void RleEncoder<T>::Clear() {
609
6
    current_value_ = 0;
610
6
    repeat_count_ = 0;
611
6
    num_buffered_values_ = 0;
612
6
    literal_count_ = 0;
613
6
    literal_indicator_byte_idx_ = -1;
614
6
    bit_writer_.Clear();
615
6
}
_ZN5doris10RleEncoderIbE5ClearEv
Line
Count
Source
608
45.5k
void RleEncoder<T>::Clear() {
609
45.5k
    current_value_ = 0;
610
45.5k
    repeat_count_ = 0;
611
45.5k
    num_buffered_values_ = 0;
612
45.5k
    literal_count_ = 0;
613
45.5k
    literal_indicator_byte_idx_ = -1;
614
45.5k
    bit_writer_.Clear();
615
45.5k
}
_ZN5doris10RleEncoderIhE5ClearEv
Line
Count
Source
608
1.24k
void RleEncoder<T>::Clear() {
609
1.24k
    current_value_ = 0;
610
1.24k
    repeat_count_ = 0;
611
1.24k
    num_buffered_values_ = 0;
612
1.24k
    literal_count_ = 0;
613
1.24k
    literal_indicator_byte_idx_ = -1;
614
1.24k
    bit_writer_.Clear();
615
1.24k
}
_ZN5doris10RleEncoderIiE5ClearEv
Line
Count
Source
608
16
void RleEncoder<T>::Clear() {
609
16
    current_value_ = 0;
610
16
    repeat_count_ = 0;
611
16
    num_buffered_values_ = 0;
612
16
    literal_count_ = 0;
613
16
    literal_indicator_byte_idx_ = -1;
614
16
    bit_writer_.Clear();
615
16
}
_ZN5doris10RleEncoderImE5ClearEv
Line
Count
Source
608
22.0M
void RleEncoder<T>::Clear() {
609
22.0M
    current_value_ = 0;
610
22.0M
    repeat_count_ = 0;
611
22.0M
    num_buffered_values_ = 0;
612
22.0M
    literal_count_ = 0;
613
22.0M
    literal_indicator_byte_idx_ = -1;
614
22.0M
    bit_writer_.Clear();
615
22.0M
}
616
617
// Copy from https://github.com/apache/impala/blob/master/be/src/util/rle-encoding.h
618
// Utility classes to do run length encoding (RLE) for fixed bit width values.  If runs
619
// are sufficiently long, RLE is used, otherwise, the values are just bit-packed
620
// (literal encoding).
621
//
622
// For both types of runs, there is a byte-aligned indicator which encodes the length
623
// of the run and the type of the run.
624
//
625
// This encoding has the benefit that when there aren't any long enough runs, values
626
// are always decoded at fixed (can be precomputed) bit offsets OR both the value and
627
// the run length are byte aligned. This allows for very efficient decoding
628
// implementations.
629
// The encoding is:
630
//    encoded-block := run*
631
//    run := literal-run | repeated-run
632
//    literal-run := literal-indicator < literal bytes >
633
//    repeated-run := repeated-indicator < repeated value. padded to byte boundary >
634
//    literal-indicator := varint_encode( number_of_groups << 1 | 1)
635
//    repeated-indicator := varint_encode( number_of_repetitions << 1 )
636
//
637
// Each run is preceded by a varint. The varint's least significant bit is
638
// used to indicate whether the run is a literal run or a repeated run. The rest
639
// of the varint is used to determine the length of the run (eg how many times the
640
// value repeats).
641
//
642
// In the case of literal runs, the run length is always a multiple of 8 (i.e. encode
643
// in groups of 8), so that no matter the bit-width of the value, the sequence will end
644
// on a byte boundary without padding.
645
// Given that we know it is a multiple of 8, we store the number of 8-groups rather than
646
// the actual number of encoded ints. (This means that the total number of encoded values
647
// can not be determined from the encoded data, since the number of values in the last
648
// group may not be a multiple of 8). For the last group of literal runs, we pad
649
// the group to 8 with zeros. This allows for 8 at a time decoding on the read side
650
// without the need for additional checks.
651
//
652
// There is a break-even point when it is more storage efficient to do run length
653
// encoding.  For 1 bit-width values, that point is 8 values.  They require 2 bytes
654
// for both the repeated encoding or the literal encoding.  This value can always
655
// be computed based on the bit-width.
656
// TODO: For 1 bit-width values it can be optimal to use 16 or 24 values, but more
657
// investigation is needed to do this efficiently, see the reverted IMPALA-6658.
658
// TODO: think about how to use this for strings.  The bit packing isn't quite the same.
659
//
660
// Examples with bit-width 1 (eg encoding booleans):
661
// ----------------------------------------
662
// 100 1s followed by 100 0s:
663
// <varint(100 << 1)> <1, padded to 1 byte> <varint(100 << 1)> <0, padded to 1 byte>
664
//  - (total 4 bytes)
665
//
666
// alternating 1s and 0s (200 total):
667
// 200 ints = 25 groups of 8
668
// <varint((25 << 1) | 1)> <25 bytes of values, bitpacked>
669
// (total 26 bytes, 1 byte overhead)
670
671
// RLE decoder with a batch-oriented interface that enables fast decoding.
672
// Users of this class must first initialize the class to point to a buffer of
673
// RLE-encoded data, passed into the constructor or Reset(). The provided
674
// bit_width must be at most min(sizeof(T) * 8, BatchedBitReader::MAX_BITWIDTH).
675
// Then they can decode data by checking NextNumRepeats()/NextNumLiterals() to
676
// see if the next run is a repeated or literal run, then calling
677
// GetRepeatedValue() or GetLiteralValues() respectively to read the values.
678
//
679
// End-of-input is signalled by NextNumRepeats() == NextNumLiterals() == 0.
680
// Other decoding errors are signalled by functions returning false. If an
681
// error is encountered then it is not valid to read any more data until
682
// Reset() is called.
683
684
//bit-packed-run-len and rle-run-len must be in the range [1, 2^31 - 1].
685
// This means that a Parquet implementation can always store the run length in a signed 32-bit integer.
686
template <typename T>
687
class RleBatchDecoder {
688
public:
689
4.96k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
690
4.96k
        Reset(buffer, buffer_len, bit_width);
691
4.96k
    }
_ZN5doris15RleBatchDecoderIjEC2EPhii
Line
Count
Source
689
2.32k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
690
2.32k
        Reset(buffer, buffer_len, bit_width);
691
2.32k
    }
_ZN5doris15RleBatchDecoderItEC2EPhii
Line
Count
Source
689
2.62k
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
690
2.62k
        Reset(buffer, buffer_len, bit_width);
691
2.62k
    }
_ZN5doris15RleBatchDecoderIhEC2EPhii
Line
Count
Source
689
12
    RleBatchDecoder(uint8_t* buffer, int buffer_len, int bit_width) {
690
12
        Reset(buffer, buffer_len, bit_width);
691
12
    }
692
693
9.59k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderItEC2Ev
Line
Count
Source
693
9.58k
    RleBatchDecoder() = default;
_ZN5doris15RleBatchDecoderIhEC2Ev
Line
Count
Source
693
16
    RleBatchDecoder() = default;
694
695
    // Reset the decoder to read from a new buffer.
696
    void Reset(uint8_t* buffer, int buffer_len, int bit_width);
697
698
    // Return the size of the current repeated run. Returns zero if the current run is
699
    // a literal run or if no more runs can be read from the input.
700
    int32_t NextNumRepeats();
701
702
    // Get the value of the current repeated run and consume the given number of repeats.
703
    // Only valid to call when NextNumRepeats() > 0. The given number of repeats cannot
704
    // be greater than the remaining number of repeats in the run. 'num_repeats_to_consume'
705
    // can be set to 0 to peek at the value without consuming repeats.
706
    T GetRepeatedValue(int32_t num_repeats_to_consume);
707
708
    // Return the size of the current literal run. Returns zero if the current run is
709
    // a repeated run or if no more runs can be read from the input.
710
    int32_t NextNumLiterals();
711
712
    // Consume 'num_literals_to_consume' literals from the current literal run,
713
    // copying the values to 'values'. 'num_literals_to_consume' must be <=
714
    // NextNumLiterals(). Returns true if the requested number of literals were
715
    // successfully read or false if an error was encountered, e.g. the input was
716
    // truncated.
717
    bool GetLiteralValues(int32_t num_literals_to_consume, T* values) WARN_UNUSED_RESULT;
718
719
    // Consume 'num_values_to_consume' values and copy them to 'values'.
720
    // Returns the number of consumed values or 0 if an error occurred.
721
    uint32_t GetBatch(T* values, uint32_t batch_num);
722
723
private:
724
    // Called when both 'literal_count_' and 'repeat_count_' have been exhausted.
725
    // Sets either 'literal_count_' or 'repeat_count_' to the size of the next literal
726
    // or repeated run, or leaves both at 0 if no more values can be read (either because
727
    // the end of the input was reached or an error was encountered decoding).
728
    void NextCounts();
729
730
    /// Fill the literal buffer. Invalid to call if there are already buffered literals.
731
    /// Return false if the input was truncated. This does not advance 'literal_count_'.
732
    bool FillLiteralBuffer() WARN_UNUSED_RESULT;
733
734
8.42k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIjE20HaveBufferedLiteralsEv
Line
Count
Source
734
3.39k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderItE20HaveBufferedLiteralsEv
Line
Count
Source
734
5.01k
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
_ZNK5doris15RleBatchDecoderIhE20HaveBufferedLiteralsEv
Line
Count
Source
734
18
    bool HaveBufferedLiterals() const { return literal_buffer_pos_ < num_buffered_literals_; }
735
736
    /// Output buffered literals, advancing 'literal_buffer_pos_' and decrementing
737
    /// 'literal_count_'. Returns the number of literals outputted.
738
    int32_t OutputBufferedLiterals(int32_t max_to_output, T* values);
739
740
    BatchedBitReader bit_reader_;
741
742
    // Number of bits needed to encode the value. Must be between 0 and 64 after
743
    // the decoder is initialized with a buffer. -1 indicates the decoder was not
744
    // initialized.
745
    int bit_width_ = -1;
746
747
    // If a repeated run, the number of repeats remaining in the current run to be read.
748
    // If the current run is a literal run, this is 0.
749
    int32_t repeat_count_ = 0;
750
751
    // If a literal run, the number of literals remaining in the current run to be read.
752
    // If the current run is a repeated run, this is 0.
753
    int32_t literal_count_ = 0;
754
755
    // If a repeated run, the current repeated value.
756
    T repeated_value_;
757
758
    // Size of buffer for literal values. Large enough to decode a full batch of 32
759
    // literals. The buffer is needed to allow clients to read in batches that are not
760
    // multiples of 32.
761
    static constexpr int LITERAL_BUFFER_LEN = 32;
762
763
    // Buffer containing 'num_buffered_literals_' values. 'literal_buffer_pos_' is the
764
    // position of the next literal to be read from the buffer.
765
    T literal_buffer_[LITERAL_BUFFER_LEN];
766
    int num_buffered_literals_ = 0;
767
    int literal_buffer_pos_ = 0;
768
};
769
770
template <typename T>
771
8.40k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
772
8.40k
    int32_t num_to_output =
773
8.40k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
774
8.40k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
775
8.40k
    literal_buffer_pos_ += num_to_output;
776
8.40k
    literal_count_ -= num_to_output;
777
8.40k
    return num_to_output;
778
8.40k
}
_ZN5doris15RleBatchDecoderIjE22OutputBufferedLiteralsEiPj
Line
Count
Source
771
3.38k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
772
3.38k
    int32_t num_to_output =
773
3.38k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
774
3.38k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
775
3.38k
    literal_buffer_pos_ += num_to_output;
776
3.38k
    literal_count_ -= num_to_output;
777
3.38k
    return num_to_output;
778
3.38k
}
_ZN5doris15RleBatchDecoderItE22OutputBufferedLiteralsEiPt
Line
Count
Source
771
5.01k
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
772
5.01k
    int32_t num_to_output =
773
5.01k
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
774
5.01k
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
775
5.01k
    literal_buffer_pos_ += num_to_output;
776
5.01k
    literal_count_ -= num_to_output;
777
5.01k
    return num_to_output;
778
5.01k
}
_ZN5doris15RleBatchDecoderIhE22OutputBufferedLiteralsEiPh
Line
Count
Source
771
12
int32_t RleBatchDecoder<T>::OutputBufferedLiterals(int32_t max_to_output, T* values) {
772
12
    int32_t num_to_output =
773
12
            std::min<int32_t>(max_to_output, num_buffered_literals_ - literal_buffer_pos_);
774
12
    memcpy(values, &literal_buffer_[literal_buffer_pos_], sizeof(T) * num_to_output);
775
12
    literal_buffer_pos_ += num_to_output;
776
12
    literal_count_ -= num_to_output;
777
12
    return num_to_output;
778
12
}
779
780
template <typename T>
781
4.96k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
782
4.96k
    bit_reader_.Reset(buffer, buffer_len);
783
4.96k
    bit_width_ = bit_width;
784
4.96k
    repeat_count_ = 0;
785
4.96k
    literal_count_ = 0;
786
4.96k
    num_buffered_literals_ = 0;
787
4.96k
    literal_buffer_pos_ = 0;
788
4.96k
}
_ZN5doris15RleBatchDecoderIjE5ResetEPhii
Line
Count
Source
781
2.32k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
782
2.32k
    bit_reader_.Reset(buffer, buffer_len);
783
2.32k
    bit_width_ = bit_width;
784
2.32k
    repeat_count_ = 0;
785
2.32k
    literal_count_ = 0;
786
2.32k
    num_buffered_literals_ = 0;
787
2.32k
    literal_buffer_pos_ = 0;
788
2.32k
}
_ZN5doris15RleBatchDecoderItE5ResetEPhii
Line
Count
Source
781
2.62k
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
782
2.62k
    bit_reader_.Reset(buffer, buffer_len);
783
2.62k
    bit_width_ = bit_width;
784
2.62k
    repeat_count_ = 0;
785
2.62k
    literal_count_ = 0;
786
2.62k
    num_buffered_literals_ = 0;
787
2.62k
    literal_buffer_pos_ = 0;
788
2.62k
}
_ZN5doris15RleBatchDecoderIhE5ResetEPhii
Line
Count
Source
781
12
void RleBatchDecoder<T>::Reset(uint8_t* buffer, int buffer_len, int bit_width) {
782
12
    bit_reader_.Reset(buffer, buffer_len);
783
12
    bit_width_ = bit_width;
784
12
    repeat_count_ = 0;
785
12
    literal_count_ = 0;
786
12
    num_buffered_literals_ = 0;
787
12
    literal_buffer_pos_ = 0;
788
12
}
789
790
template <typename T>
791
13.2k
int32_t RleBatchDecoder<T>::NextNumRepeats() {
792
13.2k
    if (repeat_count_ > 0) return repeat_count_;
793
10.4k
    if (literal_count_ == 0) NextCounts();
794
10.4k
    return repeat_count_;
795
13.2k
}
_ZN5doris15RleBatchDecoderIjE14NextNumRepeatsEv
Line
Count
Source
791
4.91k
int32_t RleBatchDecoder<T>::NextNumRepeats() {
792
4.91k
    if (repeat_count_ > 0) return repeat_count_;
793
4.25k
    if (literal_count_ == 0) NextCounts();
794
4.25k
    return repeat_count_;
795
4.91k
}
_ZN5doris15RleBatchDecoderItE14NextNumRepeatsEv
Line
Count
Source
791
7.78k
int32_t RleBatchDecoder<T>::NextNumRepeats() {
792
7.78k
    if (repeat_count_ > 0) return repeat_count_;
793
6.16k
    if (literal_count_ == 0) NextCounts();
794
6.16k
    return repeat_count_;
795
7.78k
}
_ZN5doris15RleBatchDecoderIhE14NextNumRepeatsEv
Line
Count
Source
791
530
int32_t RleBatchDecoder<T>::NextNumRepeats() {
792
530
    if (repeat_count_ > 0) return repeat_count_;
793
20
    if (literal_count_ == 0) NextCounts();
794
20
    return repeat_count_;
795
530
}
796
797
template <typename T>
798
5.40k
void RleBatchDecoder<T>::NextCounts() {
799
    // Read the next run's indicator int, it could be a literal or repeated run.
800
    // The int is encoded as a ULEB128-encoded value.
801
5.40k
    uint32_t indicator_value = 0;
802
5.40k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
803
4
        return;
804
4
    }
805
806
    // lsb indicates if it is a literal run or repeated run
807
5.40k
    bool is_literal = indicator_value & 1;
808
809
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
810
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
811
5.40k
    uint32_t run_len = indicator_value >> 1;
812
5.40k
    if (is_literal) {
813
        // Use int64_t to avoid overflowing multiplication.
814
3.38k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
815
3.38k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
816
3.38k
        literal_count_ = cast_set<int32_t>(literal_count);
817
3.38k
    } else {
818
2.01k
        if (UNLIKELY(run_len == 0)) return;
819
2.01k
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
820
2.01k
        if (UNLIKELY(!result)) return;
821
2.01k
        repeat_count_ = run_len;
822
2.01k
    }
823
5.40k
}
_ZN5doris15RleBatchDecoderIjE10NextCountsEv
Line
Count
Source
798
2.70k
void RleBatchDecoder<T>::NextCounts() {
799
    // Read the next run's indicator int, it could be a literal or repeated run.
800
    // The int is encoded as a ULEB128-encoded value.
801
2.70k
    uint32_t indicator_value = 0;
802
2.70k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
803
0
        return;
804
0
    }
805
806
    // lsb indicates if it is a literal run or repeated run
807
2.70k
    bool is_literal = indicator_value & 1;
808
809
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
810
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
811
2.70k
    uint32_t run_len = indicator_value >> 1;
812
2.70k
    if (is_literal) {
813
        // Use int64_t to avoid overflowing multiplication.
814
1.84k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
815
1.84k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
816
1.84k
        literal_count_ = cast_set<int32_t>(literal_count);
817
1.84k
    } else {
818
864
        if (UNLIKELY(run_len == 0)) return;
819
864
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
820
864
        if (UNLIKELY(!result)) return;
821
864
        repeat_count_ = run_len;
822
864
    }
823
2.70k
}
_ZN5doris15RleBatchDecoderItE10NextCountsEv
Line
Count
Source
798
2.68k
void RleBatchDecoder<T>::NextCounts() {
799
    // Read the next run's indicator int, it could be a literal or repeated run.
800
    // The int is encoded as a ULEB128-encoded value.
801
2.68k
    uint32_t indicator_value = 0;
802
2.68k
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
803
4
        return;
804
4
    }
805
806
    // lsb indicates if it is a literal run or repeated run
807
2.67k
    bool is_literal = indicator_value & 1;
808
809
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
810
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
811
2.67k
    uint32_t run_len = indicator_value >> 1;
812
2.67k
    if (is_literal) {
813
        // Use int64_t to avoid overflowing multiplication.
814
1.52k
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
815
1.52k
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
816
1.52k
        literal_count_ = cast_set<int32_t>(literal_count);
817
1.52k
    } else {
818
1.15k
        if (UNLIKELY(run_len == 0)) return;
819
1.15k
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
820
1.15k
        if (UNLIKELY(!result)) return;
821
1.15k
        repeat_count_ = run_len;
822
1.15k
    }
823
2.67k
}
_ZN5doris15RleBatchDecoderIhE10NextCountsEv
Line
Count
Source
798
16
void RleBatchDecoder<T>::NextCounts() {
799
    // Read the next run's indicator int, it could be a literal or repeated run.
800
    // The int is encoded as a ULEB128-encoded value.
801
16
    uint32_t indicator_value = 0;
802
16
    if (UNLIKELY(!bit_reader_.GetUleb128<uint32_t>(&indicator_value))) {
803
0
        return;
804
0
    }
805
806
    // lsb indicates if it is a literal run or repeated run
807
16
    bool is_literal = indicator_value & 1;
808
809
    // Don't try to handle run lengths that don't fit in an int32_t - just fail gracefully.
810
    // The Parquet standard does not allow longer runs - see PARQUET-1290.
811
16
    uint32_t run_len = indicator_value >> 1;
812
16
    if (is_literal) {
813
        // Use int64_t to avoid overflowing multiplication.
814
14
        int64_t literal_count = static_cast<int64_t>(run_len) * 8;
815
14
        if (UNLIKELY(literal_count > std::numeric_limits<int32_t>::max())) return;
816
14
        literal_count_ = cast_set<int32_t>(literal_count);
817
14
    } else {
818
2
        if (UNLIKELY(run_len == 0)) return;
819
2
        bool result = bit_reader_.GetBytes<T>(BitUtil::Ceil(bit_width_, 8), &repeated_value_);
820
2
        if (UNLIKELY(!result)) return;
821
2
        repeat_count_ = run_len;
822
2
    }
823
16
}
824
825
template <typename T>
826
5.42k
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
827
5.42k
    repeat_count_ -= num_repeats_to_consume;
828
5.42k
    return repeated_value_;
829
5.42k
}
_ZN5doris15RleBatchDecoderIjE16GetRepeatedValueEi
Line
Count
Source
826
1.52k
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
827
1.52k
    repeat_count_ -= num_repeats_to_consume;
828
1.52k
    return repeated_value_;
829
1.52k
}
_ZN5doris15RleBatchDecoderItE16GetRepeatedValueEi
Line
Count
Source
826
3.39k
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
827
3.39k
    repeat_count_ -= num_repeats_to_consume;
828
3.39k
    return repeated_value_;
829
3.39k
}
_ZN5doris15RleBatchDecoderIhE16GetRepeatedValueEi
Line
Count
Source
826
512
T RleBatchDecoder<T>::GetRepeatedValue(int32_t num_repeats_to_consume) {
827
512
    repeat_count_ -= num_repeats_to_consume;
828
512
    return repeated_value_;
829
512
}
830
831
template <typename T>
832
8.42k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
833
8.42k
    if (literal_count_ > 0) return literal_count_;
834
2
    if (repeat_count_ == 0) NextCounts();
835
2
    return literal_count_;
836
8.42k
}
_ZN5doris15RleBatchDecoderIjE15NextNumLiteralsEv
Line
Count
Source
832
3.39k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
833
3.39k
    if (literal_count_ > 0) return literal_count_;
834
0
    if (repeat_count_ == 0) NextCounts();
835
0
    return literal_count_;
836
3.39k
}
_ZN5doris15RleBatchDecoderItE15NextNumLiteralsEv
Line
Count
Source
832
5.01k
int32_t RleBatchDecoder<T>::NextNumLiterals() {
833
5.01k
    if (literal_count_ > 0) return literal_count_;
834
2
    if (repeat_count_ == 0) NextCounts();
835
2
    return literal_count_;
836
5.01k
}
_ZN5doris15RleBatchDecoderIhE15NextNumLiteralsEv
Line
Count
Source
832
18
int32_t RleBatchDecoder<T>::NextNumLiterals() {
833
18
    if (literal_count_ > 0) return literal_count_;
834
0
    if (repeat_count_ == 0) NextCounts();
835
0
    return literal_count_;
836
18
}
837
838
template <typename T>
839
8.42k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
840
8.42k
    int32_t num_consumed = 0;
841
    // Copy any buffered literals left over from previous calls.
842
8.42k
    if (HaveBufferedLiterals()) {
843
4.97k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
844
4.97k
    }
845
846
8.42k
    int32_t num_remaining = num_literals_to_consume - num_consumed;
847
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
848
    // Need to round to a batch of 32 if the caller is consuming only part of the current
849
    // run avoid ending on a non-byte boundary.
850
8.42k
    int32_t num_to_bypass =
851
8.42k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
852
8.42k
    if (num_to_bypass > 0) {
853
480
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
854
        // If we couldn't read the expected number, that means the input was truncated.
855
480
        if (num_read < num_to_bypass) return false;
856
480
        literal_count_ -= num_to_bypass;
857
480
        num_consumed += num_to_bypass;
858
480
        num_remaining = num_literals_to_consume - num_consumed;
859
480
    }
860
861
8.42k
    if (num_remaining > 0) {
862
        // We weren't able to copy all the literals requested directly from the input.
863
        // Buffer literals and copy over the requested number.
864
3.44k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
865
3.43k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
866
3.43k
    }
867
8.41k
    return true;
868
8.42k
}
_ZN5doris15RleBatchDecoderIjE16GetLiteralValuesEiPj
Line
Count
Source
839
3.39k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
840
3.39k
    int32_t num_consumed = 0;
841
    // Copy any buffered literals left over from previous calls.
842
3.39k
    if (HaveBufferedLiterals()) {
843
1.53k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
844
1.53k
    }
845
846
3.39k
    int32_t num_remaining = num_literals_to_consume - num_consumed;
847
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
848
    // Need to round to a batch of 32 if the caller is consuming only part of the current
849
    // run avoid ending on a non-byte boundary.
850
3.39k
    int32_t num_to_bypass =
851
3.39k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
852
3.39k
    if (num_to_bypass > 0) {
853
478
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
854
        // If we couldn't read the expected number, that means the input was truncated.
855
478
        if (num_read < num_to_bypass) return false;
856
478
        literal_count_ -= num_to_bypass;
857
478
        num_consumed += num_to_bypass;
858
478
        num_remaining = num_literals_to_consume - num_consumed;
859
478
    }
860
861
3.39k
    if (num_remaining > 0) {
862
        // We weren't able to copy all the literals requested directly from the input.
863
        // Buffer literals and copy over the requested number.
864
1.85k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
865
1.85k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
866
1.85k
    }
867
3.39k
    return true;
868
3.39k
}
_ZN5doris15RleBatchDecoderItE16GetLiteralValuesEiPt
Line
Count
Source
839
5.01k
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
840
5.01k
    int32_t num_consumed = 0;
841
    // Copy any buffered literals left over from previous calls.
842
5.01k
    if (HaveBufferedLiterals()) {
843
3.43k
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
844
3.43k
    }
845
846
5.01k
    int32_t num_remaining = num_literals_to_consume - num_consumed;
847
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
848
    // Need to round to a batch of 32 if the caller is consuming only part of the current
849
    // run avoid ending on a non-byte boundary.
850
5.01k
    int32_t num_to_bypass =
851
5.01k
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
852
5.01k
    if (num_to_bypass > 0) {
853
2
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
854
        // If we couldn't read the expected number, that means the input was truncated.
855
2
        if (num_read < num_to_bypass) return false;
856
2
        literal_count_ -= num_to_bypass;
857
2
        num_consumed += num_to_bypass;
858
2
        num_remaining = num_literals_to_consume - num_consumed;
859
2
    }
860
861
5.01k
    if (num_remaining > 0) {
862
        // We weren't able to copy all the literals requested directly from the input.
863
        // Buffer literals and copy over the requested number.
864
1.57k
        if (UNLIKELY(!FillLiteralBuffer())) return false;
865
1.57k
        OutputBufferedLiterals(num_remaining, values + num_consumed);
866
1.57k
    }
867
5.01k
    return true;
868
5.01k
}
_ZN5doris15RleBatchDecoderIhE16GetLiteralValuesEiPh
Line
Count
Source
839
18
bool RleBatchDecoder<T>::GetLiteralValues(int32_t num_literals_to_consume, T* values) {
840
18
    int32_t num_consumed = 0;
841
    // Copy any buffered literals left over from previous calls.
842
18
    if (HaveBufferedLiterals()) {
843
4
        num_consumed = OutputBufferedLiterals(num_literals_to_consume, values);
844
4
    }
845
846
18
    int32_t num_remaining = num_literals_to_consume - num_consumed;
847
    // Copy literals directly to the output, bypassing 'literal_buffer_' when possible.
848
    // Need to round to a batch of 32 if the caller is consuming only part of the current
849
    // run avoid ending on a non-byte boundary.
850
18
    int32_t num_to_bypass =
851
18
            std::min<int32_t>(literal_count_, BitUtil::RoundDownToPowerOf2(num_remaining, 32));
852
18
    if (num_to_bypass > 0) {
853
0
        int num_read = bit_reader_.UnpackBatch(bit_width_, num_to_bypass, values + num_consumed);
854
        // If we couldn't read the expected number, that means the input was truncated.
855
0
        if (num_read < num_to_bypass) return false;
856
0
        literal_count_ -= num_to_bypass;
857
0
        num_consumed += num_to_bypass;
858
0
        num_remaining = num_literals_to_consume - num_consumed;
859
0
    }
860
861
18
    if (num_remaining > 0) {
862
        // We weren't able to copy all the literals requested directly from the input.
863
        // Buffer literals and copy over the requested number.
864
14
        if (UNLIKELY(!FillLiteralBuffer())) return false;
865
8
        OutputBufferedLiterals(num_remaining, values + num_consumed);
866
8
    }
867
12
    return true;
868
18
}
869
870
template <typename T>
871
3.44k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
872
3.44k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
873
3.44k
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
874
    // If we couldn't read the expected number, that means the input was truncated.
875
3.44k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
876
3.43k
    literal_buffer_pos_ = 0;
877
3.43k
    return true;
878
3.44k
}
_ZN5doris15RleBatchDecoderIjE17FillLiteralBufferEv
Line
Count
Source
871
1.85k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
872
1.85k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
873
1.85k
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
874
    // If we couldn't read the expected number, that means the input was truncated.
875
1.85k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
876
1.85k
    literal_buffer_pos_ = 0;
877
1.85k
    return true;
878
1.85k
}
_ZN5doris15RleBatchDecoderItE17FillLiteralBufferEv
Line
Count
Source
871
1.57k
bool RleBatchDecoder<T>::FillLiteralBuffer() {
872
1.57k
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
873
1.57k
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
874
    // If we couldn't read the expected number, that means the input was truncated.
875
1.57k
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
876
1.57k
    literal_buffer_pos_ = 0;
877
1.57k
    return true;
878
1.57k
}
_ZN5doris15RleBatchDecoderIhE17FillLiteralBufferEv
Line
Count
Source
871
14
bool RleBatchDecoder<T>::FillLiteralBuffer() {
872
14
    int32_t num_to_buffer = std::min<int32_t>(LITERAL_BUFFER_LEN, literal_count_);
873
14
    num_buffered_literals_ = bit_reader_.UnpackBatch(bit_width_, num_to_buffer, literal_buffer_);
874
    // If we couldn't read the expected number, that means the input was truncated.
875
14
    if (UNLIKELY(num_buffered_literals_ < num_to_buffer)) return false;
876
8
    literal_buffer_pos_ = 0;
877
8
    return true;
878
14
}
879
880
template <typename T>
881
8.41k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
882
8.41k
    uint32_t num_consumed = 0;
883
16.8k
    while (num_consumed < batch_num) {
884
        // Add RLE encoded values by repeating the current value this number of times.
885
8.46k
        uint32_t num_repeats = NextNumRepeats();
886
8.46k
        if (num_repeats > 0) {
887
3.27k
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
888
3.27k
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
889
4.45M
            for (int i = 0; i < num_repeats_to_set; ++i) {
890
4.45M
                values[num_consumed + i] = repeated_value;
891
4.45M
            }
892
3.27k
            num_consumed += num_repeats_to_set;
893
3.27k
            continue;
894
3.27k
        }
895
896
        // Add remaining literal values, if any.
897
5.19k
        uint32_t num_literals = NextNumLiterals();
898
5.19k
        if (num_literals == 0) {
899
2
            break;
900
2
        }
901
5.19k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
902
5.19k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
903
8
            return 0;
904
8
        }
905
5.18k
        num_consumed += num_literals_to_set;
906
5.18k
    }
907
8.40k
    return num_consumed;
908
8.41k
}
_ZN5doris15RleBatchDecoderIjE8GetBatchEPjj
Line
Count
Source
881
1.74k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
882
1.74k
    uint32_t num_consumed = 0;
883
3.49k
    while (num_consumed < batch_num) {
884
        // Add RLE encoded values by repeating the current value this number of times.
885
1.75k
        uint32_t num_repeats = NextNumRepeats();
886
1.75k
        if (num_repeats > 0) {
887
622
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
888
622
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
889
2.11M
            for (int i = 0; i < num_repeats_to_set; ++i) {
890
2.11M
                values[num_consumed + i] = repeated_value;
891
2.11M
            }
892
622
            num_consumed += num_repeats_to_set;
893
622
            continue;
894
622
        }
895
896
        // Add remaining literal values, if any.
897
1.13k
        uint32_t num_literals = NextNumLiterals();
898
1.13k
        if (num_literals == 0) {
899
0
            break;
900
0
        }
901
1.13k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
902
1.13k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
903
0
            return 0;
904
0
        }
905
1.13k
        num_consumed += num_literals_to_set;
906
1.13k
    }
907
1.74k
    return num_consumed;
908
1.74k
}
_ZN5doris15RleBatchDecoderItE8GetBatchEPtj
Line
Count
Source
881
6.14k
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
882
6.14k
    uint32_t num_consumed = 0;
883
12.3k
    while (num_consumed < batch_num) {
884
        // Add RLE encoded values by repeating the current value this number of times.
885
6.18k
        uint32_t num_repeats = NextNumRepeats();
886
6.18k
        if (num_repeats > 0) {
887
2.13k
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
888
2.13k
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
889
239k
            for (int i = 0; i < num_repeats_to_set; ++i) {
890
237k
                values[num_consumed + i] = repeated_value;
891
237k
            }
892
2.13k
            num_consumed += num_repeats_to_set;
893
2.13k
            continue;
894
2.13k
        }
895
896
        // Add remaining literal values, if any.
897
4.04k
        uint32_t num_literals = NextNumLiterals();
898
4.04k
        if (num_literals == 0) {
899
2
            break;
900
2
        }
901
4.04k
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
902
4.04k
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
903
2
            return 0;
904
2
        }
905
4.04k
        num_consumed += num_literals_to_set;
906
4.04k
    }
907
6.14k
    return num_consumed;
908
6.14k
}
_ZN5doris15RleBatchDecoderIhE8GetBatchEPhj
Line
Count
Source
881
526
uint32_t RleBatchDecoder<T>::GetBatch(T* values, uint32_t batch_num) {
882
526
    uint32_t num_consumed = 0;
883
1.05k
    while (num_consumed < batch_num) {
884
        // Add RLE encoded values by repeating the current value this number of times.
885
530
        uint32_t num_repeats = NextNumRepeats();
886
530
        if (num_repeats > 0) {
887
512
            int32_t num_repeats_to_set = std::min(num_repeats, batch_num - num_consumed);
888
512
            T repeated_value = GetRepeatedValue(num_repeats_to_set);
889
2.09M
            for (int i = 0; i < num_repeats_to_set; ++i) {
890
2.09M
                values[num_consumed + i] = repeated_value;
891
2.09M
            }
892
512
            num_consumed += num_repeats_to_set;
893
512
            continue;
894
512
        }
895
896
        // Add remaining literal values, if any.
897
18
        uint32_t num_literals = NextNumLiterals();
898
18
        if (num_literals == 0) {
899
0
            break;
900
0
        }
901
18
        uint32_t num_literals_to_set = std::min(num_literals, batch_num - num_consumed);
902
18
        if (!GetLiteralValues(num_literals_to_set, values + num_consumed)) {
903
6
            return 0;
904
6
        }
905
12
        num_consumed += num_literals_to_set;
906
12
    }
907
520
    return num_consumed;
908
526
}
909
} // namespace doris