Optimizations for reuse of a Decoder instance
This commit is contained in:
parent
550224e0b1
commit
5412f8b9d0
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "preserves",
|
"name": "preserves",
|
||||||
"version": "0.6.3",
|
"version": "0.6.4",
|
||||||
"description": "Experimental data serialization format",
|
"description": "Experimental data serialization format",
|
||||||
"homepage": "https://gitlab.com/preserves/preserves",
|
"homepage": "https://gitlab.com/preserves/preserves",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
|
|
|
@ -28,7 +28,11 @@ export class Decoder<T extends object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
write(data: BytesLike) {
|
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;
|
this.index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,6 +146,10 @@ export class Decoder<T extends object> {
|
||||||
|
|
||||||
try_next(): Value<T> | undefined {
|
try_next(): Value<T> | undefined {
|
||||||
const start = this.index;
|
const start = this.index;
|
||||||
|
|
||||||
|
if (start >= this.packet.length) return void 0;
|
||||||
|
// ^ important somewhat-common case optimization - avoid the exception
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return this.next();
|
return this.next();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue