Skip to content

crypto: zero-alloc, syncpool-free method to hash []byte type#20114

Merged
awskii merged 16 commits into
release/3.4from
alex/crypto_hash_no_heap_leak_34
Mar 28, 2026
Merged

crypto: zero-alloc, syncpool-free method to hash []byte type#20114
awskii merged 16 commits into
release/3.4from
alex/crypto_hash_no_heap_leak_34

Conversation

@AskAlexSharov

Copy link
Copy Markdown
Collaborator

problem: sha.Read(h[:]) escaping to heap

  • in RLP func rlpHash used new sha.Sum256() method - to prevent heap-escaping caused by sha.Read(h[:]) intereface-method:
if h, ok := sha.(*keccak.Hasher); ok {
	return h.Sum256()
}	
  • in crypto.HashData(): used new keccak.Sum256() method - which doesn't use sync.Pool and doesn't alloc (-20ns)
  ┌─────────────────────────────────┬───────┬──────┬───────────┐                                   
  │             Branch              │ ns/op │ B/op │ allocs/op │                                   
  ├─────────────────────────────────┼───────┼──────┼───────────┤                                   
  │ release/3.4 (pool + Read(h[:])) │ 272   │ 32   │ 1         │                                   
  ├─────────────────────────────────┼───────┼──────┼───────────┤                                   
  │ current (keccak.Sum256 direct)  │ 240   │ 0    │ 0         │                                   
  └─────────────────────────────────┴───────┴──────┴───────────┘                                                                                                                               

}
if len(code) > 0 {
copy(u.CodeHash[:], crypto.Keccak256(code))
u.CodeHash = crypto.HashData(code)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also can be used for some cells during fold instead of hph.keccak

@awskii awskii merged commit 75281b4 into release/3.4 Mar 28, 2026
22 checks passed
@awskii awskii deleted the alex/crypto_hash_no_heap_leak_34 branch March 28, 2026 01:19
AskAlexSharov added a commit that referenced this pull request Apr 6, 2026
problem: `sha.Read(h[:])` escaping to heap

- in RLP `func rlpHash` used new `sha.Sum256()` method - to prevent
heap-escaping caused by `sha.Read(h[:])` intereface-method:
```
if h, ok := sha.(*keccak.Hasher); ok {
	return h.Sum256()
}
```
- in `crypto.HashData()`: used new `keccak.Sum256()` method - which
doesn't use `sync.Pool` and doesn't alloc (`-20ns`)

```
  ┌─────────────────────────────────┬───────┬──────┬───────────┐
  │             Branch              │ ns/op │ B/op │ allocs/op │
  ├─────────────────────────────────┼───────┼──────┼───────────┤
  │ release/3.4 (pool + Read(h[:])) │ 272   │ 32   │ 1         │
  ├─────────────────────────────────┼───────┼──────┼───────────┤
  │ current (keccak.Sum256 direct)  │ 240   │ 0    │ 0         │
  └─────────────────────────────────┴───────┴──────┴───────────┘
```

---------

Co-authored-by: awskii <awskii@users.noreply.github.com>
Co-authored-by: Giulio rebuffo <giulio.rebuffo@gmail.com>
github-merge-queue Bot pushed a commit that referenced this pull request Apr 6, 2026
Cherry-pick from release/3.4 to main.

Original PRs:
- #20335 — paged writer: lost amount of workers
- #20281 — pagedWriter and compressor to use same COMPRESS_WORKERS
variable
- #20262 — stageloop, execmodule: add ERIGON_IN_MEM_HISTORY env flag
- #20114 — crypto: zero-alloc, syncpool-free method to hash []byte type
- #20194 — execution/state: revert CodeSizePath in codeChange journal
entry

20 candidates checked total; 15 were already effectively on main.

---------

Co-authored-by: moskud <sudeepdino008@gmail.com>
Co-authored-by: awskii <awskii@users.noreply.github.com>
Co-authored-by: Giulio rebuffo <giulio.rebuffo@gmail.com>
Co-authored-by: lystopad <oleksandr.lystopad@erigon.tech>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants