New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slice: use reflect.Append
instead of pre-allocating whole slice.
#7
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5cb550a
Decoding slices: use `reflect.Append` instead of allocating slice.
gagliardetto d8654f8
Fix borsh test
gagliardetto 4dc4569
readNBytes: add limit checks; ReadRustString: add limit check; add Di…
gagliardetto d94c910
Add tests for go 1.19.x
gagliardetto c0cb6ac
More tests
gagliardetto 1ff28ef
Add specialized uint array/slice encoders
gagliardetto a8f0149
Add optimized uint array/slice encoders
gagliardetto 179e9d3
Reduce usage of reflect
gagliardetto 90ac7a1
Make tests more uniform
gagliardetto e6ae947
More tests
gagliardetto 06d42a1
Improve benchmarks
gagliardetto df95dbc
Add benchmark for uint32 slices/arrays
gagliardetto 5788bc1
Add missing checks to benchmarks
gagliardetto 76b2308
Add bench for custom decoder
gagliardetto b4927dd
Cleanup
gagliardetto File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package bin | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkDecodeUintSlice(b *testing.B) { | ||
buf := concatByteSlices( | ||
// length: | ||
uint32ToBytes(8, LE), | ||
// data: | ||
uint32ToBytes(0, LE), | ||
uint32ToBytes(1, LE), | ||
uint32ToBytes(2, LE), | ||
uint32ToBytes(3, LE), | ||
uint32ToBytes(4, LE), | ||
uint32ToBytes(5, LE), | ||
uint32ToBytes(6, LE), | ||
uint32ToBytes(7, LE), | ||
) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
got := make([]uint32, 0) | ||
decoder := NewBorshDecoder(buf) | ||
err := decoder.Decode(&got) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can save a lot of bounds checking here by
Same for all the other primitive arrays and slices
reflect.Append
is worse than pre-allocation when the slice size is known because the end result is the same (in terms of memory usage), but the former is less efficientThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two more optimization ideas: