Skip to content

lib: make new SafeSet(arr) safe#57895

Draft
ljharb wants to merge 1 commit into
nodejs:mainfrom
ljharb:safeset
Draft

lib: make new SafeSet(arr) safe#57895
ljharb wants to merge 1 commit into
nodejs:mainfrom
ljharb:safeset

Conversation

@ljharb

@ljharb ljharb commented Apr 16, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
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

@ShogunPanda ShogunPanda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@ljharb

ljharb commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

(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)

Comment thread lib/internal/error_serdes.js Outdated
Comment thread doc/contributing/primordials.md Outdated
@aduh95 aduh95 added the needs-benchmark-ci PR that need a benchmark CI run. label Apr 16, 2025
@aduh95

aduh95 commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

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.

Comment thread lib/internal/per_context/primordials.js Outdated
Comment thread lib/internal/per_context/primordials.js
@ljharb

ljharb commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

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

aduh95 commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

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

ljharb commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

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

@JakobJingleheimer JakobJingleheimer left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

@github-actions github-actions Bot added the stale label Apr 20, 2026
@github-actions github-actions Bot removed the stale label Apr 21, 2026
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.

10 participants