HuaHuaY commented on code in PR #55897:
URL: https://github.com/apache/arrow/pull/55897#discussion_r4780717981
##########
cpp/src/parquet/decoder.cc:
##########
@@ -1325,30 +1319,48 @@ class DictByteArrayDecoderImpl : public
DictDecoderImpl<ByteArrayType> {
const int values_to_decode = num_values - null_count;
int values_decoded = 0;
int num_indices = 0;
- int pos_indices = 0;
+ auto refill_indices = [&]() {
+ const int batch_size =
+ std::min<int>(kBufferSize, values_to_decode - values_decoded);
+ num_indices = idx_decoder_.GetBatch(indices, batch_size);
+ if (ARROW_PREDICT_FALSE(num_indices < 1)) {
+ return Status::Invalid("Invalid number of indices: ", num_indices);
+ }
+ return Status::OK();
+ };
+ auto append_indices = [&](const int32_t* batch, int length) {
+ for (int i = 0; i < length; ++i) {
+ const auto index = batch[i];
+ RETURN_NOT_OK(IndexInBounds(index));
+ const auto& val = dict_values[index];
+ RETURN_NOT_OK(helper->AppendValue(val.ptr,
static_cast<int32_t>(val.len)));
+ }
+ values_decoded += length;
+ return Status::OK();
+ };
+ if (null_count == 0) {
+ while (values_decoded < num_values) {
+ RETURN_NOT_OK(refill_indices());
+ RETURN_NOT_OK(append_indices(indices, num_indices));
+ }
+ *out_num_values = values_decoded;
+ return Status::OK();
+ }
+
+ int pos_indices = 0;
auto visit_bit_run = [&](int64_t position, int64_t length, bool valid) {
if (valid) {
while (length > 0) {
if (num_indices == pos_indices) {
- // Refill indices buffer
- const auto max_batch_size =
- std::min<int32_t>(kBufferSize, values_to_decode -
values_decoded);
- num_indices = idx_decoder_.GetBatch(indices, max_batch_size);
- if (ARROW_PREDICT_FALSE(num_indices < 1)) {
- return Status::Invalid("Invalid number of indices: ",
num_indices);
- }
+ RETURN_NOT_OK(refill_indices());
pos_indices = 0;
}
- const auto batch_size = std::min<int64_t>(num_indices -
pos_indices, length);
- for (int64_t j = 0; j < batch_size; ++j) {
- const auto index = indices[pos_indices++];
Review Comment:
I suspect part of the issue is that `pos_indices` is captured by reference.
In the local build, it is written back before each call to
`helper->AppendValue` and reloaded on the next iteration. The loop also reloads
the index buffer and dictionary pointers.
I believe that by switching to
```cpp
const auto* batch = indices + pos_indices;
const auto* const run_dict = dict_values;
auto* const run_helper = helper;
for (int64_t j = 0; j < batch_size; ++j) {
const auto index = batch[j];
RETURN_NOT_OK(IndexInBounds(index));
const auto& val = run_dict[index];
RETURN_NOT_OK(run_helper->AppendValue(val.ptr,
static_cast<int32_t>(val.len)));
}
pos_indices += static_cast<int>(batch_size);
```
and mark `ArrowBinaryHelper<ByteArrayType,
::arrow::BinaryType>::AppendValue` `ARROW_FORCE_INLINE`
, we should be able to achieve a similar optimization.
Or, keeping `batch[j]` and `ARROW_FORCE_INLINE` on `AppendValue`, we may be
able to mark the lambda `always_inline` instead of adding the `run_dict` and
`run_helper` variables.
```cpp
auto visit_bit_run = [&](int64_t position, int64_t length, bool valid)
__attribute__((always_inline)) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]