perf(builtin): min, max, mean, median fast paths#909
Merged
antonmedv merged 2 commits intoexpr-lang:masterfrom Jan 23, 2026
Merged
perf(builtin): min, max, mean, median fast paths#909antonmedv merged 2 commits intoexpr-lang:masterfrom
antonmedv merged 2 commits intoexpr-lang:masterfrom
Conversation
Member
|
Nice! I like such optimizations! |
Contributor
Author
|
Thanks! So much fun :) Needs more tests and fine-tuning, but I'll mark it ready once there 🤞 |
b9bb62d to
0918bbd
Compare
Avoid reflection and per-element allocations for common typed slices ([]int, []float64, []any) by adding type-switch fast paths that iterate directly without calling reflect.Value.Interface(). For []any containing numeric types, the fast path handles int and float64 directly, falling back to reflection for other numeric types (int32, etc.) to keep the code compact while still avoiding per-element recursion. Falls back to reflection for other slice types to maintain compatibility. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
0918bbd to
4e035dd
Compare
antonmedv
approved these changes
Jan 23, 2026
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
The
min,max,mean, andmedianbuiltin functions were using reflection for every element in the input array, callingreflect.Value.Index(i).Interface()in a loop. This causes significant allocations (100+ per call for 100 elements) because each.Interface()call triggers heap allocations.Changes
Avoid reflection and per-element allocations for common typed slices (
[]int,[]float64,[]any) by adding type-switch fast paths that iterate directly without callingreflect.Value.Interface().Added benchmarks:
Benchmark_minBenchmark_maxBenchmark_meanBenchmark_medianBenchstat results against
masterbranch:Further comments
Other typed slices (e.g.,
[]int32,[]uint64, custom numeric types) fall back to the original reflection-based path. Open to feedback whether we should include more typed slices here.Similar optimizations could potentially be applied to other builtins like
sum.