From 5412f8b9d04c56f31adab2c6c6c18b7f143154d9 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 4 Mar 2021 11:26:11 +0100 Subject: [PATCH] Optimizations for reuse of a Decoder instance --- implementations/javascript/package.json | 2 +- implementations/javascript/src/decoder.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/implementations/javascript/package.json b/implementations/javascript/package.json index 5e4b6c8..38a0fe2 100644 --- a/implementations/javascript/package.json +++ b/implementations/javascript/package.json @@ -1,6 +1,6 @@ { "name": "preserves", - "version": "0.6.3", + "version": "0.6.4", "description": "Experimental data serialization format", "homepage": "https://gitlab.com/preserves/preserves", "license": "Apache-2.0", diff --git a/implementations/javascript/src/decoder.ts b/implementations/javascript/src/decoder.ts index a730c50..59f5303 100644 --- a/implementations/javascript/src/decoder.ts +++ b/implementations/javascript/src/decoder.ts @@ -28,7 +28,11 @@ export class Decoder { } write(data: BytesLike) { - this.packet = Bytes.concat([this.packet.slice(this.index), data])._view; + if (this.index === this.packet.length) { + this.packet = underlying(data); + } else { + this.packet = Bytes.concat([this.packet.slice(this.index), data])._view; + } this.index = 0; } @@ -142,6 +146,10 @@ export class Decoder { try_next(): Value | undefined { const start = this.index; + + if (start >= this.packet.length) return void 0; + // ^ important somewhat-common case optimization - avoid the exception + try { return this.next(); } catch (e) {