-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Optimize memory buffer, simplify set32, use sha256-simd #7060
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
package utils | ||
|
||
import ( | ||
"crypto/sha256" | ||
"github.com/minio/sha256-simd" | ||
"hash" | ||
"sync" | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,9 @@ type Memory struct { | |
|
||
// NewMemory returns a new memory model. | ||
func NewMemory() *Memory { | ||
return &Memory{} | ||
return &Memory{ | ||
store: make([]byte, 0, 4*1024), | ||
} | ||
} | ||
|
||
// Set sets offset + size to value | ||
|
@@ -56,8 +58,7 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { | |
panic("invalid memory: store empty") | ||
} | ||
// Zero the memory area | ||
copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) | ||
// Fill in relevant bits | ||
copy(m.store[offset:offset+32], zeroes) | ||
val.WriteToSlice(m.store[offset:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed, thanks. WriteToSlice will write 32 bytes but I agree that specifying explicitly reads better, it was just the way I found it. |
||
} | ||
|
||
|
@@ -77,6 +78,11 @@ func (m *Memory) Resize(size uint64) { | |
m.store = append(m.store, zeroes[:l]...) | ||
} | ||
|
||
func (m *Memory) Reset() { | ||
m.lastGasCost = 0 | ||
m.store = m.store[:0] | ||
} | ||
|
||
// GetCopy returns offset + size as a new slice | ||
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { | ||
if size == 0 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package vtree | ||
|
||
import ( | ||
"crypto/sha256" | ||
"github.com/minio/sha256-simd" | ||
"math/big" | ||
"math/rand" | ||
"testing" | ||
|
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.
Mabye good. But I need run some experiment with Grafana to see an impact.
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.
Thanks for taking a look!
For the Celeron N5100 I'm left with
github.com/ledgerwatch/erigon/crypto/bn256/cloudflare.gfpMul
dominating 47% of the CPU profiling while before I had memory.go related calls. However I must admit that the SHA/SIMD change made a larger difference than the memory pool. This machine is at 100% CPU and no iowait as it has a good nvme on a slow CPU.Another machine syncing Ethereum mainnet on Intel 11800h using mechanical HDD with nvme lvm cache just had a minor CPU usage reduction as it is severely limited by the slow HDD IO.
If desired, I can split the changes into smaller PRs.