Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statemanager: re architect shallow copy to improve tree shaking #3596

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

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

👀

Ah, ok, now I get it.

So seems my observation was correct (?).

Cool, great dive and find! 🎉 🙂

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),
Copy link
Member

Choose a reason for hiding this comment

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

🤯

Ugh. That's really a brilliant separation!

})
}

Expand Down
Loading