From 66d599310b8f113aa8f80d3d2367d85e5622fee0 Mon Sep 17 00:00:00 2001 From: Gabriel Rocheleau Date: Thu, 15 Aug 2024 10:58:49 -0400 Subject: [PATCH] statemanager: re architect shallow copy to improve tree shaking (#3596) --- packages/statemanager/src/cache/caches.ts | 37 +++++++++++++++++++++++ packages/statemanager/src/stateManager.ts | 28 ++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/packages/statemanager/src/cache/caches.ts b/packages/statemanager/src/cache/caches.ts index 1373d02f16..fdfb47c3a1 100644 --- a/packages/statemanager/src/cache/caches.ts +++ b/packages/statemanager/src/cache/caches.ts @@ -81,6 +81,43 @@ export class Caches { this.storage?.clearStorage(address) } + shallowCopy(downlevelCaches: boolean) { + let cacheOptions: CachesStateManagerOpts | undefined + + // Account cache options + if (this.settings.account.size !== 0) { + cacheOptions = { + account: downlevelCaches + ? { size: this.settings.account.size, type: CacheType.ORDERED_MAP } + : this.settings.account, + } + } + + // Storage cache options + if (this.settings.storage.size !== 0) { + cacheOptions = { + ...cacheOptions, + storage: downlevelCaches + ? { size: this.settings.storage.size, type: CacheType.ORDERED_MAP } + : this.settings.storage, + } + } + + // Code cache options + if (this.settings.code.size !== 0) { + cacheOptions = { + ...cacheOptions, + code: downlevelCaches + ? { size: this.settings.code.size, type: CacheType.ORDERED_MAP } + : this.settings.code, + } + } + + if (cacheOptions !== undefined) { + return new Caches(cacheOptions) + } else return undefined + } + revert() { this.account?.revert() this.storage?.revert() diff --git a/packages/statemanager/src/stateManager.ts b/packages/statemanager/src/stateManager.ts index 7717544676..0fa21094f3 100644 --- a/packages/statemanager/src/stateManager.ts +++ b/packages/statemanager/src/stateManager.ts @@ -31,12 +31,12 @@ import { import debugDefault from 'debug' import { keccak256 } from 'ethereum-cryptography/keccak.js' -import { CacheType, OriginalStorageCache } from './cache/index.js' +import { OriginalStorageCache } from './cache/index.js' import { modifyAccountFields } from './util.js' -import { CODEHASH_PREFIX, Caches, type DefaultStateManagerOpts } from './index.js' +import { CODEHASH_PREFIX, type DefaultStateManagerOpts } from './index.js' -import type { StorageProof } from './index.js' +import type { Caches, StorageProof } from './index.js' import type { AccountFields, Proof, @@ -901,8 +901,8 @@ export class DefaultStateManager implements StateManagerInterface { * This means in particular: * 1. For caches instantiated as an LRU cache type * the copy() method will instantiate with an ORDERED_MAP cache - * instead, since copied instantances are mostly used in - * short-term usage contexts and LRU cache instantation would create + * instead, since copied instances are mostly used in + * short-term usage contexts and LRU cache instantiation would create * a large overhead here. * 2. The underlying trie object is initialized with 0 cache size * @@ -920,29 +920,13 @@ export class DefaultStateManager implements StateManagerInterface { const trie = this._trie.shallowCopy(false, { cacheSize }) const prefixCodeHashes = this._prefixCodeHashes const prefixStorageTrieKeys = this._prefixStorageTrieKeys - let accountCacheOpts = { ...this._caches?.settings.account } - if (downlevelCaches && this._caches?.settings.account.size !== 0) { - accountCacheOpts = { ...accountCacheOpts, type: CacheType.ORDERED_MAP } - } - let storageCacheOpts = { ...this._caches?.settings.storage } - if (downlevelCaches && this._caches?.settings.storage.size !== 0) { - storageCacheOpts = { ...storageCacheOpts, type: CacheType.ORDERED_MAP } - } - let codeCacheOpts = { ...this._caches?.settings.code } - if (this._caches?.settings.code.size !== 0) { - codeCacheOpts = { ...codeCacheOpts, type: CacheType.ORDERED_MAP } - } return new DefaultStateManager({ common, trie, prefixStorageTrieKeys, prefixCodeHashes, - caches: new Caches({ - account: accountCacheOpts, - code: codeCacheOpts, - storage: storageCacheOpts, - }), + caches: this._caches?.shallowCopy(downlevelCaches), }) }