GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Buffer Use Uint8Array by trevnorris · Pull Request #1825 · nodejs/node

Conversation

@trevnorris

@trevnorris trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor

This is still partially incomplete. All tests are passing, but some parts still need to be hardened. The native API needs to return MaybeLocal<>, and CreateFromString() needs to check the return value to make sure the string could be written.

All existing tests are passing. A good chunk of the JS changes are whitespace related. For the conditional logic to allow --use-old-buffer flag.

R=@bnoordhuis

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@trevnorris
buffer: allow ARGS_THIS to accept a name
266526c
Allowing the name to be passed to the ARGS_THIS macro will make it
easier to share code with the Uint8Array implementation.
@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label May 28, 2015
trevnorris
trevnorris reviewed May 28, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@trevnorris trevnorris May 28, 2015

Copy link
Copy Markdown
Contributor Author

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.

Because we can't resize these Buffers as we could previously, the complete string write is done on the native side so the memory can be shrunk for base64 encoding before it is passed to the new buffer.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@targos

targos commented May 28, 2015

Copy link
Copy Markdown
Member
Buffers now have a maximum size of Smi::kMaxLength, as defined by V8.
Which is ~2 GB on 64 bit and ~1 GB on 32 bit.

What happened to the arbitrary sized buffers you talked about on Twitter?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor Author

@targos ArrayBuffer can be arbitrary sized, but Typed Arrays have a maximum index value. My initial code was all C++, so I read the data into an ArrayBuffer instance. Then was performing operations on that data using the native Buffer methods. Was later that night when I realized that creating a new Uint8Array from an ArrayBuffer that large was throwing. After some investigation saw that V8 sets a limitation (https://github.com/v8/v8-git-mirror/blob/4.3.66/src/api.cc#L6453-L6456). This is something I hope to get fixed in the future so Typed Arrays can also have arbitrarily long indices.

Though I was also thinking that it may be useful to allow creating new Buffer instances from slices of an ArrayBuffer. Will look into that after this PR is finished.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor Author

Link to V8 issue I created to hopefully one day allow a much higher upper limit on Typed Array indices: https://code.google.com/p/v8/issues/detail?id=4153

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented May 28, 2015

Copy link
Copy Markdown
Member

Thanks for the explanation.
FWIW I starred your issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

domenic
domenic reviewed May 28, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@domenic domenic May 28, 2015

Copy link
Copy Markdown
Contributor

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.

Might not need any of the custom iterator implementation for the new version (i.e. can hide all this behind an if)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris May 28, 2015

Copy link
Copy Markdown
Contributor Author

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.

Already do, and you're right. It doesn't. :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@domenic domenic May 28, 2015

Copy link
Copy Markdown
Contributor

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.

Ah, just scrolled past that part, sorry!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@trevnorris

trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor Author

@targos Thanks :)

For anyone curious about benchmarks, here are a few results I compiled from the benchmarks folder. Left column is old implementation. Right column is with this PR:

buffer-slice.js type=fast n=1024: 6646.1  2570.8
buffer-slice.js type=slow n=1024: 6711.2  2617.4

buffer-creation.js type=fast len=10 n=1024:   5103.1  2498.6
buffer-creation.js type=fast len=1024 n=1024: 2648.3  2054.9
buffer-creation.js type=slow len=10 n=1024:   2190.0  2067.8
buffer-creation.js type=slow len=1024 n=1024: 1294.9  1677.7

http-simple type=bytes length=1024:   21588.6  18053.3
http-simple type=bytes length=10240:  8601.9   8439.7

I'll post more once I've done more precise testing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris
trevnorris force-pushed the buffer-use-ab branch from ad218c8 to bcd8e41 Compare May 28, 2015 19:34
@domenic

domenic commented May 28, 2015

Copy link
Copy Markdown
Contributor

In IRC you mentioned that there is no more .parent, instead you use .buffer. Maybe we should alias it (with a getter if nothing else)?

In general what would the changelog look like here? Especially in terms of back-compat breakers? I imagine most of them are on the C++ side but those are worth putting in the changelog too.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor Author

@domenic Most the breakage is on the C++ side. The only JS changes that break, that I'm aware of, are removal of .parent and .offset (which are now .buffer and .byteOffset).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented May 28, 2015

Copy link
Copy Markdown
Contributor Author

On the side, using .buffer wasn't a personal choice. That's what property the ArrayBuffer is set to for a Typed Array.

Making each one a getter shouldn't be difficult, if we'd like to maintain full backwards compatibility that way.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@domenic

domenic commented May 28, 2015

Copy link
Copy Markdown
Contributor

Yeah, it seems relatively worthwhile to add aliasing-getters for .parent and .offset. A little crufty, but worthwhile IMO.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bnoordhuis
bnoordhuis reviewed May 28, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@bnoordhuis bnoordhuis May 28, 2015

Copy link
Copy Markdown
Member

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.

Suggestion: instead of having if statements everywhere, maybe move the old and new code to two files buffer_new.js and buffer_old.js in lib/internal? Then you just require() the right one here and re-export it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris May 29, 2015

Copy link
Copy Markdown
Contributor Author

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.

Ooh. I like that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@targos

targos commented May 28, 2015

