Skip to content

lib: make new SafeSet(arr) safe #57895

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

lib: make new SafeSet(arr) safe #57895

wants to merge 1 commit into from

Conversation

ljharb
Copy link
Member

@ljharb ljharb commented Apr 16, 2025

This has the direct ergonomic benefit of being able to safely pass an array to new SafeSet().

It has the unintended but desirable benefit of making a lot of existing post-startup calls to new SafeSet with an array more robust.

This helps with #57876, and whichever lands first, the other will want to rebase on top of it.

@ljharb ljharb added the lib / src Issues and PRs related to general changes in the lib or src directory. label Apr 16, 2025
@ljharb ljharb requested review from BridgeAR and aduh95 April 16, 2025 05:58
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/test_runner
  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Apr 16, 2025
@ljharb ljharb force-pushed the safeset branch 2 times, most recently from d57bd99 to fa04710 Compare April 16, 2025 06:06
Copy link
Contributor

@ShogunPanda ShogunPanda left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@ljharb
Copy link
Member Author

ljharb commented Apr 16, 2025

(i assume the coverage failures mean i need to add some tests; if nobody has any objections to the PR then i'll ofc add those before attempting to land)

@aduh95 aduh95 added the needs-benchmark-ci PR that need a benchmark CI run. label Apr 16, 2025
@aduh95
Copy link
Contributor

aduh95 commented Apr 16, 2025

It would be worth specifying in primordials.md that SafeSet constructor is an odd one out

<summary><code>%Map%</code>, <code>%Set%</code>, <code>%WeakMap%</code>, and
<code>%WeakSet%</code> constructors iterate over an array</summary>

and probably add it to the list of culprit for performance issues (I guess we should first established whether it's indeed the case)

## Primordials with known performance issues
One of the reasons why the current Node.js API is not completely tamper-proof is
performance: sometimes the use of primordials can cause performance regressions
with V8, which when in a hot code path, could significantly decrease the
performance of code in Node.js.

Also, build is failing.

@ljharb
Copy link
Member Author

ljharb commented Apr 16, 2025

Regarding the primordials.md comment, those are about the builtins, not the SafeX constructors, so I don't think it makes sense there - and I agree that we should wait until performance issues are identified before noting a performance culprit.

I believe I've addressed the rest of the concerns (except probably the failing build, which I'll iterate on).

I haven't applied this to SafeWeakSet, because there's only 2 instances of it in the codebase and neither are adding at construction time - I'm happy to add it in the future if it proves useful.

@aduh95
Copy link
Contributor

aduh95 commented Apr 16, 2025

Regarding the primordials.md comment, those are about the builtins, not the SafeX constructors, so I don't think it makes sense there

This section is using the SafeSet constructor as an example though

@ljharb
Copy link
Member Author

ljharb commented Apr 16, 2025

ah, true. should i change the example to something else, and then add a note indicating that SafeSet is immune to this?

Copy link
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

oow, nice ergonomic improvement indeed 🙌

constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
constructor(i) {
super(
i && ArrayIsArray(i) ?
Copy link
Member

Choose a reason for hiding this comment

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

nit: the truthy test is probably superfluous. I can't find the implementation of ArrayIsArray, but it probably already does that, so this micro-op is likely actually a micro-deop.

Copy link
Member Author

Choose a reason for hiding this comment

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

my assumption is that a truthiness check is much cheaper than any function call, but if a benchmark indicates that's untrue i'd be happy to change it in the future.

@ljharb ljharb marked this pull request as draft April 18, 2025 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants