Skip to content

[WIP] perf(fill): make regular::index branch-free - #452

Draft
henryiii wants to merge 3 commits into
boostorg:developfrom
henryiii:perf/branchless-regular-index
Draft

henryiii wants to merge 3 commits into
boostorg:developfrom
henryiii:perf/branchless-regular-index

Conversation

@henryiii

@henryiii henryiii commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

WIP: This avoids branch prediction on the fill hot loop. For fully random values, this can be up to a 1.7x speed up. This is complementary to #451. There are slowdowns too, so this is in draft while I see if I can get those under control. #453 helps in some cases, but there are still regressions I'm working on.

🤖 AI text below 🤖

regular::index runs in the fill hot loop, and on data that is partly out of range its two range checks mispredict. This PR computes both comparisons first and selects the result. The value is clamped to [0, 1] before the conversion to int, so the conversion is in range for any input, including NaN, which still maps to size().

The result is identical to the previous code: a check over 4M values (NaN, both infinities, denormals, values on either side of every edge, uniform and normal random data) across six axis/option combinations reports 0 mismatches, on clang and gcc.

Measured with a 1D regular<> axis, dense_storage<int64_t> and 1M normally distributed doubles on [-1, 1], ns/element (Apple M5 Pro, aarch64):

develop this PR #451 both
clang, span argument 0.8 0.8 0.8 0.8
clang, variant<double, span> argument 3.4 1.1 0.8 0.75
gcc 14, default flags 3.6 2.2 3.6 2.1
gcc 14, -fno-trapping-math 0.8 1.0 0.8 0.8

Independent of #451 and complementary to it. gcc still does not vectorize this loop with its default -ftrapping-math, because it treats the int/double conversions as possibly trapping, but the branch-free form removes the mispredictions. The gcc -fno-trapping-math row may be slightly slower; it needs a repeat on x86 before this is taken out of draft.

Random data mispredicts the range checks in the hot loop. Compute the
comparisons first and select the result, with the value clamped so the
conversion to int is in range for any input, including NaN. The result
is identical to the previous code for all inputs.

1D regular axis, 1M normal doubles, ns/element:
clang, variant argument 3.4 -> 1.1; gcc 14 default flags 3.6 -> 2.2.

Assisted-by: ClaudeCode:claude-fable-5-1
@henryiii henryiii changed the title perf: make regular::index branch-free perf(fill): make regular::index branch-free Nov 2, 2026
Replaces the nested ternary clamp with fmax(fmin(z, 1), 0). This maps
to min/max instructions on clang and on gcc with -fno-trapping-math,
and halves most of the remaining regressions vs develop.

Assisted-by: ClaudeCode:claude-fable-5-1
The 128 kB buffer spills out of L1 on recent CPUs. With 32 kB, bulk
fills where every value lands in the same bin run 2x faster on Apple M5
with clang and gcc; other cases are unchanged. Stack usage drops as well.

Assisted-by: ClaudeCode:claude-fable-5-1
@henryiii henryiii changed the title perf(fill): make regular::index branch-free [WIP] perf(fill): make regular::index branch-free Nov 3, 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