perf: numeric fast path for std.member array search#901
Open
He-Pin wants to merge 1 commit into
Open
Conversation
Motivation: `std.member` on arrays uses `ev.equal()` for every element comparison, which involves virtual dispatch and two pattern matches per element. For numeric arrays (common in Jsonnet), this overhead is unnecessary. Modification: When the search value is `Val.Num`, extract the double once and use direct double comparison in the loop, skipping the `ev.equal()` virtual dispatch entirely. Result: Native: member benchmark 7.1 → 7.0ms (-2.4%) JVM: flat (JIT already specializes the pattern match)
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.
Motivation
std.memberon arrays usesev.equal()for every element comparison, which involves virtual dispatch and two pattern matches per element. For numeric arrays (common in Jsonnet — ASCII code arrays, index lists), this overhead is unnecessary sinceVal.Numequality is a simple double comparison.Modification
When the search value is
Val.Num, extract the double once and use directasDouble == targetcomparison in the loop, bypassingev.equal()entirely. Non-numeric elements are correctly skipped (a number can never equal a non-number). The genericev.equal()path is retained for non-numeric search values.Result
Native A/B (Scala Native 0.5.12, hyperfine --warmup 10 --min-runs 30):
The benchmark is startup-dominated (internal compute ~1.6ms out of 6.7ms total). On JVM, the JIT already specializes the pattern match, so the improvement is negligible. On Native, the virtual dispatch savings are real but masked by startup overhead in this micro-benchmark. The optimization benefits longer-running workloads where
std.memberis called in loops.References