Skip to content

node-api: use container swap in DrainFinalizerQueue to reduce erase overhead #57861

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

Closed

Conversation

mertcanaltin
Copy link
Member

@mertcanaltin mertcanaltin commented Apr 13, 2025

use container swap in DrainFinalizerQueue to reduce erase overhead

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/node-api

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. labels Apr 13, 2025
Copy link

codecov bot commented Apr 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.13%. Comparing base (6102159) to head (4fe09e9).
Report is 51 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57861      +/-   ##
==========================================
- Coverage   90.15%   90.13%   -0.02%     
==========================================
  Files         630      629       -1     
  Lines      186756   186629     -127     
  Branches    36653    36623      -30     
==========================================
- Hits       168369   168227     -142     
- Misses      11189    11199      +10     
- Partials     7198     7203       +5     
Files with missing lines Coverage Δ
src/node_api.cc 76.19% <100.00%> (+0.02%) ⬆️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

The assumption in the comment "userland code can delete additional references in one finalizer" will break with the new change.

However, this assumption was not reflected in test test/js-native-api/6_object_wrap/6_object_wrap.cc. Would you mind updating the test to verify deleting another reference in the destructor of MyObject?

MyObject::~MyObject() {
  napi_delete_reference(env_, nested_);
  napi_delete_reference(env_, wrapper_);
}

@mertcanaltin
Copy link
Member Author

The assumption in the comment "userland code can delete additional references in one finalizer" will break with the new change.

However, this assumption was not reflected in test test/js-native-api/6_object_wrap/6_object_wrap.cc. Would you mind updating the test to verify deleting another reference in the destructor of MyObject?

MyObject::~MyObject() {
  napi_delete_reference(env_, nested_);
  napi_delete_reference(env_, wrapper_);
}

I updated, thanks

@mertcanaltin
Copy link
Member Author

tests passed in my local environment for macos but I think there is a problem in linux I will try

@mertcanaltin mertcanaltin changed the title node-api: optimize finalizer queue using container swap [WIP] node-api: optimize finalizer queue using container swap Apr 15, 2025
@mertcanaltin mertcanaltin changed the title [WIP] node-api: optimize finalizer queue using container swap node-api: optimize finalizer queue using container swap Apr 22, 2025
@legendecas
Copy link
Member

#57861 (review) was not addressed.

The change proposed does not fulfill the behavior as described in the original comment:

node/src/node_api.cc

Lines 116 to 118 in 68cc1c9

// As userland code can delete additional references in one finalizer,
// the list of pending finalizers may be mutated as we execute them, so
// we keep iterating it until it is empty.

This change will crash the test #57981.

@mertcanaltin
Copy link
Member Author

#57861 (review) was not addressed.

The change proposed does not fulfill the behavior as described in the original comment:

node/src/node_api.cc

Lines 116 to 118 in 68cc1c9

// As userland code can delete additional references in one finalizer,
// the list of pending finalizers may be mutated as we execute them, so
// we keep iterating it until it is empty.

This change will crash the test #57981.

thanks I updated it as you indicated here
#57861 (comment)

@mertcanaltin mertcanaltin changed the title node-api: optimize finalizer queue using container swap [wip] node-api: optimize finalizer queue using container swap Apr 24, 2025
@mhdawson mhdawson moved this from Need Triage to In Progress in Node-API Team Project Apr 25, 2025
@mertcanaltin mertcanaltin changed the title [wip] node-api: optimize finalizer queue using container swap node-api: drain finalizer queue one-by-one to support nested napi_ref deletion May 6, 2025
@mertcanaltin mertcanaltin changed the title node-api: drain finalizer queue one-by-one to support nested napi_ref deletion node-api: drain finalizer queue per-item to support nested napi_ref deletion May 6, 2025
@mertcanaltin mertcanaltin force-pushed the mert/optimize/finalizer-queue branch from 61b9793 to 8e74c92 Compare May 6, 2025 07:58
@mertcanaltin mertcanaltin force-pushed the mert/optimize/finalizer-queue branch from 8e74c92 to a696b8b Compare May 6, 2025 08:02
@mertcanaltin mertcanaltin changed the title node-api: drain finalizer queue per-item to support nested napi_ref deletion node-api: per-item finalizer queue drain for nested napi_ref deletion May 6, 2025
@mertcanaltin mertcanaltin changed the title node-api: per-item finalizer queue drain for nested napi_ref deletion wip node-api: per-item finalizer queue drain for nested napi_ref deletion May 6, 2025
@mertcanaltin mertcanaltin changed the title wip node-api: per-item finalizer queue drain for nested napi_ref deletion [wip] node-api: use container swap in DrainFinalizerQueue to reduce erase overhead May 6, 2025
@mertcanaltin
Copy link
Member Author

mertcanaltin commented May 6, 2025

Hello @vmoroz, @legendecas, @mhdawson

I’ve updated this PR to implement the per-item finalizer drain as discussed—switching from value-based erase(ref_tracker) to iterator-based erase(it) for true O(1) removal:

- pending_finalizers.erase(ref_tracker);
+ auto it = pending_finalizers.begin();
+ pending_finalizers.erase(it);

@mertcanaltin mertcanaltin changed the title [wip] node-api: use container swap in DrainFinalizerQueue to reduce erase overhead node-api: use container swap in DrainFinalizerQueue to reduce erase overhead May 6, 2025
@mertcanaltin mertcanaltin requested a review from legendecas May 11, 2025 20:44
Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

FWIW, the existing code always erase the first item on the list. Though the new code LGTM as well.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label May 11, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 11, 2025
@nodejs-github-bot
Copy link
Collaborator

@vmoroz
Copy link
Member

vmoroz commented May 11, 2025

Hello @vmoroz, @legendecas, @mhdawson

I’ve updated this PR to implement the per-item finalizer drain as discussed—switching from value-based erase(ref_tracker) to iterator-based erase(it) for true O(1) removal:

- pending_finalizers.erase(ref_tracker);
+ auto it = pending_finalizers.begin();
+ pending_finalizers.erase(it);

@mertcanaltin , I do not see any meaningful difference from the existing code besides the declaring of the unnecessary auto it variable. It is better to abandon the PR if it does not add any value.

@mertcanaltin
Copy link
Member Author

Hello @vmoroz, @legendecas, @mhdawson
I’ve updated this PR to implement the per-item finalizer drain as discussed—switching from value-based erase(ref_tracker) to iterator-based erase(it) for true O(1) removal:

- pending_finalizers.erase(ref_tracker);
+ auto it = pending_finalizers.begin();
+ pending_finalizers.erase(it);

@mertcanaltin , I do not see any meaningful difference from the existing code besides the declaring of the unnecessary auto it variable. It is better to abandon the PR if it does not add any value.

thanks I am closing this pr as it does not contribute to the current code, thanks a lot for the information

@github-project-automation github-project-automation bot moved this from In Progress to Done in Node-API Team Project May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants