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

max cache for decoder #530

Merged
merged 3 commits into from
Nov 12, 2023
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"comlink": "^4.4.1",
"eventemitter3": "^5.0.1",
"lodash": "^4.17.21",
"lru-cache": "^10.0.1",
"pino": "^8.16.1",
"pino-pretty": "^10.2.3",
"zod": "^3.22.4"
Expand Down
19 changes: 14 additions & 5 deletions packages/core/src/utils/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ import '@polkadot/types-codec'
import { Block } from '../blockchain/block.js'
import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types'
import { HexString } from '@polkadot/util/types'
import { LRUCache } from 'lru-cache'
import { StorageEntry } from '@polkadot/types/primitive/types'
import { StorageKey } from '@polkadot/types'
import { hexToU8a, u8aToHex } from '@polkadot/util'
import _ from 'lodash'

import { decodeWellKnownKey } from './well-known-keys.js'

const _CACHE: Record<string, Map<HexString, StorageEntry>> = {}
const _CACHE: Record<string, LRUCache<HexString, StorageEntry>> = {}

const getCache = (uid: string): Map<HexString, StorageEntry> => {
function createCache() {
return new LRUCache<HexString, StorageEntry>({
max: 50, // The maximum number of items in the cache
})
}

const getCache = (uid: string): LRUCache<HexString, StorageEntry> => {
if (!_CACHE[uid]) {
_CACHE[uid] = new Map()
_CACHE[uid] = createCache()
}
return _CACHE[uid]
}

const getStorageEntry = (meta: DecoratedMeta, block: Block, key: HexString) => {
const cache = getCache(block.chain.uid)
for (const [prefix, storageEntry] of cache.entries()) {
if (key.startsWith(prefix)) return storageEntry
for (const prefix of cache.keys()) {
if (key.startsWith(prefix))
// update the recency of the cache entry
return cache.get(prefix)
}
for (const module of Object.values(meta.query)) {
for (const storage of Object.values(module)) {
Expand Down
15 changes: 8 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ __metadata:
comlink: ^4.4.1
eventemitter3: ^5.0.1
lodash: ^4.17.21
lru-cache: ^10.0.1
pino: ^8.16.1
pino-pretty: ^10.2.3
typescript: ^5.2.2
Expand Down Expand Up @@ -6283,6 +6284,13 @@ __metadata:
languageName: node
linkType: hard

"lru-cache@npm:^10.0.1, lru-cache@npm:^9.1.1 || ^10.0.0":
version: 10.0.1
resolution: "lru-cache@npm:10.0.1"
checksum: 06f8d0e1ceabd76bb6f644a26dbb0b4c471b79c7b514c13c6856113879b3bf369eb7b497dad4ff2b7e2636db202412394865b33c332100876d838ad1372f0181
languageName: node
linkType: hard

"lru-cache@npm:^4.0.1":
version: 4.1.5
resolution: "lru-cache@npm:4.1.5"
Expand Down Expand Up @@ -6318,13 +6326,6 @@ __metadata:
languageName: node
linkType: hard

"lru-cache@npm:^9.1.1 || ^10.0.0":
version: 10.0.1
resolution: "lru-cache@npm:10.0.1"
checksum: 06f8d0e1ceabd76bb6f644a26dbb0b4c471b79c7b514c13c6856113879b3bf369eb7b497dad4ff2b7e2636db202412394865b33c332100876d838ad1372f0181
languageName: node
linkType: hard

"lunr@npm:^2.3.9":
version: 2.3.9
resolution: "lunr@npm:2.3.9"
Expand Down