Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dev/benchmarks): Benchmark ViewArrayAs for versions that contain the helper #443

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion dev/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if(NOT DEFINED CMAKE_C_STANDARD)
endif()

if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

Expand Down
38 changes: 38 additions & 0 deletions dev/benchmarks/c/array_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#include <nanoarrow/nanoarrow.hpp>

#if NANOARROW_VERSION_MINOR >= 5
#include "util.h"
using nanoarrow::Enumerate;
using nanoarrow::Zip;
#endif

// The length of most arrays used in these benchmarks. Just big enough so
// that the benchmark takes a non-trivial amount of time to run.
static const int64_t kNumItemsPrettyBig = 1000000;
Expand Down Expand Up @@ -92,6 +98,37 @@ ArrowErrorCode InitArrayViewFromBuffers(ArrowType type, ArrowArray* array,
return NANOARROW_OK;
}

#if NANOARROW_VERSION_MINOR >= 5

template <typename CType, ArrowType type>
static void BaseArrayViewGetInt(benchmark::State& state) {
nanoarrow::UniqueArray array;
nanoarrow::UniqueArrayView array_view;

int64_t n_values = kNumItemsPrettyBig;

std::vector<CType> values(n_values);
for (auto [i, value] : Zip(Enumerate, values)) {
value = i % std::numeric_limits<CType>::max();
}

NANOARROW_THROW_NOT_OK(
InitArrayViewFromBuffers(type, array.get(), array_view.get(), {}, values));

std::vector<CType> values_out(n_values);
for (auto _ : state) {
for (auto [i, array_slot, value_out] :
Zip(Enumerate, nanoarrow::ViewArrayAs<CType>(array_view.get()), values_out)) {
value_out = *array_slot;
}
benchmark::DoNotOptimize(values_out);
}

state.SetItemsProcessed(n_values * state.iterations());
}

#else

template <typename CType, ArrowType type>
static void BaseArrayViewGetInt(benchmark::State& state) {
nanoarrow::UniqueArray array;
Expand All @@ -117,6 +154,7 @@ static void BaseArrayViewGetInt(benchmark::State& state) {

state.SetItemsProcessed(n_values * state.iterations());
}
#endif

/// \brief Use ArrowArrayViewGet() to consume an int8 array
static void BenchmarkArrayViewGetInt8(benchmark::State& state) {
Expand Down
91 changes: 91 additions & 0 deletions dev/benchmarks/c/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 <tuple>

#include <nanoarrow/nanoarrow.hpp>

#if !defined(__cpp_variadic_using) || !defined(__cpp_deduction_guides) || \
!defined(__cpp_fold_expressions) || !defined(__cpp_lib_integer_sequence) || \
__cpp_range_based_for < 201603L // end sentinels
#error "Zip cannot be supported without C++17 features"
#endif

namespace nanoarrow {

template <typename Ranges, typename Indices>
struct Zip;

template <typename... Ranges>
Zip(Ranges&&...) -> Zip<std::tuple<Ranges...>, std::index_sequence_for<Ranges...>>;

template <typename... Ranges, size_t... I>
struct Zip<std::tuple<Ranges...>, std::index_sequence<I...>> {
explicit Zip(Ranges... ranges) : ranges_(std::forward<Ranges>(ranges)...) {}

std::tuple<Ranges...> ranges_;

using sentinel = std::tuple<decltype(std::get<I>(ranges_).end())...>;

struct iterator : std::tuple<decltype(std::get<I>(ranges_).begin())...> {
using iterator::tuple::tuple;

auto operator*() {
return std::tuple<decltype(*std::get<I>(*this))...>{*std::get<I>(*this)...};
}

iterator& operator++() {
(++std::get<I>(*this), ...);
return *this;
}

bool operator!=(const sentinel& s) const {
bool any_iterator_at_end = (... || (std::get<I>(*this) == std::get<I>(s)));
return !any_iterator_at_end;
}
};

iterator begin() { return {std::get<I>(ranges_).begin()...}; }

sentinel end() { return {std::get<I>(ranges_).end()...}; }
};

constexpr auto Enumerate = [] {
struct {
struct sentinel {};
constexpr sentinel end() const { return {}; }

struct iterator {
int64_t i{0};

constexpr int64_t operator*() { return i; }

constexpr iterator& operator++() {
++i;
return *this;
}

constexpr std::true_type operator!=(sentinel) const { return {}; }
constexpr std::false_type operator==(sentinel) const { return {}; }
};
constexpr iterator begin() const { return {}; }
} out;

return out;
}();

} // namespace nanoarrow
Loading