From 9d28c3ec90b257a93d184a1aef3cbbde9cbaa19c Mon Sep 17 00:00:00 2001 From: Jeff Smale <6363749+jeffsmale90@users.noreply.github.com> Date: Mon, 4 Apr 2022 20:25:48 +1200 Subject: [PATCH] perf: improve performance of the persistent cache used in forking - Make 'lt' argument more specific in order to retrieve less entries - Convert block hash to Buffer type immediately on initiatisation (rather than every time we get / put an entry) --- .../forking/persistent-cache/persistent-cache.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/chains/ethereum/ethereum/src/forking/persistent-cache/persistent-cache.ts b/src/chains/ethereum/ethereum/src/forking/persistent-cache/persistent-cache.ts index 654d0b6440..7ec06cb6be 100644 --- a/src/chains/ethereum/ethereum/src/forking/persistent-cache/persistent-cache.ts +++ b/src/chains/ethereum/ethereum/src/forking/persistent-cache/persistent-cache.ts @@ -43,7 +43,7 @@ export class PersistentCache { AbstractIterator >; protected ancestry: Ancestry; - protected hash: Data; + protected hashBuffer: Buffer; protected request: Request; constructor() {} @@ -131,7 +131,7 @@ export class PersistentCache { } async initialize(height: Quantity, hash: Data, request: Request) { - this.hash = hash; + this.hashBuffer = hash.toBuffer(); this.request = request; const { @@ -325,22 +325,21 @@ export class PersistentCache { const height = Quantity.from(blockNumber); const bufKey = Buffer.from(key); const start = lexico.encode([height.toBuffer(), bufKey]); - const end = lexico.encode([ - Quantity.from(height.toBigInt() + 1n).toBuffer() - ]); + const end = Buffer.concat([start, Buffer.from([0xff])]); + const readStream = this.cacheDb.createReadStream({ gt: start, lt: end, keys: true, values: true }); - const hashBuf = this.hash.toBuffer(); + for await (const data of readStream) { const { key: k, value } = (data as any) as { key: Buffer; value: Buffer }; const [_height, _key, blockHash] = lexico.decode(k); // if our key no longer matches make sure we don't keep searching if (!_key.equals(bufKey)) return; - if (hashBuf.equals(blockHash) || (await this.ancestry.has(blockHash))) { + if (this.hashBuffer.equals(blockHash) || (await this.ancestry.has(blockHash))) { return value; } } @@ -354,7 +353,7 @@ export class PersistentCache { const dbKey = lexico.encode([ height.toBuffer(), Buffer.from(key), - this.hash.toBuffer() + this.hashBuffer ]); await this.cacheDb.put(dbKey, value); return true;