-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
n-api: cache Symbol.hasInstance #12246
n-api: cache Symbol.hasInstance #12246
Conversation
This improves the performance of napi_instanceof() by retrieving Symbol.hasInstance from the global object once and then storing a persistent reference to it in the env. Re nodejs/abi-stable-node#209 Re nodejs/abi-stable-node#214
} | ||
v8::Isolate* isolate; | ||
v8::Persistent<v8::Value> last_exception; | ||
v8::Persistent<v8::Value> has_instance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need a copy in every env ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ie could we share one copy across all envs ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That brings back the question of what prompts the creation of a new env? Currently it is the loading of a module. But should it be a new isolate?
Also, how likely is this to be a large waste of memory? I mean, are we going to have tons of native modules loaded all at once?
I guess when there really start to appear multiple isolates (or whatever keys meaning "this JS world and not the other" because "this v8::Local<v8::Value>
is only valid in this world") then we can re-examine our choice of one env per module, because the problem will have been solved upstream and we need only follow suit. We might then introduce a std::map
keyed to whatever the engine then provides, or we may have a better mechanism for tacking data onto such a key and we'll once again cast.
... and we can do all this without breaking any ABI, since it's purely within our implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wondered if the exact same code, except that having has_instance be static would work. The cache would be created the first time an env was created. The only concern was whether this could happen in parallel, but given Node.js generally single thread nature that may not be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhdawson because eventually the env will be keyed to some kind of context, and we are preparing ourselves for that. Otherwise last_exception
could also be static - and it used to be, and the env was precisely equal to the isolate. The problem is we can't currently tack onto the isolate because there is no reliable mechanism for that, so we're instantiating an environment per module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, yes, it would work perfectly, and it's only our desire to be proactive that prompted us to implement this solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, got it.
Alternatively, we can convince V8 to add a |
@TimothyGu why not go for the whole nine yards and ask them to expose the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Discussion of getting changes into v8 is a good follow on, but don't think it should block landing this PR. CI run: https://ci.nodejs.org/job/node-test-pull-request/7270/ |
CI run was green landing. |
landed as 8fbace1 |
This improves the performance of napi_instanceof() by retrieving Symbol.hasInstance from the global object once and then storing a persistent reference to it in the env. PR-URL: #12246 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
It appears this PR may be making a n-api test fail on Windows. Here is a CI run of current master: https://ci.nodejs.org/job/node-test-binary-windows/7558/
Perhaps something was changed since the last CI run? All other platforms seem to be unaffected. |
@mscdex Looking for an n-api change that might have landed after CI was run for this one, the only candidate I'm seeing is 8460284. Maybe that change in conjunction with this one is the problem on Windows but either change by itself is fine? (Totally speculating here, but that's my best guess?) |
solved regression by @gabrielschulhof in #12283 |
I'm considering adding Is this what you need? |
Yes, the options were either doing that or calling it something other than
Yes, looks like it. Thank you! 💚 |
Alright. Will have to add some tests. It will not land before V8 6.0 though because we are cutting the 5.9 branch very soon. |
I changed the API to |
The changes for Do we want to open an issue to track so that this can be used as soon as Node.js upgrades to V8 6.0? |
@hashseed Usually we have an issue for anything related to a V8 upgrade. Might make sense to start that now for 60. |
Just as a reminder, |
I'm working on a PR to use v8::Value::InstanceOf in napi. |
This improves the performance of napi_instanceof() by retrieving Symbol.hasInstance from the global object once and then storing a persistent reference to it in the env. PR-URL: nodejs#12246 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
This improves the performance of napi_instanceof() by retrieving Symbol.hasInstance from the global object once and then storing a persistent reference to it in the env. Backport-PR-URL: #19447 PR-URL: #12246 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
This improves the performance of napi_instanceof() by retrieving
Symbol.hasInstance from the global object once and then storing a
persistent reference to it in the env.
Re nodejs/abi-stable-node#209
Re nodejs/abi-stable-node#214
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
N-API