[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Srabutdotcom/msgpack-javascript/main/src/CachedKeyDecoder.ts [Back]  [Original]

import { utf8DecodeJs } from "./utils/utf8";

const DEFAULT_MAX_KEY_LENGTH = 16;
const DEFAULT_MAX_LENGTH_PER_KEY = 16;

export interface KeyDecoder {
  canBeCached(byteLength: number): boolean;
  decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string;
}
interface KeyCacheRecord {
  readonly bytes: Uint8Array;
  readonly str: string;
}

export class CachedKeyDecoder implements KeyDecoder {
  hit = 0;
  miss = 0;
  private readonly caches: Array;

  constructor(
    readonly maxKeyLength = DEFAULT_MAX_KEY_LENGTH,
    readonly maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY,
  ) {
    // avoid `new Array(N)`, which makes a sparse array,
    // because a sparse array is typically slower than a non-sparse array.
    this.caches = [];
    for (let i = 0; i < this.maxKeyLength; i++) {
      this.caches.push([]);
    }
  }

  public canBeCached(byteLength: number): boolean {
    return byteLength > 0 && byteLength = this.maxLengthPerKey) {
      // `records` are full!
      // Set `record` to an arbitrary position.
      records[(Math.random() * records.length) | 0] = record;
    } else {
      records.push(record);
    }
  }

  public decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
    const cachedValue = this.find(bytes, inputOffset, byteLength);
    if (cachedValue != null) {
      this.hit++;
      return cachedValue;
    }
    this.miss++;

    const str = utf8DecodeJs(bytes, inputOffset, byteLength);
    // Ensure to copy a slice of bytes because the byte may be NodeJS Buffer and Buffer#slice() returns a reference to its internal ArrayBuffer.
    const slicedCopyOfBytes = Uint8Array.prototype.slice.call(bytes, inputOffset, inputOffset + byteLength);
    this.store(slicedCopyOfBytes, str);
    return str;
  }
}

Web Proxy Viewer  |  New URL  |  Original Page