From 750c27d4734d1f96e7f7f2ca9fb108c079588058 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 3 Aug 2022 08:34:47 +0100 Subject: [PATCH] deps: update uint8arraylist dep Updates dep and replaces `.slice` for `.subarray` to avoid copying memory unecessarily. --- package.json | 2 +- src/decode.ts | 2 +- src/encode.ts | 4 ++-- src/mplex.ts | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 57d1f1f..fe0ce26 100644 --- a/package.json +++ b/package.json @@ -156,7 +156,7 @@ "it-pipe": "^2.0.3", "it-pushable": "^3.0.0", "it-stream-types": "^1.0.4", - "uint8arraylist": "^1.4.0", + "uint8arraylist": "^2.1.1", "uint8arrays": "^3.0.0", "varint": "^6.0.0" }, diff --git a/src/decode.ts b/src/decode.ts index 2ff231d..71610db 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -49,7 +49,7 @@ class Decoder { } if (type === MessageTypes.NEW_STREAM || type === MessageTypes.MESSAGE_INITIATOR || type === MessageTypes.MESSAGE_RECEIVER) { - msg.data = this._buffer.slice(offset, offset + length) + msg.data = this._buffer.subarray(offset, offset + length) } msgs.push(msg) diff --git a/src/encode.ts b/src/encode.ts index 353189f..2661846 100644 --- a/src/encode.ts +++ b/src/encode.ts @@ -32,7 +32,7 @@ class Encoder { offset += varint.encode.bytes - const header = pool.slice(this._poolOffset, offset) + const header = pool.subarray(this._poolOffset, offset) if (POOL_SIZE - offset < 100) { this._pool = allocUnsafe(POOL_SIZE) @@ -44,7 +44,7 @@ class Encoder { if ((msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) && msg.data != null) { return [ header, - msg.data instanceof Uint8Array ? msg.data : msg.data.slice() + msg.data instanceof Uint8Array ? msg.data : msg.data.subarray() ] } diff --git a/src/mplex.ts b/src/mplex.ts index c64346f..31fa22c 100644 --- a/src/mplex.ts +++ b/src/mplex.ts @@ -30,11 +30,11 @@ function printMessage (msg: Message) { } if (msg.type === MessageTypes.NEW_STREAM) { - output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice()) + output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.subarray()) } if (msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) { - output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice(), 'base16') + output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.subarray(), 'base16') } return output @@ -165,7 +165,7 @@ export class MplexStreamMuxer implements StreamMuxer { } if (msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) { - msg.data = msg.data instanceof Uint8Array ? msg.data : msg.data.slice() + msg.data = msg.data instanceof Uint8Array ? msg.data : msg.data.subarray() } this._source.push(msg) @@ -263,7 +263,7 @@ export class MplexStreamMuxer implements StreamMuxer { return } - const stream = this._newReceiverStream({ id, name: uint8ArrayToString(message.data instanceof Uint8Array ? message.data : message.data.slice()) }) + const stream = this._newReceiverStream({ id, name: uint8ArrayToString(message.data instanceof Uint8Array ? message.data : message.data.subarray()) }) if (this._init.onIncomingStream != null) { this._init.onIncomingStream(stream) @@ -301,7 +301,7 @@ export class MplexStreamMuxer implements StreamMuxer { } // We got data from the remote, push it into our local stream - stream.source.push(message.data.slice()) + stream.source.push(message.data.subarray()) break case MessageTypes.CLOSE_INITIATOR: case MessageTypes.CLOSE_RECEIVER: