Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions cpp/src/arrow/chunked_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ std::shared_ptr<ChunkedArray> 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<int>(location.chunk_index);
offset = location.index_in_chunk;
}

ArrayVector new_chunks;
Expand All @@ -201,8 +202,10 @@ std::shared_ptr<ChunkedArray> 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++;
}
Expand Down
176 changes: 176 additions & 0 deletions cpp/src/arrow/chunked_array_benchmark.cc
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>

#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<ChunkedArray> 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<ChunkedArray>(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<ChunkedArray> 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<ChunkedArray>(new_chunks, array.type());
}

template <bool UseLinearLookup>
void ChunkedArraySliceFixedOffsetImpl(benchmark::State& state) {
const int32_t num_chunks = static_cast<int32_t>(state.range(0));
const int32_t start_percent = static_cast<int32_t>(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<true>(state);
}

void ChunkedArraySliceFixedOffsetResolver(benchmark::State& state) {
ChunkedArraySliceFixedOffsetImpl<false>(state);
}

// Vary offsets to measure lookup when the cached chunk from the previous call
// is unlikely to match.
template <bool UseLinearLookup>
void ChunkedArraySliceVaryingOffsetsImpl(benchmark::State& state) {
const int32_t num_chunks = static_cast<int32_t>(state.range(0));
auto array = MakeChunkedArray(num_chunks);
constexpr int32_t kNumOffsets = 4096;
std::vector<int64_t> 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<int64_t>(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<true>(state);
}

void ChunkedArraySliceVaryingOffsetsResolver(benchmark::State& state) {
ChunkedArraySliceVaryingOffsetsImpl<false>(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
15 changes: 15 additions & 0 deletions cpp/src/arrow/chunked_array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkedArray>(
ArrayVector{empty, first, empty, second, empty});
auto expected = std::make_shared<ChunkedArray>(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<ChunkedArray>(empty, int8());
Expand Down
Loading