-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Significant slowdown of createContext and runInContext #29842
Comments
Reproducing here for posterity: const vm = require('vm')
const ctxFn = new vm.Script(`
var b = Math.random();
var c = a + b;
`);
function run(m) {
const ctx = {a: 'a'};
vm.createContext(ctx);
for (let i = 0; i < m; i++) {
ctxFn.runInContext(ctx);
}
}
function test(n, m) {
const s = Date.now();
for (let i = 0; i < m; i++) {
run(m);
}
console.log(Date.now() - s);
}
test(1000, 10); Are you sure you're testing what you think you're testing? |
This is what I get from our vm benchmarks using
The slowdown may come from createContext instead |
I tried with this benchmark 'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [100]
});
const vm = require('vm');
const ctxFn = new vm.Script(`
var b = Math.random();
var c = a + b;
`);
function main({ n }) {
bench.start();
let context;
for (var i = 0; i < n; i++) {
context = vm.createContext({ a: 'a' });
}
bench.end(n);
ctxFn.runInContext(context);
} There is a -75.07% performance regression on master with |
PR-URL: #29845 Refs: #29842 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
I proflied the benchmark (stripped out into a separate script). Output of --prof:
And of I suspect this is related to #29766 - one possibility is to make primordials behind a configure-time flag until we fix the performance issues. |
Oh, wait, I realized that the screenshot in #29842 (comment) should not contain frames of |
PR-URL: #29845 Refs: #29842 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Reposted from #29925
|
Hmm, or maybe we could include one more context with the interceptors in the embedded snapshot (however we have to register the interceptors as external references first), and use it in |
This is also locking up the event loop in version 12. Any more info on the availability of a configure-time flag until this is fixed? Or is the only option to revert to Node 8/10? Thanks |
It seems rather difficult to make a toggle for the primordials at configure-time at this point, but another idea would be to lazily initialize the primoridals only when they are necessary for vm contexts - in most cases, there should be no need because it's used by builtin modules that probably will have their own set of primordials when being passed into a context. I am still looking into whether this is safe to do - it does impose some requirements for cross-context support of builtins (e.g. |
Lazily initialize primordials when cross-context support for builtins is needed to fix the performance regression in context creation. PR-URL: #31738 Fixes: #29842 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
Lazily initialize primordials when cross-context support for builtins is needed to fix the performance regression in context creation. PR-URL: nodejs#31738 Fixes: nodejs#29842 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
Lazily initialize primordials when cross-context support for builtins is needed to fix the performance regression in context creation. PR-URL: #31738 Fixes: #29842 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
In the latest v12.11.0 these methods work very slowly relative to v10.16.3.
Here is a simple example where contexts are created and then a script is run:
https://gist.github.com/yrsh/28d274727c1e67f88d0cdf2e2091b6c2
For example at my local machine, execution time with v12 is 21 ms, but with v10 it is 7.
The text was updated successfully, but these errors were encountered: