Skip to content

Commit bc71d5c

Browse files
committed
extract KeyDecoder interface
1 parent 9b1aefa commit bc71d5c

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
"@typescript-eslint/no-throw-literal": "warn",
4646
"@typescript-eslint/no-extra-semi": "warn",
4747
"@typescript-eslint/no-extra-non-null-assertion": "warn",
48-
"@typescript-eslint/no-unused-vars": "warn",
48+
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
4949
"@typescript-eslint/no-use-before-define": "warn",
5050
"@typescript-eslint/no-for-in-array": "warn",
5151
"@typescript-eslint/no-unnecessary-condition": ["warn", { "allowConstantLoopConditions": true }],

benchmark/benchmark-from-msgpack-lite.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ if (msgpack_msgpack) {
6969
buf = bench('buf = /* @msgpack/msgpack */ encoder.encode(obj);', (data) => encoder.encode(data), data);
7070
obj = bench('obj = /* @msgpack/msgpack */ decoder.decodeSync(buf);', (buf) => decoder.decodeSync(buf), buf);
7171
runTest(obj);
72+
73+
if (process.env.CACHE_HIT_RATE) {
74+
const {hit, miss} = decoder.keyDecoder;
75+
console.log(`CACHE_HIT_RATE: cache hit rate in CachedKeyDecoder: hit=${hit}, hiss=${miss}, hit rate=${hit / (hit + miss)}`);
76+
}
7277
}
7378

7479
if (msgpack_js_v5) {

src/CachedKeyDecoder.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ interface KeyCacheRecord {
88
const DEFAULT_MAX_KEY_LENGTH = 16;
99
const DEFAULT_MAX_LENGTH_PER_KEY = 16;
1010

11-
export class CachedKeyDecoder {
11+
export interface KeyDecoder {
12+
canBeCached(byteLength: number): boolean;
13+
decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string;
14+
}
15+
16+
export class CachedKeyDecoder implements KeyDecoder {
17+
hit = 0;
18+
miss = 0;
1219
private readonly caches: Array<Array<KeyCacheRecord>>;
1320

1421
constructor(readonly maxKeyLength = DEFAULT_MAX_KEY_LENGTH, readonly maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY) {
@@ -57,8 +64,10 @@ export class CachedKeyDecoder {
5764
public decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
5865
const cachedValue = this.get(bytes, inputOffset, byteLength);
5966
if (cachedValue != null) {
67+
this.hit++;
6068
return cachedValue;
6169
}
70+
this.miss++;
6271

6372
const value = utf8DecodeJs(bytes, inputOffset, byteLength);
6473
// Ensure to copy a slice of bytes because the byte may be NodeJS Buffer and Buffer#slice() returns a reference to its internal ArrayBuffer.

src/Decoder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
33
import { getInt64, getUint64 } from "./utils/int";
44
import { utf8DecodeJs, TEXT_ENCODING_AVAILABLE, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
55
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
6-
import { CachedKeyDecoder } from "./CachedKeyDecoder";
6+
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
77

88
const enum State {
99
ARRAY,
@@ -77,7 +77,7 @@ export class Decoder<ContextType> {
7777
readonly maxArrayLength = DEFAULT_MAX_LENGTH,
7878
readonly maxMapLength = DEFAULT_MAX_LENGTH,
7979
readonly maxExtLength = DEFAULT_MAX_LENGTH,
80-
readonly cachedKeyDecoder: CachedKeyDecoder | null = sharedCachedKeyDecoder,
80+
readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder,
8181
) {}
8282

8383
private reinitializeState() {
@@ -494,8 +494,8 @@ export class Decoder<ContextType> {
494494

495495
const offset = this.pos + headerOffset;
496496
let object: string;
497-
if (this.stateIsMapKey() && this.cachedKeyDecoder?.canBeCached(byteLength)) {
498-
object = this.cachedKeyDecoder.decode(this.bytes, offset, byteLength);
497+
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
498+
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
499499
} else if (TEXT_ENCODING_AVAILABLE && byteLength > TEXT_DECODER_THRESHOLD) {
500500
object = utf8DecodeTD(this.bytes, offset, byteLength);
501501
} else {

0 commit comments

Comments
 (0)