Skip to content

Commit

Permalink
max cache for decoder (#530)
Browse files Browse the repository at this point in the history
* max cache for decoder

* change cache size
  • Loading branch information
qiweiii authored Nov 12, 2023
1 parent 13a4911 commit 3221894
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
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

0 comments on commit 3221894

Please sign in to comment.