From d75f2822d3d56dfa1c98de2bbb7eaa4dac5ff8f3 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Thu, 11 Dec 2025 18:48:37 +0800 Subject: [PATCH] core/types: use new atomic types in caches #29411 (#1793) --- core/types/block.go | 16 ++++++++-------- core/types/transaction.go | 14 +++++++------- core/types/transaction_signing.go | 7 +++---- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 0ae8439485..edc1184012 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -201,8 +201,8 @@ type Block struct { transactions Transactions // caches - hash atomic.Value - size atomic.Value + hash atomic.Pointer[common.Hash] + size atomic.Uint64 // Td is used by package core to store the total difficulty // of the chain up to and including the block. @@ -413,8 +413,8 @@ func (b *Block) HashNoValidator() common.Hash { // Size returns the true RLP encoded storage size of the block, either by encoding // and returning it, or returning a previsouly cached value. func (b *Block) Size() uint64 { - if size := b.size.Load(); size != nil { - return size.(uint64) + if size := b.size.Load(); size > 0 { + return size } c := writeCounter(0) rlp.Encode(&c, b) @@ -468,11 +468,11 @@ func (b *Block) WithBody(body Body) *Block { // The hash is computed on the first call and cached thereafter. func (b *Block) Hash() common.Hash { if hash := b.hash.Load(); hash != nil { - return hash.(common.Hash) + return *hash } - v := b.header.Hash() - b.hash.Store(v) - return v + h := b.header.Hash() + b.hash.Store(&h) + return h } func (b *Block) String() string { diff --git a/core/types/transaction.go b/core/types/transaction.go index c9e3c28beb..15f692458b 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -63,9 +63,9 @@ type Transaction struct { time time.Time // Time first seen locally (spam avoidance) // caches - hash atomic.Value - size atomic.Value - from atomic.Value + hash atomic.Pointer[common.Hash] + size atomic.Uint64 + from atomic.Pointer[sigCache] } // NewTx creates a new transaction. @@ -399,7 +399,7 @@ func (tx *Transaction) EffectiveGasTipIntCmp(other *big.Int, baseFee *big.Int) i // Hash returns the transaction hash. func (tx *Transaction) Hash() common.Hash { if hash := tx.hash.Load(); hash != nil { - return hash.(common.Hash) + return *hash } var h common.Hash @@ -408,15 +408,15 @@ func (tx *Transaction) Hash() common.Hash { } else { h = prefixedRlpHash(tx.Type(), tx.inner) } - tx.hash.Store(h) + tx.hash.Store(&h) return h } // Size returns the true encoded storage size of the transaction, either by encoding // and returning it, or returning a previously cached value. func (tx *Transaction) Size() uint64 { - if size := tx.size.Load(); size != nil { - return size.(uint64) + if size := tx.size.Load(); size > 0 { + return size } // Cache miss, encode and cache. diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index 31fe31cc56..25976d7a95 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -123,8 +123,7 @@ func Sender(signer Signer, tx *Transaction) (common.Address, error) { return common.Address{}, ErrInvalidNilTx } - if sc := tx.from.Load(); sc != nil { - sigCache := sc.(sigCache) + if sigCache := tx.from.Load(); sigCache != nil { // If the signer used to derive from in a previous // call is not the same as used current, invalidate // the cache. @@ -137,7 +136,7 @@ func Sender(signer Signer, tx *Transaction) (common.Address, error) { if err != nil { return common.Address{}, err } - tx.from.Store(sigCache{signer: signer, from: addr}) + tx.from.Store(&sigCache{signer: signer, from: addr}) return addr, nil } @@ -509,5 +508,5 @@ func CacheSigner(signer Signer, tx *Transaction) { if err != nil { return } - tx.from.Store(sigCache{signer: signer, from: addr}) + tx.from.Store(&sigCache{signer: signer, from: addr}) }