pitrou commented on a change in pull request #10915: URL: https://github.com/apache/arrow/pull/10915#discussion_r691093042
########## File path: cpp/src/arrow/util/small_vector_benchmark.cc ########## @@ -0,0 +1,297 @@ +// 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 <algorithm> +#include <cstdint> +#include <iterator> +#include <memory> +#include <numeric> +#include <string> +#include <vector> + +#include <benchmark/benchmark.h> + +#include "arrow/testing/util.h" +#include "arrow/util/logging.h" +#include "arrow/util/macros.h" +#include "arrow/util/small_vector.h" + +namespace arrow { +namespace internal { + +template <typename T> +T ValueInitializer(); +template <typename T> +T ValueInitializer(int seed); + +template <> +int ValueInitializer<int>() { + return 42; +} +template <> +int ValueInitializer<int>(int seed) { + return 42; +} + +template <> +std::string ValueInitializer<std::string>() { + return "42"; +} +template <> +std::string ValueInitializer<std::string>(int seed) { + return std::string("x", seed & 0x3f); // avoid making string too long +} + +template <> +std::shared_ptr<int> ValueInitializer<std::shared_ptr<int>>() { + return std::make_shared<int>(42); +} +template <> +std::shared_ptr<int> ValueInitializer<std::shared_ptr<int>>(int seed) { + return std::make_shared<int>(seed); +} + +template <typename Vector> +ARROW_NOINLINE int64_t ConsumeVector(Vector v) { + return reinterpret_cast<intptr_t>(v.data()); +} + +template <typename Vector> +void MoveEmptyVector(benchmark::State& state) { + constexpr int kNumIters = 1000; + + for (auto _ : state) { + int64_t dummy = 0; + for (int i = 0; i < kNumIters; ++i) { + Vector vec{}; + dummy += ConsumeVector(std::move(vec)); + } + benchmark::DoNotOptimize(dummy); + } + + state.SetItemsProcessed(state.iterations() * kNumIters); +} + +template <typename Vector> +void MoveShortVector(benchmark::State& state) { + using T = typename Vector::value_type; + constexpr int kSize = 3; + constexpr int kNumIters = 1000; + const auto initializer = ValueInitializer<T>(); + + for (auto _ : state) { + int64_t dummy = 0; + for (int i = 0; i < kNumIters; ++i) { + Vector vec(kSize, initializer); Review comment: Unfortunately I haven't found a better way (using a large source vector turns out to add more overhead and is unrealistic to begin with). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
