Skip to content

Commit

Permalink
optimize cache size according to the benchmark results
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Aug 3, 2019
1 parent 760ed90 commit bc5e681
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 13 additions & 2 deletions benchmark/key-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ type InputType = {
const keys: Array<InputType> = Object.keys(require("./benchmark-from-msgpack-lite-data.json")).map((str) => {
const byteLength = utf8Count(str);
const bytes = new Uint8Array(new ArrayBuffer(byteLength));

utf8EncodeJs(str, bytes, 0);

return { bytes, byteLength, str };
});

for (const dataSet of [keys]) {
const keyDecoder = new CachedKeyDecoder();
// make cache storage full
for (let i = 0; i < keyDecoder.maxKeyLength; i++) {
for (let j = 0; j < keyDecoder.maxLengthPerKey; j++) {
const str = `${j.toString().padStart(i + 1, "0")}`;
const byteLength = utf8Count(str);
const bytes = new Uint8Array(new ArrayBuffer(byteLength));
utf8EncodeJs(str, bytes, 0);
keyDecoder.decode(bytes, 0, byteLength); // fill
}
}

// console.dir(keyDecoder, { depth: 100 });
console.log("## When the cache storage is full.");

const suite = new Benchmark.Suite();

Expand Down
10 changes: 4 additions & 6 deletions src/CachedKeyDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ interface KeyCacheRecord {
}

const DEFAULT_MAX_KEY_LENGTH = 16;
const DEFAULT_MAX_LENGTH_PER_KEY = 32;
const DEFAULT_MAX_LENGTH_PER_KEY = 16;

export class CachedKeyDecoder {
private readonly caches: Array<Array<KeyCacheRecord>>;

constructor(
private readonly maxKeyLength = DEFAULT_MAX_KEY_LENGTH,
private readonly maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY,
) {
constructor(readonly maxKeyLength = DEFAULT_MAX_KEY_LENGTH, readonly maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY) {
// avoid `new Array(N)` to create a non-sparse array for performance.
this.caches = [];
for (let i = 0; i < this.maxKeyLength; i++) {
Expand All @@ -32,9 +29,10 @@ export class CachedKeyDecoder {

FIND_CHUNK: for (let i = 0; i < recordsLength; i++) {
const record = records[i];
const recordBytes = record.bytes;

for (let j = 0; j < byteLength; j++) {
if (record.bytes[j] !== bytes[inputOffset + j]) {
if (recordBytes[j] !== bytes[inputOffset + j]) {
continue FIND_CHUNK;
}
}
Expand Down

0 comments on commit bc5e681

Please sign in to comment.