|
29 | 29 | #include "arrow/util/bit_run_reader.h" |
30 | 30 | #include "arrow/util/bit_stream_utils_internal.h" |
31 | 31 | #include "arrow/util/bit_util.h" |
| 32 | +#include "arrow/util/bpacking_internal.h" |
| 33 | +#include "arrow/util/logging.h" |
32 | 34 | #include "arrow/util/macros.h" |
33 | 35 |
|
34 | 36 | namespace arrow::util { |
@@ -278,10 +280,9 @@ class RleRunDecoder { |
278 | 280 | /// Return the repeated value of this decoder. |
279 | 281 | constexpr value_type value() const { return value_; } |
280 | 282 |
|
281 | | - /// Try to advance by as many values as provided. |
| 283 | + /// Advance by as many values as provided or until exhaustion of the decoder. |
282 | 284 | /// Return the number of values skipped. |
283 | | - /// May advance by less than asked for if there are not enough values left. |
284 | | - [[nodiscard]] rle_size_t Advance(rle_size_t batch_size, rle_size_t value_bit_width) { |
| 285 | + [[nodiscard]] rle_size_t Advance(rle_size_t batch_size) { |
285 | 286 | const auto steps = std::min(batch_size, remaining_count_); |
286 | 287 | remaining_count_ -= steps; |
287 | 288 | return steps; |
@@ -331,52 +332,58 @@ class BitPackedRunDecoder { |
331 | 332 | } |
332 | 333 |
|
333 | 334 | void Reset(const RunType& run, rle_size_t value_bit_width) noexcept { |
334 | | - remaining_count_ = run.values_count(); |
335 | 335 | ARROW_DCHECK_GE(value_bit_width, 0); |
336 | 336 | ARROW_DCHECK_LE(value_bit_width, 64); |
337 | | - bit_reader_.Reset(run.raw_data_ptr(), run.raw_data_size(value_bit_width)); |
| 337 | + data_ = run.raw_data_ptr(); |
| 338 | + values_count_ = run.values_count(); |
| 339 | + values_read_ = 0; |
338 | 340 | } |
339 | 341 |
|
340 | 342 | /// Return the number of values that can be advanced. |
341 | | - constexpr rle_size_t remaining() const { return remaining_count_; } |
| 343 | + constexpr rle_size_t remaining() const { return values_count_ - values_read_; } |
342 | 344 |
|
343 | | - /// Try to advance by as many values as provided. |
344 | | - /// Return the number of values skipped or 0 if it fail to advance. |
345 | | - /// May advance by less than asked for if there are not enough values left. |
346 | | - [[nodiscard]] rle_size_t Advance(rle_size_t batch_size, rle_size_t value_bit_width) { |
347 | | - const auto steps = std::min(batch_size, remaining_count_); |
348 | | - if (bit_reader_.Advance(steps * value_bit_width)) { |
349 | | - remaining_count_ -= steps; |
350 | | - return steps; |
351 | | - } |
352 | | - return 0; |
| 345 | + /// Advance by as many values as provided or until exhaustion of the decoder. |
| 346 | + /// Return the number of values skipped. |
| 347 | + [[nodiscard]] rle_size_t Advance(rle_size_t batch_size) { |
| 348 | + const auto steps = std::min(batch_size, remaining()); |
| 349 | + values_read_ += steps; |
| 350 | + return steps; |
353 | 351 | } |
354 | 352 |
|
355 | | - /// Get the next value and return false if there are no more or an error occurred. |
| 353 | + /// Get the next value and return false if there are no more. |
356 | 354 | [[nodiscard]] constexpr bool Get(value_type* out_value, rle_size_t value_bit_width) { |
357 | 355 | return GetBatch(out_value, 1, value_bit_width) == 1; |
358 | 356 | } |
359 | 357 |
|
360 | 358 | /// Get a batch of values return the number of decoded elements. |
361 | 359 | /// May write fewer elements to the output than requested if there are not enough values |
362 | | - /// left or if an error occurred. |
| 360 | + /// left. |
363 | 361 | [[nodiscard]] rle_size_t GetBatch(value_type* out, rle_size_t batch_size, |
364 | 362 | rle_size_t value_bit_width) { |
365 | | - if (ARROW_PREDICT_FALSE(remaining_count_ == 0)) { |
366 | | - return 0; |
367 | | - } |
| 363 | + const auto steps = std::min(batch_size, remaining()); |
| 364 | + const auto bits_read = values_read_ * value_bit_width; |
| 365 | + const auto* unread_data = data_ + bits_read / 8; |
| 366 | + const auto bit_offset = bits_read % 8; |
368 | 367 |
|
369 | | - const auto to_read = std::min(remaining_count_, batch_size); |
370 | | - const auto actual_read = bit_reader_.GetBatch(value_bit_width, out, to_read); |
371 | | - // There should not be any reason why the actual read would be different |
372 | | - // but this is error resistant. |
373 | | - remaining_count_ -= actual_read; |
374 | | - return actual_read; |
| 368 | + if constexpr (std::is_same_v<T, bool>) { |
| 369 | + ::arrow::internal::unpack(unread_data, out, steps, value_bit_width, bit_offset); |
| 370 | + |
| 371 | + } else { |
| 372 | + ::arrow::internal::unpack(unread_data, |
| 373 | + reinterpret_cast<std::make_unsigned_t<value_type>*>(out), |
| 374 | + steps, value_bit_width, bit_offset); |
| 375 | + } |
| 376 | + values_read_ += steps; |
| 377 | + return steps; |
375 | 378 | } |
376 | 379 |
|
377 | 380 | private: |
378 | | - ::arrow::bit_util::BitReader bit_reader_ = {}; |
379 | | - rle_size_t remaining_count_ = 0; |
| 381 | + /// The pointer to the beginning of the run |
| 382 | + const uint8_t* data_ = nullptr; |
| 383 | + /// The total number of values in the run |
| 384 | + rle_size_t values_count_ = 0; |
| 385 | + /// The number of values read by the decoder |
| 386 | + rle_size_t values_read_ = 0; |
380 | 387 |
|
381 | 388 | static_assert(std::is_integral_v<value_type>, |
382 | 389 | "This class is meant to decode positive integers"); |
@@ -895,7 +902,7 @@ auto RunGetSpaced(Converter* converter, typename Converter::out_type* out, |
895 | 902 | return {0, 0}; |
896 | 903 | } |
897 | 904 | converter->WriteRepeated(out, out + batch.total_read(), value); |
898 | | - const auto actual_values_read = decoder->Advance(batch.values_read(), value_bit_width); |
| 905 | + const auto actual_values_read = decoder->Advance(batch.values_read()); |
899 | 906 | // We always cropped the number of values_read by the remaining values in the run. |
900 | 907 | // What's more the RLE decoder should not encounter any errors. |
901 | 908 | ARROW_DCHECK_EQ(actual_values_read, batch.values_read()); |
|
0 commit comments