Skip to content

perf: replace two-comparison bounds check with single unsigned cast#136

Open
fcostaoliveira wants to merge 1 commit into
HdrHistogram:mainfrom
fcostaoliveira:perf/opt5-unsigned-bounds-check
Open

perf: replace two-comparison bounds check with single unsigned cast#136
fcostaoliveira wants to merge 1 commit into
HdrHistogram:mainfrom
fcostaoliveira:perf/opt5-unsigned-bounds-check

Conversation

@fcostaoliveira
Copy link
Copy Markdown

@fcostaoliveira fcostaoliveira commented May 25, 2026

Summary

  • Replace counts_index < 0 || h->counts_len <= counts_index with (uint32_t)counts_index >= (uint32_t)h->counts_len in hdr_record_values and hdr_record_values_atomic
  • For valid non-negative input, counts_index is always >= 0 by construction — the negative check is dead code
  • One unsigned comparison vs two signed comparisons, reducing branch count on the hot path

Benchmark (hdr_record_value throughput, ops/sec)

Baseline Optimized Delta
avg last 5 iters 261,584,518 319,067,198 +22.0%

Steps to reproduce

# Build baseline (main)
git clone https://github.com/HdrHistogram/HdrHistogram_c.git hdr_baseline
cd hdr_baseline && mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DHDR_HISTOGRAM_BUILD_PROGRAMS=ON
make hdr_histogram_perf -j$(nproc)
./test/hdr_histogram_perf        # note ops/sec from last 5 iterations

# Build this PR
cd ../../
git clone https://github.com/fcostaoliveira/HdrHistogram_c.git hdr_opt
cd hdr_opt && git checkout perf/opt5-unsigned-bounds-check
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DHDR_HISTOGRAM_BUILD_PROGRAMS=ON
make hdr_histogram_perf -j$(nproc)
./test/hdr_histogram_perf        # compare ops/sec from last 5 iterations

The benchmark (test/hdr_histogram_perf.c) records 400M values in a tight loop and reports ops/sec per iteration. Use the last 5 of 100 iterations for steady-state throughput.

Notes

  • Behavior-preserving: any value rejected before is still rejected
  • Full test suite passes (5/5)

🤖 Generated with Claude Code

In hdr_record_values (and atomic variant), replace the bounds check
  if (counts_index < 0 || h->counts_len <= counts_index)
with a single unsigned comparison
  if ((uint32_t)counts_index >= (uint32_t)h->counts_len)

For valid input, counts_index is always >= 0 by construction, so the
negative check is dead code. The unsigned cast eliminates it and reduces
the check to one branch instruction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant