Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions cpp/src/parquet/decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1301,14 +1301,8 @@ class DictByteArrayDecoderImpl : public DictDecoderImpl<ByteArrayType> {
int64_t valid_bits_offset,
typename EncodingTraits<ByteArrayType>::Accumulator* out) override {
int result = 0;
if (null_count == 0) {
PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count,
/*valid_bits=*/nullptr, valid_bits_offset,
out, &result));
} else {
PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, valid_bits,
valid_bits_offset, out, &result));
}
PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, valid_bits,
valid_bits_offset, out, &result));
return result;
}

Expand All @@ -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++];

@HuaHuaY HuaHuaY Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

auto visit_bit_run = [&](int64_t position, int64_t length, bool valid)
     __attribute__((always_inline)) {

@HuaHuaY HuaHuaY Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the changes in this PR already yield performance improvements, these comments serve merely to explain the reasons behind those gains. They may also serve as a reference should anyone wish to simplify the code in the future. I have decided to approve the original PR.

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 += static_cast<int32_t>(batch_size);
length -= static_cast<int32_t>(batch_size);
const int batch_size =
std::min(num_indices - pos_indices, static_cast<int>(length));
RETURN_NOT_OK(append_indices(indices + pos_indices, batch_size));
pos_indices += batch_size;
length -= batch_size;
}
} else {
for (int64_t i = 0; i < length; ++i) {
Expand Down
Loading