perf: optimize Zip and ZipBy functions#818
Merged
samber merged 1 commit intosamber:masterfrom Feb 25, 2026
Merged
Conversation
- Replace Nth() with NthOrEmpty() to avoid unnecessary error allocation on miss - Change make(0, size) + append to make(size) + direct assignment - Use uint for loop indices - Remove temporary variables in ZipBy functions This reduces overhead from append operations and structure creation, similar to the Zip5Copy2 optimization pattern. Reduces code size: +174/-242 lines
Contributor
Author
|
There is probably no need for this But to improve performance, you can do something like this func Zip5My2[A, B, C, D, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E] {
minSize, maxSize := MinMax(len(a), len(b), len(c), len(d), len(e))
result := make([]Tuple5[A, B, C, D, E], maxSize)
index := uint(0)
_ = a[minSize-1]
_ = b[minSize-1]
_ = c[minSize-1]
_ = d[minSize-1]
_ = e[minSize-1]
_ = result[minSize-1]
for ; index < minSize; index++ {
result[index].A = a[index]
result[index].B = b[index]
result[index].C = c[index]
result[index].D = d[index]
result[index].E = e[index]
}
for ; index < maxSize; index++ {
result[index].A = NthOrEmpty(a, index)
result[index].B = NthOrEmpty(b, index)
result[index].C = NthOrEmpty(c, index)
result[index].D = NthOrEmpty(d, index)
result[index].E = NthOrEmpty(e, index)
}
return result
} |
Owner
|
Why do you mount this into memory? _ = a[minSize-1]
_ = b[minSize-1]
_ = c[minSize-1]
_ = d[minSize-1]
_ = e[minSize-1]
_ = result[minSize-1]Is there a Go optimization behind? |
Contributor
Author
|
Yeap, it eliminates bounds checking in the next cycle |
Owner
|
Yes, feel free to create a PR if you see a performance improvement. (with proper comments to explain the optimizations please!) |
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.
This reduces overhead from append operations and structure creation, similar to the Zip5Copy2 optimization pattern.
Reduces code size: +174/-242 lines