Skip to content
Merged
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
12 changes: 12 additions & 0 deletions cpp/src/arrow/util/bit_util_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,17 @@ static void SetBitsTo(benchmark::State& state) {
state.SetBytesProcessed(state.iterations() * nbytes);
}

static void CountSetBits(benchmark::State& state) {
int64_t nbytes = state.range(0);
std::shared_ptr<Buffer> buffer = CreateRandomBuffer(nbytes);

for (auto _ : state) {
auto count = internal::CountSetBits(buffer->data(), /*bit_offset=*/0, nbytes * 8);
benchmark::DoNotOptimize(count);
}
state.SetBytesProcessed(state.iterations() * nbytes);
}

template <int64_t OffsetSrc, int64_t OffsetDest = 0>
static void CopyBitmap(benchmark::State& state) { // NOLINT non-const reference
const int64_t buffer_size = state.range(0);
Expand Down Expand Up @@ -519,6 +530,7 @@ BENCHMARK(ReverseSetBitRunReader)->Apply(SetBitRunReaderPercentageArg);
BENCHMARK(VisitBits)->Arg(kBufferSize);
BENCHMARK(VisitBitsUnrolled)->Arg(kBufferSize);
BENCHMARK(SetBitsTo)->Arg(2)->Arg(1 << 4)->Arg(1 << 10)->Arg(1 << 17);
BENCHMARK(CountSetBits)->Arg(1 << 4)->Arg(1 << 10)->Arg(1 << 17);

#ifdef ARROW_WITH_BENCHMARKS_REFERENCE
static void ReferenceNaiveBitmapWriter(benchmark::State& state) {
Expand Down
11 changes: 7 additions & 4 deletions cpp/src/arrow/util/bitmap_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "arrow/util/bitmap_ops.h"

#include <array>
#include <cstdint>
#include <cstring>
#include <functional>
Expand Down Expand Up @@ -55,13 +56,15 @@ int64_t CountSetBits(const uint8_t* data, int64_t bit_offset, int64_t length) {
constexpr int64_t kCountUnrollFactor = 4;
const int64_t words_rounded =
bit_util::RoundDown(p.aligned_words, kCountUnrollFactor);
int64_t count_unroll[kCountUnrollFactor] = {0};
std::array<int64_t, kCountUnrollFactor> count_unroll{};

// Unroll the loop for better performance
for (int64_t i = 0; i < words_rounded; i += kCountUnrollFactor) {
for (int64_t k = 0; k < kCountUnrollFactor; k++) {
count_unroll[k] += bit_util::PopCount(u64_data[k]);
}
// (hand-unrolled as some gcc versions would unnest a nested `for` loop)
count_unroll[0] += bit_util::PopCount(u64_data[0]);
count_unroll[1] += bit_util::PopCount(u64_data[1]);
count_unroll[2] += bit_util::PopCount(u64_data[2]);
count_unroll[3] += bit_util::PopCount(u64_data[3]);
u64_data += kCountUnrollFactor;
}
for (int64_t k = 0; k < kCountUnrollFactor; k++) {
Expand Down
Loading