Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmark/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { WASM_AVAILABLE } from "../src/wasmFunctions";

console.log(`WASM_AVAILABLE=${WASM_AVAILABLE}`);

const ascii = "A".repeat(40000);
const emoji = "🌏".repeat(20000);
const ascii = "A".repeat(30);
const emoji = "🌏".repeat(8);

{
// warm up ascii
Expand All @@ -17,13 +17,13 @@ const emoji = "🌏".repeat(20000);
// run

console.time("encode ascii");
for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 1000000; i++) {
encode(data);
}
console.timeEnd("encode ascii");

console.time("decode ascii");
for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 1000000; i++) {
decode(encoded);
}
console.timeEnd("decode ascii");
Expand All @@ -40,13 +40,13 @@ const emoji = "🌏".repeat(20000);
// run

console.time("encode emoji");
for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 1000000; i++) {
encode(data);
}
console.timeEnd("encode emoji");

console.time("decode emoji");
for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 1000000; i++) {
decode(encoded);
}
console.timeEnd("decode emoji");
Expand Down
18 changes: 17 additions & 1 deletion src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import { WASM_AVAILABLE, utf8EncodeWasm, WASM_STR_THRESHOLD } from "./wasmFuncti
export const DEFAULT_MAX_DEPTH = 100;
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048;

const STR_CACHE_THRESHOLD = 32;

export class Encoder {
private pos = 0;
private view = new DataView(new ArrayBuffer(this.initialBufferSize));
private bytes = new Uint8Array(this.view.buffer);

private readonly strCache: Record<string, Uint8Array> = Object.create(null);

constructor(
readonly extensionCodec = ExtensionCodec.defaultCodec,
readonly maxDepth = DEFAULT_MAX_DEPTH,
Expand Down Expand Up @@ -146,6 +150,16 @@ export class Encoder {
const maxHeaderSize = 1 + 4;
const strLength = object.length;

if (strLength < STR_CACHE_THRESHOLD) {
const buffer = this.strCache[object];
if (buffer != null) {
this.writeStringHeader(buffer.byteLength);
this.bytes.set(buffer, this.pos);
this.pos += buffer.byteLength;
return;
}
}

if (WASM_AVAILABLE && strLength > WASM_STR_THRESHOLD) {
// ensure max possible size
const maxSize = maxHeaderSize + strLength * 4;
Expand All @@ -154,12 +168,14 @@ export class Encoder {
// utf8EncodeWasm() handles headByte+size as well as string itself
const ouputLength = utf8EncodeWasm(object, this.bytes, this.pos);
this.pos += ouputLength;
return;
} else {
const byteLength = utf8Count(object);
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
this.writeStringHeader(byteLength);
utf8Encode(object, this.bytes, this.pos);
if (byteLength < STR_CACHE_THRESHOLD) {
this.strCache[object] = this.bytes.subarray(this.pos, this.pos + byteLength);
}
this.pos += byteLength;
}
}
Expand Down