Skip to content

Commit

Permalink
statemanager: re architect shallow copy to improve tree shaking (#3596)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau authored Aug 15, 2024
1 parent 7b4e98d commit 66d5993
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
37 changes: 37 additions & 0 deletions packages/statemanager/src/cache/caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 6 additions & 22 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*
Expand All @@ -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),
})
}

Expand Down

0 comments on commit 66d5993

Please sign in to comment.