Optimizations for reuse of a Decoder instance

This commit is contained in:
Tony Garnock-Jones 2021-03-04 11:26:11 +01:00
parent 550224e0b1
commit 5412f8b9d0
2 changed files with 10 additions and 2 deletions

View File

@ -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",

View File

@ -28,7 +28,11 @@ export class Decoder<T extends object> {
}
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<T extends object> {
try_next(): Value<T> | 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) {