Repository navigation
Conversation
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
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::indexruns 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 tosize().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):spanargumentvariant<double, span>argument-fno-trapping-mathIndependent 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-mathrow may be slightly slower; it needs a repeat on x86 before this is taken out of draft.