diff --git a/cpp/src/parquet/encoding_benchmark.cc b/cpp/src/parquet/encoding_benchmark.cc index 137c0f8a6fc..bf209ea3796 100644 --- a/cpp/src/parquet/encoding_benchmark.cc +++ b/cpp/src/parquet/encoding_benchmark.cc @@ -730,6 +730,74 @@ static void BM_AlpDecodingDouble(benchmark::State& state) { BENCHMARK(BM_AlpDecodingDouble)->Range(MIN_RANGE, MAX_RANGE); +// ALP speed depends on how "decimal-like" the data is, so vary the share of values that +// cannot be encoded that way (exceptions). Decimal values are generated as +// digits * 10^-decimals, which is how ALP decodes them. +template +static std::vector MakeAlpInput(int64_t length, int exception_percent) { + constexpr int kDecimals = 2; + std::mt19937_64 gen(/*seed=*/42); + std::uniform_int_distribution dist_digits(-100000, 100000); + std::uniform_real_distribution dist_random(-1000, 1000); + std::bernoulli_distribution dist_exception(exception_percent / 100.0); + const T scale = static_cast(std::pow(10.0, -kDecimals)); + std::vector values(length); + for (auto& v : values) { + v = dist_exception(gen) ? dist_random(gen) : static_cast(dist_digits(gen)) * scale; + } + return values; +} + +template +static void BM_AlpEncodingVaried(benchmark::State& state) { + using T = typename DType::c_type; + std::vector values = + MakeAlpInput(state.range(0), static_cast(state.range(1))); + auto encoder = MakeTypedEncoder(Encoding::ALP); + std::shared_ptr buf; + for (auto _ : state) { + encoder->Put(values.data(), static_cast(values.size())); + buf = encoder->FlushValues(); + ::benchmark::DoNotOptimize(buf); + } + state.SetBytesProcessed(state.iterations() * values.size() * sizeof(T)); + state.SetItemsProcessed(state.iterations() * values.size()); + state.counters["compression_ratio"] = + static_cast(values.size() * sizeof(T)) / buf->size(); +} + +template +static void BM_AlpDecodingVaried(benchmark::State& state) { + using T = typename DType::c_type; + std::vector values = + MakeAlpInput(state.range(0), static_cast(state.range(1))); + auto encoder = MakeTypedEncoder(Encoding::ALP); + encoder->Put(values.data(), static_cast(values.size())); + std::shared_ptr buf = encoder->FlushValues(); + auto decoder = MakeTypedDecoder(Encoding::ALP); + std::vector output(values.size()); + for (auto _ : state) { + decoder->SetData(static_cast(values.size()), buf->data(), + static_cast(buf->size())); + decoder->Decode(output.data(), static_cast(values.size())); + benchmark::ClobberMemory(); + } + state.SetBytesProcessed(state.iterations() * values.size() * sizeof(T)); + state.SetItemsProcessed(state.iterations() * values.size()); + state.counters["compression_ratio"] = + static_cast(values.size() * sizeof(T)) / buf->size(); +} + +static void AlpCustomArguments(benchmark::internal::Benchmark* b) { + b->ArgsProduct({{MIN_RANGE, MAX_RANGE}, {0, 1, 10}}) + ->ArgNames({"num-values", "exception-percent"}); +} + +BENCHMARK_TEMPLATE(BM_AlpEncodingVaried, FloatType)->Apply(AlpCustomArguments); +BENCHMARK_TEMPLATE(BM_AlpEncodingVaried, DoubleType)->Apply(AlpCustomArguments); +BENCHMARK_TEMPLATE(BM_AlpDecodingVaried, FloatType)->Apply(AlpCustomArguments); +BENCHMARK_TEMPLATE(BM_AlpDecodingVaried, DoubleType)->Apply(AlpCustomArguments); + // ---------------------------------------------------------------------- // DeltaBitPacking encoding/decoding benchmarks