1
0
Fork 0
forked from forks/go-ethereum
go-ethereum-modded-tocallarg/trie
rjl493456442 4dfec7e83e
trie: optimize memory allocation (#30932)
This pull request removes the node copy operation to reduce memory
allocation. Key Changes as below:

**(a) Use `decodeNodeUnsafe` for decoding nodes retrieved from the trie
node reader**

In the current implementation of the MPT, once a trie node blob is
retrieved, it is passed to `decodeNode` for decoding. However,
`decodeNode` assumes the supplied byte slice might be mutated later, so
it performs a deep copy internally before parsing the node.

Given that the node reader is implemented by the path database and the
hash database, both of which guarantee the immutability of the returned
byte slice. By restricting the node reader interface to explicitly
guarantee that the returned byte slice will not be modified, we can
safely replace `decodeNode` with `decodeNodeUnsafe`. This eliminates the
need for a redundant byte copy during each node resolution.

**(b) Modify the trie in place**

In the current implementation of the MPT, a copy of a trie node is
created before any modifications are made. These modifications include:
- Node resolution: Converting the value from a hash to the actual node.
- Node hashing: Tagging the hash into its cache.
- Node commit: Replacing the children with its hash.
- Structural changes: For example, adding a new child to a fullNode or
replacing a child of a shortNode.

This mechanism ensures that modifications only affect the live tree,
leaving all previously created copies unaffected.

Unfortunately, this property leads to a huge memory allocation
requirement. For example, if we want to modify the fullNode for n times,
the node will be copied for n times.

In this pull request, all the trie modifications are made in place. In
order to make sure all previously created copies are unaffected, the
`Copy` function now will deep-copy all the live nodes rather than the
root node itself.

With this change, while the `Copy` function becomes more expensive, it's
totally acceptable as it's not a frequently used one. For the normal
trie operations (Get, GetNode, Hash, Commit, Insert, Delete), the node
copy is not required anymore.
2025-03-25 14:59:44 +01:00
..
trienode all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
utils all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
bytepool.go trie: reduce allocations in stacktrie (#30743) 2025-01-23 10:17:12 +01:00
committer.go trie: optimize memory allocation (#30932) 2025-03-25 14:59:44 +01:00
database_test.go core, trie, triedb: minor changes from snapshot integration (#30599) 2024-10-18 17:06:31 +02:00
encoding.go trie: reduce allocations in stacktrie (#30743) 2025-01-23 10:17:12 +01:00
encoding_test.go trie: reduce allocs in recHash (#27770) 2023-08-18 22:41:19 +02:00
errors.go all: fix various typos (#29600) 2024-04-23 13:09:42 +03:00
hasher.go trie: optimize memory allocation (#30932) 2025-03-25 14:59:44 +01:00
iterator.go trie: iterate values pre-order and fix seek behavior (#27838) 2024-06-04 11:17:26 +08:00
iterator_test.go core, trie, triedb: minor changes from snapshot integration (#30599) 2024-10-18 17:06:31 +02:00
node.go trie: optimize memory allocation (#30932) 2025-03-25 14:59:44 +01:00
node_enc.go trie: reduce allocations in stacktrie (#30743) 2025-01-23 10:17:12 +01:00
node_test.go trie: improve node rlp decoding performance (#25357) 2022-08-19 00:39:47 +02:00
proof.go trie: combine validation loops in VerifyRangeProof (#30823) 2024-11-28 17:17:58 +08:00
proof_test.go all: fix mismatched names in comments (#29348) 2024-03-26 21:01:28 +01:00
secure_trie.go trie: copy preimage store pointer in StateTrie.Copy (#31158) 2025-02-12 09:55:00 +08:00
secure_trie_test.go core/state, eth/protocols, trie, triedb/pathdb: remove unused error from trie Commit (#29869) 2024-06-12 12:23:16 +03:00
stacktrie.go trie: reduce allocations in stacktrie (#30743) 2025-01-23 10:17:12 +01:00
stacktrie_fuzzer_test.go trie: do not expect ordering in stacktrie during fuzzing (#31170) 2025-02-18 10:48:42 +08:00
stacktrie_test.go trie: reduce allocations in stacktrie (#30743) 2025-01-23 10:17:12 +01:00
sync.go trie: update the valid function comments (#29809) 2024-05-21 19:53:34 +08:00
sync_test.go core, trie, triedb: minor changes from snapshot integration (#30599) 2024-10-18 17:06:31 +02:00
tracer.go trie, consensus/clique: use maps.Clone (#29616) 2024-04-24 14:27:58 +02:00
tracer_test.go core/state, eth/protocols, trie, triedb/pathdb: remove unused error from trie Commit (#29869) 2024-06-12 12:23:16 +03:00
trie.go trie: optimize memory allocation (#30932) 2025-03-25 14:59:44 +01:00
trie_id.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
trie_reader.go triedb/pathdb: configure different node hasher in pathdb (#31008) 2025-01-10 20:51:19 +08:00
trie_test.go trie: optimize memory allocation (#30932) 2025-03-25 14:59:44 +01:00
verkle.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
verkle_test.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00