Copy link
Copy Markdown
Member

is new Buffer(size) initially filled with 0 now ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bnoordhuis
bnoordhuis reviewed May 28, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@bnoordhuis bnoordhuis May 28, 2015

Copy link
Copy Markdown
Member

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.

Maybe (Buffer.poolSize >>> 1)? Probably doesn't matter much performance-wise.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris May 29, 2015

Copy link
Copy Markdown
Contributor Author

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.

Did this before I realized that just because ArrayBuffers can be arbitrarily big, typed arrays can't. So it was "technically" possible to set the pool size greater than a bitwise operation could handle. But that's not actually the case. So I'll change that back.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
buffer: implement Uint8Array backed Buffer
1393a25
With V8 4.4 removing the external array data API currently used by
Buffer, the new implementation uses the Uint8Array to back Buffer.

Buffers now have a maximum size of Smi::kMaxLength, as defined by V8.
Which is ~2 GB on 64 bit and ~1 GB on 32 bit.

The flag --use-old-buffer allows using the old Buffer implementation.
This flag will be removed once V8 4.4 has landed.

The two JS Buffer implementations have been split into two files for
simplicity.

Use getter to return expected .parent/.offset values for backwards
compatibility.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
buffer: finish implementing FreeCallback
091c0b0
Passing a FreeCallback to Buffer::New() now uses externalized
ArrayBuffer's.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
buffer: switch to using Maybe<T> API
95c3eca
Use the new Maybe<T> syntax for v8::Object::SetPrototype().

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
buffer: switch API to return MaybeLocal<T>
7cf9e75
Instead of aborting in case of internal failure, return an empty
Local<Object>. Using the MaybeLocal<T> API, users must check their
return values.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
buffer: make additional changes to native API
a7054af
Address comments and deprecations left in source files. These changes
include:

* Remove the deprecated API.
* Change Buffer::New() that did a copy of the data to Buffer::Copy()
* Change Buffer::Use() to Buffer::New()

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Jul 30, 2015
@trevnorris @rvagg
crypto: remove kMaxLength on randomBytes()
18c6137
New Buffer implementation allows greater than kMaxLength to be created.
So instead check if the passed value is a valid Smi.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: allow ARGS_THIS to accept a name
557e2da
Allowing the name to be passed to the ARGS_THIS macro will make it
easier to share code with the Uint8Array implementation.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: implement Uint8Array backed Buffer
ee09d9d
With V8 4.4 removing the external array data API currently used by
Buffer, the new implementation uses the Uint8Array to back Buffer.

Buffers now have a maximum size of Smi::kMaxLength, as defined by V8.
Which is ~2 GB on 64 bit and ~1 GB on 32 bit.

The flag --use-old-buffer allows using the old Buffer implementation.
This flag will be removed once V8 4.4 has landed.

The two JS Buffer implementations have been split into two files for
simplicity.

Use getter to return expected .parent/.offset values for backwards
compatibility.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: finish implementing FreeCallback
0071b19
Passing a FreeCallback to Buffer::New() now uses externalized
ArrayBuffer's.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: switch to using Maybe<T> API
a8c12a1
Use the new Maybe<T> syntax for v8::Object::SetPrototype().

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: switch API to return MaybeLocal<T>
f0c8986
Instead of aborting in case of internal failure, return an empty
Local<Object>. Using the MaybeLocal<T> API, users must check their
return values.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
buffer: make additional changes to native API
73211ea
Address comments and deprecations left in source files. These changes
include:

* Remove the deprecated API.
* Change Buffer::New() that did a copy of the data to Buffer::Copy()
* Change Buffer::Use() to Buffer::New()

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 1, 2015
@trevnorris @rvagg
crypto: remove kMaxLength on randomBytes()
9dbac89
New Buffer implementation allows greater than kMaxLength to be created.
So instead check if the passed value is a valid Smi.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 3, 2015
@trevnorris @rvagg
buffer: allow ARGS_THIS to accept a name
1d9f152
Allowing the name to be passed to the ARGS_THIS macro will make it
easier to share code with the Uint8Array implementation.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
trevnorris added a commit that referenced this pull request Aug 3, 2015
@trevnorris @rvagg
buffer: implement Uint8Array backed Buffer
46f011f
With V8 4.4 removing the external array data API currently used by
Buffer, the new implementation uses the Uint8Array to back Buffer.

Buffers now have a maximum size of Smi::kMaxLength, as defined by V8.
Which is ~2 GB on 64 bit and ~1 GB on 32 bit.

The flag --use-old-buffer allows using the old Buffer implementation.
This flag will be removed once V8 4.4 has landed.

The two JS Buffer implementations have been split into two files for
simplicity.

Use getter to return expected .parent/.offset values for backwards
compatibility.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
@rvagg rvagg mentioned this pull request Aug 4, 2015
Release proposal: v3.0.0 #2299
Merged
@joyeecheung joyeecheung mentioned this pull request Dec 6, 2016
buffer: throw a consistent range error for length > kMaxLength #10152
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

buffer Issues and PRs related to the buffer subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@trevnorris @targos @domenic @kkoopa @bnoordhuis @YurySolovyov @Fishrock123 @ronkorving @mscdex
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.