Optimizations for reuse of a Decoder instance
This commit is contained in:
parent
550224e0b1
commit
5412f8b9d0
|
@ -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",
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue