buffer: stricter Buffer.isBuffer#11961
Conversation
There was a problem hiding this comment.
No need to require this, right?
There was a problem hiding this comment.
@cjihrig Our linter forbids using Buffer via the global
There was a problem hiding this comment.
Our linter forbids using Buffer via the global
That's only in lib. Tests like this (as well as benchmarks and tools) can use the Buffer global and the linter won't complain.
|
A couple of things:
¹ edit: That’s orthogonal to this PR, I’m not fundamentally opposed to a change like this or anything |
It can be taken both ways, but I think it is a semver-patch since an object with merely
😕 That does mean that we have to walk the prototype chain instead of simply relying on the prototype being the same as
As you have remarked it's orthogonal to this change, but independent with it. |
|
|
Agreed! But still, sometimes bugfixes are semver-major and we have to live with that… the example from the issue is constructed enough to not worry about that, I’d say. |
|
We likely need to hold off on this until we decide what we're doing with Buffer in general. |
|
I still would like to advance this – if only the |
|
Ping @nodejs/buffer |
|
I think that the JS Buffer check is used enough that performance is a concern, and simply checking whether the argument is obviously not a Buffer is acceptable. Buffers always end up being passed to C++ anyway where they need to be checked again. Or we could go the cheap route and set something like: const is_buffer_symbol = Symbol('this is a Buffer fool!');
function Buffer() {
this[is_buffer_symbol] = true;
}Seems ridiculous, and of course the user could use While checking the performance of the call, I'd prioritize for when |
|
(removed my previous comment about |
|
@trevnorris The |
|
I think the idea from @trevnorris to use a Symbol is good. It should probably yield the best performance result and it is safe for inheritance. @TimothyGu would you mind rebasing and giving that a try? |
|
Ping @TimothyGu once more. |
Last time I checked, this PR actually increases the performance of I'll try to address the rest of the questions and get this up to date tonight. |
Make "fake" Buffer subclasses whose instances are not valid Uint8Arrays fail the test. Fixes: nodejs#11954
|
Unfortunately the performance of isUint8Array (or any C++ methods) on TurboFan seems to be very much subpar... I have rebased this but will temporarily put this on hold. |
88cf96b to
ee2d44a
Compare

Make "fake" Buffer subclasses whose instances are not valid Uint8Arrays fail the test.
Performance-wise, there are two options for this PR: one is to simply add an
isUint8Arraycheck and be done with it (w/o 88cf96b). It makes non-Buffers a lot faster, but Buffers slower:Because of the decrease in performance of actual buffers, I exploited the fact that currently subclassing Buffer is not possible due to #4701, and only checked
[[Prototype]]of the value itself (rather than going up the prototype chain asinstanceofdoes). I can remove 88cf96b if it is deemed to be less future-proof but it does help out the performance a lot.Fixes: #11954
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
buffer