Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

perf(reduce): replace per-cell indexing in reduce with strided copy - #448

Open
henryiii wants to merge 5 commits into
boostorg:developfrom
henryiii:perf/reduce-strided-copy
Open

henryiii wants to merge 5 commits into
boostorg:developfrom
henryiii:perf/reduce-strided-copy

Conversation

@henryiii

@henryiii henryiii commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

This is a follow-up to #429. This now runs faster than the numpy version of this operation in boost-histogram.

I have a followup on this followup as well that gets another performance boost for smaller growing axes (1.2-2x) and combines parts of reduce (reduce_axis_map and reduce_fill) and storage_grower (multi-index loop and per-cell for_each_axis) into a single cell_map (roughly the same number of LoC). I can't make a stacked PR on a branch in a fork, so I will open it as a draft that depends on this PR, and can rebase it if this goes in.

🤖 AI text below 🤖

reduce visited every input cell with indexed() and called result.at(idx) for each one, which recomputes the linear index through the axes. This PR builds one lookup table per axis, which maps each stored input position to an output offset or a skip marker, and runs a nested strided loop over raw storage. Skipped outer slabs are skipped as a block, so crop and pick with slice_mode::crop scale with the output size. For arithmetic dense storages, runs of input cells that land in the same output cell are summed in a local accumulator, in the same order as before. Tables with up to 256 entries live on the stack.

Benchmark on Apple Silicon, 3 × category<std::string>(32) + regular(32), dense double storage:

Case Before After
pick, overflow 5960 µs 506 µs
pick, crop 2314 µs 1.3 µs
pick + slice 6091 µs 676 µs
pick, unlimited storage 7225 µs 3496 µs

The existing rebin/shrink benchmarks improve as well: 3D dense up to 13x, 2D dense up to 7x, unlimited 3D about 3.5x, small 1D histograms 1.2x to 2.5x.

Also adds Pick, PickCrop, and PickAndSlice benchmarks which mirror the boost-histogram Python benchmarks, and a test with 300 × 300 bins which exercises the heap-backed lookup table. The new test passes against the old implementation too.

reduce visited every input cell through indexed() and computed the
output index with result.at(idx) per cell. It now builds one lookup
table per axis which maps each stored input position to an output
offset, or marks it as skipped, and runs a nested strided loop over the
raw storage. Skipped outer slabs are skipped as a block, so crop and
pick in crop mode scale with the output size. For arithmetic dense
storages, runs of input cells which land in the same output cell are
summed in a local accumulator, in the same order as before.

Adds pick benchmarks which mirror the boost-histogram python
benchmarks, and a test with large axes which exercises the heap-backed
lookup table.

Assisted-by: ClaudeCode:claude-fable-5-1
ClaudeCode:claude-fable-5-1
Drop reduce_command::lut and fill the per-axis table for pick commands
directly, which removes a heap allocation per picked axis and the
underflow special case. Use axis::traits::extent for the axis extent and
share the setup of the pick benchmarks.

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5
@henryiii

henryiii commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

The stack reduce branch saves a little time, but I don't think it's worth it (done manually, there is boost::container::small_vector which would be less code but that would add a new dependency). It's 15% faster for a 4 element histogram, and since it's a constant time (12 ns), it drops off for larger histograms. I'll remove it, but leave it in the history if you want to see it. (Edit: it's not a ton of code, though, to be fair, so if you do like it, I can revert. I still don't think it's worth it, not for small reductions.)

The stack buffer saved about 12 ns per call, visible only on 1D
histograms with few bins.

Assisted-by: ClaudeCode:claude-fable-5-1
Assisted-by: ClaudeCode:claude-opus-5-5
@henryiii
henryiii marked this pull request as ready for review November 2, 2026 15:09
@henryiii henryiii changed the title perf: replace per-cell indexing in reduce with strided copy perf(reduce): replace per-cell indexing in reduce with strided copy Nov 2, 2026

This branch has not been deployed

No deployments
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