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

fix: replace node buffers with uint8arrays #114

Merged
merged 2 commits into from
Aug 11, 2020

Conversation

achingbrain
Copy link
Member

Buffer lists are still used internally but any Buffers created by this module are now Uint8Arrays.

BREAKING CHANGE:

  • All use of node Buffers has been replaced with Uint8Arrays

BREAKING CHANGE:

- All use of node Buffers has been replaced with Uint8Arrays
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)
Copy link
Contributor

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

@@ -29,7 +28,7 @@ class Encoder {
const header = pool.slice(this._poolOffset, offset)
Copy link
Contributor

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.

Copy link
Contributor

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.

@jacobheun
Copy link
Contributor

I created a browser specific file for encode to use Uint8Array, which will let us keep allocUnsafe for Node.js streams.

@jacobheun jacobheun merged commit d005338 into master Aug 11, 2020
@jacobheun jacobheun deleted the fix/replace-node-buffers-with-uint8arrays branch August 11, 2020 16:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants