Skip to content

Conversation

@MariusVanDerWijden
Copy link
Member

@MariusVanDerWijden MariusVanDerWijden commented Feb 25, 2025

This PR roughly halves the number of allocations needed to compute the sigHash for a transaction.
This sigHash is used whenever we recover a signature of a transaction, so quite often. During a recent benchmark full syncing on Holesky, roughly 2.8% of all allocations were happening here because the fields from the transaction would be copied multiple times.

66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}

This PR reduces the allocations and speeds up the computation of the sigHash by ~22%, which is quite significantly given that this operation involves a call to Keccak

// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)

It also kinda cleans up stuff in my opinion, since the transaction should itself know best how to compute the sighash

Screenshot_2025-02-25_13-52-41

Copy link
Member

Choose a reason for hiding this comment

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

It's actually Post-EIP155

rjl493456442
rjl493456442 previously approved these changes Feb 26, 2025
@rjl493456442
Copy link
Member

Let's deploy it on benchmarks before merging it.

@rjl493456442 rjl493456442 force-pushed the reduce-allocs-signer branch from bf896e8 to d3f230f Compare March 18, 2025 13:56
@rjl493456442 rjl493456442 changed the title Reduce allocs signer core/types: reduce allocs in transaction signing Mar 19, 2025
@rjl493456442 rjl493456442 added this to the 1.15.6 milestone Mar 19, 2025
@rjl493456442 rjl493456442 merged commit b47e4d5 into ethereum:master Mar 19, 2025
4 checks passed
sivaratrisrinivas pushed a commit to sivaratrisrinivas/go-ethereum that referenced this pull request Apr 21, 2025
This PR roughly halves the number of allocations needed to compute the
sigHash for a transaction.

This sigHash is used whenever we recover a signature of a transaction,
so quite often. During a recent benchmark full syncing on Holesky,
roughly 2.8% of all allocations were happening here because the fields
from the transaction would be copied multiple times.

```
66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}
```

This PR reduces the allocations and speeds up the computation of the
sigHash by ~22%, which is quite significantly given that this operation
involves a call to Keccak
```
// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
```

```
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)
```

It also kinda cleans up stuff in my opinion, since the transaction
should itself know best how to compute the sighash




![Screenshot_2025-02-25_13-52-41](https://github.com/user-attachments/assets/e2b268aa-e137-417d-926b-f3619daef748)

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
sduchesneau pushed a commit to streamingfast/go-ethereum that referenced this pull request May 22, 2025
This PR roughly halves the number of allocations needed to compute the
sigHash for a transaction.

This sigHash is used whenever we recover a signature of a transaction,
so quite often. During a recent benchmark full syncing on Holesky,
roughly 2.8% of all allocations were happening here because the fields
from the transaction would be copied multiple times.

```
66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}
```

This PR reduces the allocations and speeds up the computation of the
sigHash by ~22%, which is quite significantly given that this operation
involves a call to Keccak
```
// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
```

```
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)
```

It also kinda cleans up stuff in my opinion, since the transaction
should itself know best how to compute the sighash




![Screenshot_2025-02-25_13-52-41](https://github.com/user-attachments/assets/e2b268aa-e137-417d-926b-f3619daef748)

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
jakub-freebit pushed a commit to fblch/go-ethereum that referenced this pull request Jul 3, 2025
This PR roughly halves the number of allocations needed to compute the
sigHash for a transaction.

This sigHash is used whenever we recover a signature of a transaction,
so quite often. During a recent benchmark full syncing on Holesky,
roughly 2.8% of all allocations were happening here because the fields
from the transaction would be copied multiple times.

```
66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}
```

This PR reduces the allocations and speeds up the computation of the
sigHash by ~22%, which is quite significantly given that this operation
involves a call to Keccak
```
// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
```

```
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)
```

It also kinda cleans up stuff in my opinion, since the transaction
should itself know best how to compute the sighash




![Screenshot_2025-02-25_13-52-41](https://github.com/user-attachments/assets/e2b268aa-e137-417d-926b-f3619daef748)

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
howjmay pushed a commit to iotaledger/go-ethereum that referenced this pull request Aug 27, 2025
This PR roughly halves the number of allocations needed to compute the
sigHash for a transaction.

This sigHash is used whenever we recover a signature of a transaction,
so quite often. During a recent benchmark full syncing on Holesky,
roughly 2.8% of all allocations were happening here because the fields
from the transaction would be copied multiple times.

```
66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}
```

This PR reduces the allocations and speeds up the computation of the
sigHash by ~22%, which is quite significantly given that this operation
involves a call to Keccak
```
// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
```

```
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)
```

It also kinda cleans up stuff in my opinion, since the transaction
should itself know best how to compute the sighash




![Screenshot_2025-02-25_13-52-41](https://github.com/user-attachments/assets/e2b268aa-e137-417d-926b-f3619daef748)

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
gballet pushed a commit to gballet/go-ethereum that referenced this pull request Sep 11, 2025
This PR roughly halves the number of allocations needed to compute the
sigHash for a transaction.

This sigHash is used whenever we recover a signature of a transaction,
so quite often. During a recent benchmark full syncing on Holesky,
roughly 2.8% of all allocations were happening here because the fields
from the transaction would be copied multiple times.

```
66168733  153175654 (flat, cum)  2.80% of Total
         .          .    368:func (s londonSigner) Hash(tx *Transaction) common.Hash {
         .          .    369:	if tx.Type() != DynamicFeeTxType {
         .          .    370:		return s.eip2930Signer.Hash(tx)
         .          .    371:	}
         .   19169966    372:	return prefixedRlpHash(
         .          .    373:		tx.Type(),
  26442187   26442187    374:		[]interface{}{
         .          .    375:			s.chainId,
   6848616    6848616    376:			tx.Nonce(),
         .   19694077    377:			tx.GasTipCap(),
         .   18956774    378:			tx.GasFeeCap(),
   6357089    6357089    379:			tx.Gas(),
         .   12321050    380:			tx.To(),
         .   16865054    381:			tx.Value(),
  13435187   13435187    382:			tx.Data(),
  13085654   13085654    383:			tx.AccessList(),
         .          .    384:		})
         .          .    385:}
```

This PR reduces the allocations and speeds up the computation of the
sigHash by ~22%, which is quite significantly given that this operation
involves a call to Keccak
```
// BenchmarkHash-8   	  440082	      2639 ns/op	     384 B/op	      13 allocs/op
// BenchmarkHash-8   	  493566	      2033 ns/op	     240 B/op	       6 allocs/op
```

```
Hash-8   2.691µ ± 8%   2.097µ ± 9%  -22.07% (p=0.000 n=10)
```

It also kinda cleans up stuff in my opinion, since the transaction
should itself know best how to compute the sighash




![Screenshot_2025-02-25_13-52-41](https://github.com/user-attachments/assets/e2b268aa-e137-417d-926b-f3619daef748)

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants