-
Notifications
You must be signed in to change notification settings - Fork 30
fix: replace node buffers with uint8arrays #114
Conversation
BREAKING CHANGE: - All use of node Buffers has been replaced with Uint8Arrays
src/coder/encode.js
Outdated
const varint = require('varint') | ||
const BufferList = require('bl/BufferList') | ||
|
||
const POOL_SIZE = 10 * 1024 | ||
|
||
class Encoder { | ||
constructor () { | ||
this._pool = Buffer.allocUnsafe(POOL_SIZE) | ||
this._pool = new Uint8Array(POOL_SIZE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should run the mega stress tests to verify the performance here is stable. https://github.com/libp2p/js-libp2p-interfaces/blob/master/src/stream-muxer/tests/mega-stress-test.js#L7
src/coder/encode.js
Outdated
@@ -29,7 +28,7 @@ class Encoder { | |||
const header = pool.slice(this._poolOffset, offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to check where we're doing slices and look at using subarray instead, https://nodejs.org/api/buffer.html#buffer_buf_subarray_start_end. slice will cause a clone on TypedArrays, whereas with Buffer it will reference the same memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now using subarray
for the browser version.
I created a browser specific file for encode to use Uint8Array, which will let us keep allocUnsafe for Node.js streams. |
Buffer lists are still used internally but any Buffers created by this module are now Uint8Arrays.
BREAKING CHANGE: