Skip to content

Commit bc5e681

Browse files
committed
optimize cache size according to the benchmark results
1 parent 760ed90 commit bc5e681

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

benchmark/key-decoder.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@ type InputType = {
1414
const keys: Array<InputType> = Object.keys(require("./benchmark-from-msgpack-lite-data.json")).map((str) => {
1515
const byteLength = utf8Count(str);
1616
const bytes = new Uint8Array(new ArrayBuffer(byteLength));
17-
1817
utf8EncodeJs(str, bytes, 0);
19-
2018
return { bytes, byteLength, str };
2119
});
2220

2321
for (const dataSet of [keys]) {
2422
const keyDecoder = new CachedKeyDecoder();
23+
// make cache storage full
24+
for (let i = 0; i < keyDecoder.maxKeyLength; i++) {
25+
for (let j = 0; j < keyDecoder.maxLengthPerKey; j++) {
26+
const str = `${j.toString().padStart(i + 1, "0")}`;
27+
const byteLength = utf8Count(str);
28+
const bytes = new Uint8Array(new ArrayBuffer(byteLength));
29+
utf8EncodeJs(str, bytes, 0);
30+
keyDecoder.decode(bytes, 0, byteLength); // fill
31+
}
32+
}
33+
34+
// console.dir(keyDecoder, { depth: 100 });
35+
console.log("## When the cache storage is full.");
2536

2637
const suite = new Benchmark.Suite();
2738

src/CachedKeyDecoder.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ interface KeyCacheRecord {
66
}
77

88
const DEFAULT_MAX_KEY_LENGTH = 16;
9-
const DEFAULT_MAX_LENGTH_PER_KEY = 32;
9+
const DEFAULT_MAX_LENGTH_PER_KEY = 16;
1010

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

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

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

3634
for (let j = 0; j < byteLength; j++) {
37-
if (record.bytes[j] !== bytes[inputOffset + j]) {
35+
if (recordBytes[j] !== bytes[inputOffset + j]) {
3836
continue FIND_CHUNK;
3937
}
4038
}

0 commit comments

Comments
 (0)