xndai commented on code in PR #1126: URL: https://github.com/apache/iceberg-cpp/pull/1126#discussion_r4702038978
########## src/iceberg/benchmark/mumbling_bitmap_benchmark.cc: ########## @@ -0,0 +1,245 @@ +/* + * 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. + */ + +// Microbenchmarks for the Mumbling bitmap reader. +// +// Build with -DICEBERG_BUILD_BENCHMARKS=ON and run, for example: +// ./build/src/iceberg/benchmark/mumbling_bitmap_benchmark --benchmark_filter=Pfor +// +// The inputs are serialized with the test encoder in +// iceberg/test/mumbling_bitmap_test_util.h, which picks PFOR bit widths the way +// the Java encoder does (checked against Java-generated fixtures in +// mumbling_bitmap_internal_test.cc). + +#include <algorithm> +#include <cstdint> +#include <cstdlib> +#include <random> +#include <set> +#include <string> +#include <utility> +#include <vector> + +#include <benchmark/benchmark.h> + +#include "iceberg/test/mumbling_bitmap_test_util.h" +#include "iceberg/util/mumbling_bitmap_internal.h" + +namespace iceberg { +namespace { + +using mumbling_test::BuildBitmapFromPositions; +using mumbling_test::Bytes; +using mumbling_test::EncodePfor; +using mumbling_test::kBestWidth; + +constexpr int64_t kMaxPositions = MumblingBitmapView::kMaxCardinality; +constexpr uint32_t kSeed = 42; + +// --------------------------------------------------------------------------- +// PFOR descriptor decoding +// --------------------------------------------------------------------------- + +// Value distributions for PFOR inputs, modeled on Java's PFORRandomData. +enum class PforData { + kConstant, // every value is the same (one bit width of 0) + kSmall, // uniform 0-3 + kExceptions1Pct, // 0-3 with 1% exceptions in 0-255 + kExceptions10Pct, // 0-3 with 10% exceptions in 0-255 + kUniform, // uniform 0-255 (stored as raw bytes) +}; + +std::vector<uint8_t> MakePforValues(PforData data, size_t count) { + std::mt19937 rng(kSeed); + std::uniform_int_distribution<int> small(0, 3); + std::uniform_int_distribution<int> any(0, 255); + std::uniform_real_distribution<double> unit(0.0, 1.0); + std::vector<uint8_t> values(count); + for (auto& value : values) { + switch (data) { + case PforData::kConstant: + value = 5; + break; + case PforData::kSmall: + value = static_cast<uint8_t>(small(rng)); + break; + case PforData::kExceptions1Pct: + value = static_cast<uint8_t>(unit(rng) < 0.01 ? any(rng) : small(rng)); + break; + case PforData::kExceptions10Pct: + value = static_cast<uint8_t>(unit(rng) < 0.10 ? any(rng) : small(rng)); + break; + case PforData::kUniform: + value = static_cast<uint8_t>(any(rng)); + break; + } + } + return values; +} + +// Arg: number of values. +void BM_PforDecode(benchmark::State& state, PforData data) { + const auto count = static_cast<size_t>(state.range(0)); + const Bytes encoded = EncodePfor(MakePforValues(data, count), kBestWidth); + std::vector<uint8_t> out(count); + while (state.KeepRunning()) { + auto result = DecodePforBytes(encoded, out); + benchmark::DoNotOptimize(result); Review Comment: After adding the correctness check (5425423), the Release benchmark results are unchanged: all 24 cases are within ±2% (median of 5 runs). The original results still hold. | Benchmark | Before | After | Change | |---|---:|---:|---:| | `Make`, random 100k | 13.3 µs | 13.3 µs | −0.1% | | `Make`, full 2M | 9.36 µs | 9.40 µs | +0.4% | | `Contains`, random 100k | 23.7 µs | 24.1 µs | +1.8% | | `Contains`, full 2M | 14.6 µs | 14.6 µs | +0.1% | | `PforDecode`, uniform 256 | 15.1 ns | 15.2 ns | +0.7% | | `Contains`, runs 100k | 7.20 µs | 7.07 µs | −1.7% | -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
