perf(vm): optimize loop iteration with scope pool#908
Open
thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
Open
perf(vm): optimize loop iteration with scope pool#908thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
Conversation
Contributor
Author
|
The fuzzing finding here looks to be valid but unrelated to this change. I'll spawn another PR from this. |
f7eb7fd to
705ccbe
Compare
Iteration over slices previously used reflection to access elements, which was slow and allocated unnecessarily. This change adds type-specialized fast paths for common slice types ([]int, []float64, []string, []any) that bypass reflection entirely. Scope objects are now pooled and reused across loop iterations. The current scope pointer is cached to avoid repeated slice lookups. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
705ccbe to
9e6a2e6
Compare
Member
|
Nice! Another thing what will be cool to investigate is how to iterate []struct arrays. This is the most common operation. Any possibility is using pointer arithmetic (maybe behind a build flag) |
Contributor
Author
|
Interesting idea, so basically store |
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
Iteration over slices previously used reflection to access elements, which was slow and allocated unnecessarily.
Changes
This change adds type-specialized fast paths for common slice types (
[]int,[]float64,[]string,[]any) that bypass reflection entirely. Scope objects are now pooled and reused across loop iterations. The current scope pointer is cached to avoid repeated slice lookups.Bench run:
Results:
Further comments
As the
Scopestruct is bigger, so is the B/op for filterFirst and filterLast. They are faster still.