core/types, internal/ethapi, signer/core/apitypes: avoid copying 128KB blobs in range loops#33717
Merged
rjl493456442 merged 2 commits intoethereum:masterfrom Feb 3, 2026
Merged
Conversation
rjl493456442
previously approved these changes
Jan 30, 2026
Member
|
Can you demonstrate the allocation difference? My test doesn't show any allocation. // BenchmarkAllocation-8 24739767 48.65 ns/op 0 B/op 0 allocs/op
func BenchmarkAllocation(b *testing.B) {
var slice [][4096]byte
for i := 0; i < 128; i++ {
slice = append(slice, [4096]byte{0xa, 0xb})
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, blob := range slice {
_ = blob
}
}
} |
Contributor
Author
|
The compiler optimizes away the copy when blob isn't used. Here's a test that shows the actual difference: func BenchmarkBlobToCommitmentIndex(b *testing.B) {
blobs := make([]kzg4844.Blob, 6)
for i := 0; i < b.N; i++ {
for j := range blobs {
kzg4844.BlobToCommitment(&blobs[j])
}
}
}
func BenchmarkBlobToCommitmentCopy(b *testing.B) {
blobs := make([]kzg4844.Blob, 6)
for i := 0; i < b.N; i++ {
for _, blob := range blobs {
kzg4844.BlobToCommitment(&blob)
}
}
}Results: |
Member
|
I don't see much of a difference tbh: |
MariusVanDerWijden
previously approved these changes
Feb 2, 2026
Member
|
I will on hold this PR if it doesn't show the difference in terms of allocation. |
Contributor
Author
|
Your benchmark ran only once (N=1) which gives unreliable results due to variance. With more iterations (-benchtime=5s, N~2500): The allocation count is the same because the copy happens on the stack, not heap. |
Member
|
OK. Can you rebase and resolve the conflicts? |
2da1077
Contributor
Author
|
@rjl493456442 resolve conflicts |
rjl493456442
approved these changes
Feb 3, 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.
kzg4844.Blob is 131072 bytes. Using
for _, blob := rangecopies the entire blob on each iteration. With up to 6 blobs per transaction, this wastes ~768KB of memory copies.Switch to index-based iteration and pass pointers directly.