From a0ab4c06157afc2d6b8a25cd23120fd1f9c19f5d Mon Sep 17 00:00:00 2001 From: Sumedh Pimplikar Date: Tue, 17 Nov 2026 14:55:55 +0530 Subject: [PATCH] MINOR: [C++][Compute][Parquet] Avoid redundant metadata copies Keep chunk and container metadata bound by reference on read-only paths, and move row-group metadata into the finalized Parquet Thrift structure instead of copying it. Replace the related empty-container checks with empty() for clarity. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cpp/src/arrow/compute/cast.cc | 2 +- cpp/src/arrow/compute/exec.cc | 2 +- cpp/src/arrow/compute/kernels/scalar_nested.cc | 2 +- cpp/src/arrow/compute/kernels/vector_replace.cc | 2 +- cpp/src/arrow/compute/kernels/vector_select_k.cc | 8 ++++---- cpp/src/arrow/pretty_print.cc | 2 +- cpp/src/arrow/tensor.cc | 4 ++-- cpp/src/parquet/metadata.cc | 6 +++--- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cpp/src/arrow/compute/cast.cc b/cpp/src/arrow/compute/cast.cc index 4a8bca3f1d2c..13d3debf4632 100644 --- a/cpp/src/arrow/compute/cast.cc +++ b/cpp/src/arrow/compute/cast.cc @@ -178,7 +178,7 @@ Result CastFunction::DispatchExact( } } - if (candidate_kernels.size() == 0) { + if (candidate_kernels.empty()) { return Status::NotImplemented("Unsupported cast from ", types[0].type->ToString(), " to ", ToTypeName(out_type_id_), " using function ", this->name()); diff --git a/cpp/src/arrow/compute/exec.cc b/cpp/src/arrow/compute/exec.cc index db86a75c53ff..231d37f6b3ba 100644 --- a/cpp/src/arrow/compute/exec.cc +++ b/cpp/src/arrow/compute/exec.cc @@ -1311,7 +1311,7 @@ int64_t InferBatchLength(const std::vector& values, bool* all_same) { } } - if (are_all_scalar && values.size() > 0) { + if (are_all_scalar && !values.empty()) { length = 1; } else if (length < 0) { length = 0; diff --git a/cpp/src/arrow/compute/kernels/scalar_nested.cc b/cpp/src/arrow/compute/kernels/scalar_nested.cc index c5d9624bada7..30937a33def6 100644 --- a/cpp/src/arrow/compute/kernels/scalar_nested.cc +++ b/cpp/src/arrow/compute/kernels/scalar_nested.cc @@ -1146,7 +1146,7 @@ Result MakeStructResolve(KernelContext* ctx, auto nullable = OptionsWrapper::Get(ctx).field_nullability; auto metadata = OptionsWrapper::Get(ctx).field_metadata; - if (names.size() == 0) { + if (names.empty()) { names.resize(types.size()); nullable.resize(types.size(), true); metadata.resize(types.size(), nullptr); diff --git a/cpp/src/arrow/compute/kernels/vector_replace.cc b/cpp/src/arrow/compute/kernels/vector_replace.cc index 9b42e5d6cbba..a27638a7bcdf 100644 --- a/cpp/src/arrow/compute/kernels/vector_replace.cc +++ b/cpp/src/arrow/compute/kernels/vector_replace.cc @@ -773,7 +773,7 @@ struct FillNullBackwardChunked { ArrayData* array_with_current = values.chunk(/*first_chunk=*/chunks_length - 1)->data().get(); int64_t last_valid_value_offset = -1; - auto chunks = values.chunks(); + const auto& chunks = values.chunks(); for (int i = chunks_length - 1; i >= 0; --i) { const auto& chunk = chunks[i]; if (is_fixed_width(out->type()->id())) { diff --git a/cpp/src/arrow/compute/kernels/vector_select_k.cc b/cpp/src/arrow/compute/kernels/vector_select_k.cc index 7a63c76f4ef3..8efb2e806c46 100644 --- a/cpp/src/arrow/compute/kernels/vector_select_k.cc +++ b/cpp/src/arrow/compute/kernels/vector_select_k.cc @@ -481,12 +481,12 @@ class RecordBatchSelector { first_remaining_sort_key.order, output.non_null_like_range); } - if (output.nan_range.size() > 0) { + if (!output.nan_range.empty()) { // We have the last sort_key, can just copy over the null values std::copy(p.nan_begin(), p.nan_begin() + output.nan_range.size(), output.nan_begin()); } - if (output.null_range.size() > 0) { + if (!output.null_range.empty()) { // We have the last sort_key, can just copy over the null values std::copy(p.null_begin(), p.null_begin() + output.null_range.size(), output.null_begin()); @@ -499,11 +499,11 @@ class RecordBatchSelector { HeapSortNonNullsToOutput(p.non_null_like_range, cmp, output.non_null_like_range); } - if (output.nan_range.size() > 0) { + if (!output.nan_range.empty()) { ARROW_RETURN_NOT_OK(selector_->DoSelectKForKey(start_sort_key_index_ + 1, p.nan_range, output.nan_range)); } - if (output.null_range.size() > 0) { + if (!output.null_range.empty()) { ARROW_RETURN_NOT_OK(selector_->DoSelectKForKey( start_sort_key_index_ + 1, p.null_range, output.null_range)); } diff --git a/cpp/src/arrow/pretty_print.cc b/cpp/src/arrow/pretty_print.cc index 723449928592..f61c71ecc005 100644 --- a/cpp/src/arrow/pretty_print.cc +++ b/cpp/src/arrow/pretty_print.cc @@ -264,7 +264,7 @@ class ArrayPrinter : public PrettyPrinter { template enable_if_t::value || is_list_view_type::value, Status> WriteDataValues(const ArrayType& array) { - const auto values = array.values(); + const auto& values = array.values(); const auto child_options = ChildOptions(); ArrayPrinter values_printer(child_options, sink_); diff --git a/cpp/src/arrow/tensor.cc b/cpp/src/arrow/tensor.cc index 8b3137d66a35..292899e3feca 100644 --- a/cpp/src/arrow/tensor.cc +++ b/cpp/src/arrow/tensor.cc @@ -526,7 +526,7 @@ Tensor::Tensor(const std::shared_ptr& type, const std::shared_ptr& dim_names) : type_(type), data_(data), shape_(shape), strides_(strides), dim_names_(dim_names) { ARROW_CHECK(is_tensor_supported(type->id())); - if (shape.size() > 0 && strides.size() == 0) { + if (!shape.empty() && strides.empty()) { ARROW_CHECK_OK(internal::ComputeRowMajorStrides( checked_cast(*type_), shape, &strides_)); } @@ -542,7 +542,7 @@ Tensor::Tensor(const std::shared_ptr& type, const std::shared_ptr(dim_names_.size())); diff --git a/cpp/src/parquet/metadata.cc b/cpp/src/parquet/metadata.cc index 2bc871a8c865..7d0565b0f421 100644 --- a/cpp/src/parquet/metadata.cc +++ b/cpp/src/parquet/metadata.cc @@ -2107,7 +2107,7 @@ class FileMetaDataBuilder::FileMetaDataBuilderImpl { total_rows += row_group.num_rows; } metadata_->__set_num_rows(total_rows); - metadata_->__set_row_groups(row_groups_); + metadata_->row_groups = std::move(row_groups_); if (key_value_metadata) { ToThriftKeyValueMetadata(*key_value_metadata, metadata_.get()); @@ -2144,7 +2144,7 @@ class FileMetaDataBuilder::FileMetaDataBuilderImpl { ToThrift(file_encryption_properties->algorithm())); const std::string& footer_signing_key_metadata = file_encryption_properties->footer_key_metadata(); - if (footer_signing_key_metadata.size() > 0) { + if (!footer_signing_key_metadata.empty()) { metadata_->__set_footer_signing_key_metadata(footer_signing_key_metadata); } } @@ -2172,7 +2172,7 @@ class FileMetaDataBuilder::FileMetaDataBuilderImpl { crypto_metadata_->__set_encryption_algorithm( ToThrift(file_encryption_properties->algorithm())); - std::string key_metadata = file_encryption_properties->footer_key_metadata(); + const std::string& key_metadata = file_encryption_properties->footer_key_metadata(); if (!key_metadata.empty()) { crypto_metadata_->__set_key_metadata(key_metadata);