Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: do not emit deprecation notice on Buffer.of #19682

Closed
wants to merge 3 commits into from

Conversation

TimothyGu
Copy link
Member

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the buffer Issues and PRs related to the buffer subsystem. label Mar 29, 2018
lib/buffer.js Outdated
//
// Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of
Buffer.of = (...items) => {
const newObj = allocate(items.length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm … we may want new FastBuffer(items.length) here, so that the returned buffer is not pooled, like you’d expect from the standard TypedArray methods.

Also, I’m curious, do you know why TypedArray.of does not use this[Symbol.species]? That seems like a good fix for this problem in the long run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. I'll opt for createUnsafeBuffer() then, as we don't need the buffer to be zero-filled.

Good question. Let me ask: tc39/ecma262#1157

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it different from Buffer.from(items)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; Buffer.of is (...items).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already in the header, but is the implementation here different in behaviour from just returning Buffer.from(items)? Do we need to reimplement it?

@ChALkeR
Copy link
Member

ChALkeR commented Mar 29, 2018

@TimothyGu, no, my question above was — is there any reason not to define this just as return Buffer.from(items)?

@ChALkeR ChALkeR added this to the 10.0.0 milestone Mar 29, 2018
lib/buffer.js Outdated
const newObj = createUnsafeBuffer(items.length);
for (var k = 0; k < items.length; k++)
newObj[k] = items[k];
return newObj;
Copy link
Member

@ChALkeR ChALkeR Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line-to-line code duplication of fromArrayLike.
These four lines could be replaced with return fromArrayLike(items).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. fromArrayLike uses allocate() while this function uses createUnsafeBuffer(). The former may be pooled, which is not desirable.

lib/buffer.js Outdated
// Buffer() constructor.
//
// Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of
Buffer.of = (...items) => {
Copy link
Member

@ChALkeR ChALkeR Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the name of the function. It should probably be named 'of'.

Copy link
Member Author

@TimothyGu TimothyGu Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grrr, I always thought this kind of assignment names the function automatically. Fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are named in cases like { of: () => {} } and const of = () => {}, but not in x.of = () => {}.

@ChALkeR ChALkeR dismissed their stale review March 30, 2018 07:15

Updated/answered.

@ChALkeR
Copy link
Member

ChALkeR commented Mar 30, 2018

@TimothyGu Ok, thanks for the updated answer re: difference from Buffer.from, I dismissed my review.

> Buffer.from([10]).buffer
ArrayBuffer { byteLength: 8192 }
> Buffer.of(10).buffer
ArrayBuffer { byteLength: 1 }

Could you explain why is pooling the desired behaviour for Buffer.from but not for Buffer.of?


Upd: ah, ok, nevermind — I suppose that the reason is that it speeds up things, but one could not expect pooling from Buffer.of.

newObj[k] = items[k];
return newObj;
};
Buffer.of = of;
Copy link
Member

@ChALkeR ChALkeR Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other functions in this file are defined as regular functions, not as arrow functions.
Is there a reason for specifically using an arrow function here? The non-arrow variant would have been one line less code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is documented in the comment above :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed that :-/.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use a normal function and throw an explicit error if new.target is truthy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We would want the function to not be a constructor.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there's not really a difference to users between whether it's got [[Construct]] or whether it throws on new.


const common = require('../common');

process.on('warning', common.mustNotCall());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the --no-warnings flag be dropped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the --no-warnings just stops the console output. The process.on('warning') event will still emit.

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 9, 2018
@BridgeAR
Copy link
Member

BridgeAR commented Apr 9, 2018

@TimothyGu
Copy link
Member Author

Landed in 42d8976.

@TimothyGu TimothyGu closed this Apr 11, 2018
@TimothyGu TimothyGu deleted the buffer-undeprecate branch April 11, 2018 05:21
TimothyGu added a commit that referenced this pull request Apr 11, 2018
PR-URL: #19682
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
targos pushed a commit that referenced this pull request Apr 12, 2018
PR-URL: #19682
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
PR-URL: #19682
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants