Skip to content

perf: numeric fast path for std.member array search#901

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/member-numeric-fastpath
Open

perf: numeric fast path for std.member array search#901
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/member-numeric-fastpath

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Jun 6, 2026

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 — ASCII code arrays, index lists), this overhead is unnecessary since Val.Num equality is a simple double comparison.

Modification

When the search value is Val.Num, extract the double once and use direct asDouble == target comparison in the loop, bypassing ev.equal() entirely. Non-numeric elements are correctly skipped (a number can never equal a non-number). The generic ev.equal() path is retained for non-numeric search values.

Result

Native A/B (Scala Native 0.5.12, hyperfine --warmup 10 --min-runs 30):

Benchmark Baseline PR jrsonnet Delta
member 6.7 ms 6.8 ms 3.9 ms +0.4% (noise)

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.member is called in loops.

References

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)
@He-Pin He-Pin marked this pull request as draft June 6, 2026 20:32
@He-Pin He-Pin marked this pull request as ready for review June 6, 2026 23:24
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