Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
deps: update uint8arraylist dep (#199)
Browse files Browse the repository at this point in the history
Updates dep and replaces `.slice` for `.subarray` to avoid copying
memory unecessarily.
  • Loading branch information
achingbrain authored Aug 3, 2022
1 parent 46334e6 commit 6e3b9d8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
]
}

Expand Down
10 changes: 5 additions & 5 deletions src/mplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6e3b9d8

Please sign in to comment.