Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
perf: improve performance of the persistent cache used in forking
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
jeffsmale90 committed Apr 6, 2022
1 parent 40d7cb4 commit 9d28c3e
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class PersistentCache {
AbstractIterator<Buffer, Buffer>
>;
protected ancestry: Ancestry;
protected hash: Data;
protected hashBuffer: Buffer;
protected request: Request;
constructor() {}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down

0 comments on commit 9d28c3e

Please sign in to comment.