diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index 6ceca297be3..05d1467a739 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -1477,6 +1477,7 @@ add_arrow_test(sparse_tensor_test) add_arrow_test(stl_test SOURCES stl_iterator_test.cc stl_test.cc) add_arrow_benchmark(builder_benchmark) +add_arrow_benchmark(chunked_array_benchmark) add_arrow_benchmark(chunk_resolver_benchmark) add_arrow_benchmark(compare_benchmark) add_arrow_benchmark(memory_pool_benchmark) diff --git a/cpp/src/arrow/chunked_array.cc b/cpp/src/arrow/chunked_array.cc index d5bb1a8c1e9..9bc829988d3 100644 --- a/cpp/src/arrow/chunked_array.cc +++ b/cpp/src/arrow/chunked_array.cc @@ -189,9 +189,10 @@ std::shared_ptr ChunkedArray::Slice(int64_t offset, int64_t length ARROW_CHECK_LE(offset, length_) << "Slice offset greater than array length"; bool offset_equals_length = offset == length_; int curr_chunk = 0; - while (curr_chunk < num_chunks() && offset >= chunk(curr_chunk)->length()) { - offset -= chunk(curr_chunk)->length(); - curr_chunk++; + if (offset >= 0) { + const auto location = chunk_resolver_.Resolve(offset); + curr_chunk = static_cast(location.chunk_index); + offset = location.index_in_chunk; } ArrayVector new_chunks; @@ -201,8 +202,10 @@ std::shared_ptr ChunkedArray::Slice(int64_t offset, int64_t length new_chunks.push_back(chunk(std::min(curr_chunk, num_chunks() - 1))->Slice(0, 0)); } else { while (curr_chunk < num_chunks() && length > 0) { - new_chunks.push_back(chunk(curr_chunk)->Slice(offset, length)); - length -= chunk(curr_chunk)->length() - offset; + const auto& current_chunk = chunk(curr_chunk); + const auto chunk_length = current_chunk->length(); + new_chunks.push_back(current_chunk->Slice(offset, length)); + length -= chunk_length - offset; offset = 0; curr_chunk++; } diff --git a/cpp/src/arrow/chunked_array_benchmark.cc b/cpp/src/arrow/chunked_array_benchmark.cc new file mode 100644 index 00000000000..601a6ae6547 --- /dev/null +++ b/cpp/src/arrow/chunked_array_benchmark.cc @@ -0,0 +1,176 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "benchmark/benchmark.h" + +#include +#include +#include +#include +#include + +#include "arrow/array.h" +#include "arrow/array/util.h" +#include "arrow/chunked_array.h" +#include "arrow/type.h" + +namespace arrow { +namespace { + +constexpr int64_t kChunkLength = 8; + +std::shared_ptr MakeChunkedArray(int32_t num_chunks) { + ArrayVector chunks; + chunks.reserve(num_chunks); + for (int32_t i = 0; i < num_chunks; ++i) { + chunks.push_back(MakeArrayOfNull(int64(), kChunkLength).ValueOrDie()); + } + return std::make_shared(std::move(chunks), int64()); +} + +// Reproduce the pre-resolver lookup so both implementations can be compared in +// the same binary with the same input data and benchmark settings. +std::shared_ptr SliceWithLinearLookup(const ChunkedArray& array, + int64_t offset, int64_t length) { + const bool offset_equals_length = offset == array.length(); + int curr_chunk = 0; + while (curr_chunk < array.num_chunks() && offset >= array.chunk(curr_chunk)->length()) { + offset -= array.chunk(curr_chunk)->length(); + curr_chunk++; + } + + ArrayVector new_chunks; + if (array.num_chunks() > 0 && (offset_equals_length || length == 0)) { + new_chunks.push_back( + array.chunk(std::min(curr_chunk, array.num_chunks() - 1))->Slice(0, 0)); + } else { + while (curr_chunk < array.num_chunks() && length > 0) { + new_chunks.push_back(array.chunk(curr_chunk)->Slice(offset, length)); + length -= array.chunk(curr_chunk)->length() - offset; + offset = 0; + curr_chunk++; + } + } + + return std::make_shared(new_chunks, array.type()); +} + +template +void ChunkedArraySliceFixedOffsetImpl(benchmark::State& state) { + const int32_t num_chunks = static_cast(state.range(0)); + const int32_t start_percent = static_cast(state.range(1)); + auto array = MakeChunkedArray(num_chunks); + const int64_t offset = (array->length() - 1) * start_percent / 100; + + for (auto _ : state) { + if constexpr (UseLinearLookup) { + benchmark::DoNotOptimize(SliceWithLinearLookup(*array, offset, 1)); + } else { + benchmark::DoNotOptimize(array->Slice(offset, 1)); + } + } + state.SetItemsProcessed(state.iterations()); +} + +void ChunkedArraySliceFixedOffsetLinear(benchmark::State& state) { + ChunkedArraySliceFixedOffsetImpl(state); +} + +void ChunkedArraySliceFixedOffsetResolver(benchmark::State& state) { + ChunkedArraySliceFixedOffsetImpl(state); +} + +// Vary offsets to measure lookup when the cached chunk from the previous call +// is unlikely to match. +template +void ChunkedArraySliceVaryingOffsetsImpl(benchmark::State& state) { + const int32_t num_chunks = static_cast(state.range(0)); + auto array = MakeChunkedArray(num_chunks); + constexpr int32_t kNumOffsets = 4096; + std::vector offsets; + offsets.reserve(kNumOffsets); + for (int32_t i = 0; i < kNumOffsets; ++i) { + const int32_t chunk_index = (i * 7919) % num_chunks; + offsets.push_back(static_cast(chunk_index) * kChunkLength); + } + + int32_t offset_index = 0; + for (auto _ : state) { + if constexpr (UseLinearLookup) { + benchmark::DoNotOptimize( + SliceWithLinearLookup(*array, offsets[offset_index], 1)); + } else { + benchmark::DoNotOptimize(array->Slice(offsets[offset_index], 1)); + } + offset_index = (offset_index + 1) % kNumOffsets; + } + state.SetItemsProcessed(state.iterations()); +} + +void ChunkedArraySliceVaryingOffsetsLinear(benchmark::State& state) { + ChunkedArraySliceVaryingOffsetsImpl(state); +} + +void ChunkedArraySliceVaryingOffsetsResolver(benchmark::State& state) { + ChunkedArraySliceVaryingOffsetsImpl(state); +} + +} // namespace + +BENCHMARK(ChunkedArraySliceFixedOffsetLinear) + ->ArgNames({"num_chunks", "start_percent"}) + ->Args({16, 0}) + ->Args({16, 50}) + ->Args({16, 99}) + ->Args({256, 0}) + ->Args({256, 50}) + ->Args({256, 99}) + ->Args({4096, 0}) + ->Args({4096, 50}) + ->Args({4096, 99}) + ->Args({16384, 0}) + ->Args({16384, 50}) + ->Args({16384, 99}); + +BENCHMARK(ChunkedArraySliceFixedOffsetResolver) + ->ArgNames({"num_chunks", "start_percent"}) + ->Args({16, 0}) + ->Args({16, 50}) + ->Args({16, 99}) + ->Args({256, 0}) + ->Args({256, 50}) + ->Args({256, 99}) + ->Args({4096, 0}) + ->Args({4096, 50}) + ->Args({4096, 99}) + ->Args({16384, 0}) + ->Args({16384, 50}) + ->Args({16384, 99}); + +BENCHMARK(ChunkedArraySliceVaryingOffsetsLinear) + ->ArgNames({"num_chunks"}) + ->Arg(256) + ->Arg(4096) + ->Arg(16384); + +BENCHMARK(ChunkedArraySliceVaryingOffsetsResolver) + ->ArgNames({"num_chunks"}) + ->Arg(256) + ->Arg(4096) + ->Arg(16384); + +} // namespace arrow diff --git a/cpp/src/arrow/chunked_array_test.cc b/cpp/src/arrow/chunked_array_test.cc index 1862e308b38..ec64a9929df 100644 --- a/cpp/src/arrow/chunked_array_test.cc +++ b/cpp/src/arrow/chunked_array_test.cc @@ -276,6 +276,21 @@ TEST_F(TestChunkedArray, SliceEquals) { ASSERT_TRUE(slice5->type()->Equals(one_->type())); } +TEST_F(TestChunkedArray, SliceWithEmptyChunks) { + auto empty = ArrayFromJSON(int32(), "[]"); + auto first = ArrayFromJSON(int32(), "[0, 1, 2]"); + auto second = ArrayFromJSON(int32(), "[3, 4]"); + auto input = std::make_shared( + ArrayVector{empty, first, empty, second, empty}); + auto expected = std::make_shared(ArrayFromJSON(int32(), "[0, 1, 2, 3, 4]")); + + for (int64_t offset : {0, 1, 2, 3, 4, 5}) { + for (int64_t length : {0, 1, 2, 5}) { + ASSERT_TRUE(expected->Slice(offset, length)->Equals(*input->Slice(offset, length))); + } + } +} + TEST_F(TestChunkedArray, ZeroChunksIssues) { ArrayVector empty = {}; auto no_chunks = std::make_shared(empty, int8());