-
Notifications
You must be signed in to change notification settings - Fork 759
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: cache and other refactors #3569
Changes from 17 commits
fa3c2ec
bd1f237
d5aaf30
533fa8c
31c94af
bba928b
a04158d
2431594
814b9f7
2439219
4c6240f
5e1666d
c754eac
5d4895c
888b20e
905a52e
f30288a
ba00837
c037ae2
fdcdb36
3d93587
48baee3
f0b66bf
5f4c253
7fe21fd
4864383
b6ee97b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { AccountCache } from './account.js' | ||
import { CodeCache } from './code.js' | ||
import { StorageCache } from './storage.js' | ||
import { type CacheSettings, CacheType, type CachesStateManagerOpts } from './types.js' | ||
|
||
import type { Address } from '@ethereumjs/util' | ||
|
||
export class Caches { | ||
account?: AccountCache | ||
code?: CodeCache | ||
storage?: StorageCache | ||
|
||
settings: Record<'account' | 'code' | 'storage', CacheSettings> | ||
|
||
constructor(opts: CachesStateManagerOpts = {}) { | ||
const accountSettings = { | ||
deactivate: | ||
(opts.accountCacheOpts?.deactivate === true || opts.accountCacheOpts?.size === 0) ?? false, | ||
type: opts.accountCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: opts.accountCacheOpts?.size ?? 100000, | ||
} | ||
const storageSettings = { | ||
deactivate: | ||
(opts.storageCacheOpts?.deactivate === true || opts.storageCacheOpts?.size === 0) ?? false, | ||
type: opts.storageCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: opts.storageCacheOpts?.size ?? 20000, | ||
} | ||
|
||
const codeSettings = { | ||
deactivate: | ||
(opts.codeCacheOpts?.deactivate === true || opts.codeCacheOpts?.size === 0) ?? false, | ||
type: opts.codeCacheOpts?.type ?? CacheType.ORDERED_MAP, | ||
size: opts.codeCacheOpts?.size ?? 20000, | ||
} | ||
|
||
this.settings = { | ||
account: accountSettings, | ||
code: codeSettings, | ||
storage: storageSettings, | ||
} | ||
|
||
if (this.settings.account.deactivate === false) { | ||
this.account = new AccountCache({ | ||
size: this.settings.account.size, | ||
type: this.settings.account.type, | ||
}) | ||
} | ||
|
||
if (this.settings.storage.deactivate === false) { | ||
this.storage = new StorageCache({ | ||
size: this.settings.storage.size, | ||
type: this.settings.storage.type, | ||
}) | ||
} | ||
|
||
if (this.settings.code.deactivate === false) { | ||
this.code = new CodeCache({ | ||
size: this.settings.code.size, | ||
type: this.settings.code.type, | ||
}) | ||
} | ||
} | ||
|
||
checkpoint() { | ||
this.account?.checkpoint() | ||
this.storage?.checkpoint() | ||
this.code?.checkpoint() | ||
} | ||
|
||
clear() { | ||
this.account?.clear() | ||
this.storage?.clear() | ||
this.code?.clear() | ||
} | ||
|
||
commit() { | ||
this.account?.commit() | ||
this.storage?.commit() | ||
this.code?.commit() | ||
} | ||
|
||
deleteAccount(address: Address) { | ||
this.code?.del(address) | ||
this.account?.del(address) | ||
this.storage?.clearStorage(address) | ||
} | ||
|
||
revert() { | ||
this.account?.revert() | ||
this.storage?.revert() | ||
this.code?.revert() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,58 @@ | ||
import type { CacheType } from '@ethereumjs/common' | ||
export enum CacheType { | ||
LRU = 'lru', | ||
ORDERED_MAP = 'ordered_map', | ||
} | ||
|
||
export interface CacheOpts { | ||
size: number | ||
type: CacheType | ||
} | ||
|
||
export type CacheSettings = { | ||
deactivate: boolean | ||
type: CacheType | ||
size: number | ||
} | ||
|
||
export interface CacheStateManagerOpts { | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe drop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we remove StateManager, we'll have a naming conflict with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe give this a second thought, but I would kill off the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, just seeing this comment I'll give that a try, would really like to simplify further |
||
* Allows for cache deactivation | ||
* | ||
* Depending on the use case and underlying datastore (and eventual concurrent cache | ||
* mechanisms there), usage with or without cache can be faster | ||
* | ||
* Default: false | ||
*/ | ||
deactivate?: boolean | ||
|
||
/** | ||
* Cache type to use. | ||
* | ||
* Available options: | ||
* | ||
* ORDERED_MAP: Cache with no fixed upper bound and dynamic allocation, | ||
* use for dynamic setups like testing or similar. | ||
* | ||
* LRU: LRU cache with pre-allocation of memory and a fixed size. | ||
* Use for larger and more persistent caches. | ||
*/ | ||
type?: CacheType | ||
|
||
/** | ||
* Size of the cache (only for LRU cache) | ||
* | ||
* Default: 100000 (account cache) / 20000 (storage cache) / 20000 (code cache) | ||
* | ||
* Note: the cache/trie interplay mechanism is designed in a way that | ||
* the theoretical number of max modified accounts between two flush operations | ||
* should be smaller than the cache size, otherwise the cache will "forget" the | ||
* old modifications resulting in an incomplete set of trie-flushed accounts. | ||
*/ | ||
size?: number | ||
} | ||
|
||
export interface CachesStateManagerOpts { | ||
accountCacheOpts?: CacheStateManagerOpts | ||
codeCacheOpts?: CacheStateManagerOpts | ||
storageCacheOpts?: CacheStateManagerOpts | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can fully drop the
CacheOpts
from this key, makes it nicer to digest/read and remains fully expressive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same for others of course)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!