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 (#2811
Browse files Browse the repository at this point in the history
)

No change in behaviour, but makes working with local forks ever so slightly faster.
  • Loading branch information
jeffsmale90 authored and davidmurdoch committed Apr 7, 2022
1 parent 8f8feb4 commit 01aeb2e
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const levelupOptions = {
valueEncoding: "binary"
};
const leveldownOpts = { prefix: "" };
const maxValueByteBuffer = Buffer.from([0xff]);

/**
* A leveldb-backed cache that enables associating immutable data as it existed
Expand All @@ -43,7 +44,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 +132,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 +326,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, maxValueByteBuffer]);

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 +354,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 01aeb2e

Please sign in to comment.