From 35031b4bf550b5558de40d1b3d558356ba7ba39d Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 9 Nov 2023 16:51:14 +0800 Subject: [PATCH 1/2] max cache for decoder --- packages/core/package.json | 1 + packages/core/src/utils/decoder.ts | 19 ++++++++++++++----- yarn.lock | 15 ++++++++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index de7af641..ce74ba48 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" diff --git a/packages/core/src/utils/decoder.ts b/packages/core/src/utils/decoder.ts index c279f048..f2a6b527 100644 --- a/packages/core/src/utils/decoder.ts +++ b/packages/core/src/utils/decoder.ts @@ -2,6 +2,7 @@ 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' @@ -9,19 +10,27 @@ import _ from 'lodash' import { decodeWellKnownKey } from './well-known-keys.js' -const _CACHE: Record> = {} +const _CACHE: Record> = {} -const getCache = (uid: string): Map => { +function createCache() { + return new LRUCache({ + max: 500, // The maximum number of items in the cache + }) +} + +const getCache = (uid: string): LRUCache => { 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)) { diff --git a/yarn.lock b/yarn.lock index ea0ac7c3..7f9c3792 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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" @@ -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" From 8985f6bd8f92d223ee5d2a86c40efb9b0ead52bd Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Fri, 10 Nov 2023 21:26:05 +0800 Subject: [PATCH 2/2] change cache size --- packages/core/src/utils/decoder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/utils/decoder.ts b/packages/core/src/utils/decoder.ts index f2a6b527..687d53b5 100644 --- a/packages/core/src/utils/decoder.ts +++ b/packages/core/src/utils/decoder.ts @@ -14,7 +14,7 @@ const _CACHE: Record> = {} function createCache() { return new LRUCache({ - max: 500, // The maximum number of items in the cache + max: 50, // The maximum number of items in the cache }) }