Skip to content

Commit b46cf35

Browse files
vsemozhetbytMylesBorins
authored andcommitted
child_process: fix deoptimizing use of arguments
Remove use of arguments in normalizeExecArgs() and normalizeSpawnArguments(). Refs: #10323 PR-URL: #11535 Backport-PR-URL: #13752 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 4dff128 commit b46cf35

2 files changed

Lines changed: 155 additions & 18 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
constcp=require('child_process');
5+
6+
constcommand='echo';
7+
constargs=['hello'];
8+
constoptions={};
9+
constcb=()=>{};
10+
11+
constconfigs={
12+
n: [1e3],
13+
methodName: [
14+
'exec','execSync',
15+
'execFile','execFileSync',
16+
'spawn','spawnSync',
17+
],
18+
params: [1,2,3,4],
19+
};
20+
21+
constbench=common.createBenchmark(main,configs);
22+
23+
functionmain(conf){
24+
constn=+conf.n;
25+
constmethodName=conf.methodName;
26+
constparams=+conf.params;
27+
28+
constmethod=cp[methodName];
29+
30+
switch(methodName){
31+
case'exec':
32+
switch(params){
33+
case1:
34+
bench.start();
35+
for(leti=0;i<n;i++)method(command).kill();
36+
bench.end(n);
37+
break;
38+
case2:
39+
bench.start();
40+
for(leti=0;i<n;i++)method(command,options).kill();
41+
bench.end(n);
42+
break;
43+
case3:
44+
bench.start();
45+
for(leti=0;i<n;i++)method(command,options,cb).kill();
46+
bench.end(n);
47+
break;
48+
}
49+
break;
50+
case'execSync':
51+
switch(params){
52+
case1:
53+
bench.start();
54+
for(leti=0;i<n;i++)method(command);
55+
bench.end(n);
56+
break;
57+
case2:
58+
bench.start();
59+
for(leti=0;i<n;i++)method(command,options);
60+
bench.end(n);
61+
break;
62+
}
63+
break;
64+
case'execFile':
65+
switch(params){
66+
case1:
67+
bench.start();
68+
for(leti=0;i<n;i++)method(command).kill();
69+
bench.end(n);
70+
break;
71+
case2:
72+
bench.start();
73+
for(leti=0;i<n;i++)method(command,args).kill();
74+
bench.end(n);
75+
break;
76+
case3:
77+
bench.start();
78+
for(leti=0;i<n;i++)method(command,args,options).kill();
79+
bench.end(n);
80+
break;
81+
case4:
82+
bench.start();
83+
for(leti=0;i<n;i++)method(command,args,options,cb).kill();
84+
bench.end(n);
85+
break;
86+
}
87+
break;
88+
case'execFileSync':
89+
switch(params){
90+
case1:
91+
bench.start();
92+
for(leti=0;i<n;i++)method(command);
93+
bench.end(n);
94+
break;
95+
case2:
96+
bench.start();
97+
for(leti=0;i<n;i++)method(command,args);
98+
bench.end(n);
99+
break;
100+
case3:
101+
bench.start();
102+
for(leti=0;i<n;i++)method(command,args,options);
103+
bench.end(n);
104+
break;
105+
}
106+
break;
107+
case'spawn':
108+
switch(params){
109+
case1:
110+
bench.start();
111+
for(leti=0;i<n;i++)method(command).kill();
112+
bench.end(n);
113+
break;
114+
case2:
115+
bench.start();
116+
for(leti=0;i<n;i++)method(command,args).kill();
117+
bench.end(n);
118+
break;
119+
case3:
120+
bench.start();
121+
for(leti=0;i<n;i++)method(command,args,options).kill();
122+
bench.end(n);
123+
break;
124+
}
125+
break;
126+
case'spawnSync':
127+
switch(params){
128+
case1:
129+
bench.start();
130+
for(leti=0;i<n;i++)method(command);
131+
bench.end(n);
132+
break;
133+
case2:
134+
bench.start();
135+
for(leti=0;i<n;i++)method(command,args);
136+
bench.end(n);
137+
break;
138+
case3:
139+
bench.start();
140+
for(leti=0;i<n;i++)method(command,args,options);
141+
bench.end(n);
142+
break;
143+
}
144+
break;
145+
}
146+
}

‎lib/child_process.js‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,10 @@ exports._forkChild = function(fd) {
7474
};
7575

7676

77-
functionnormalizeExecArgs(command/*, options, callback*/){
78-
letoptions;
79-
letcallback;
80-
81-
if(typeofarguments[1]==='function'){
77+
functionnormalizeExecArgs(command,options,callback){
78+
if(typeofoptions==='function'){
79+
callback=options;
8280
options=undefined;
83-
callback=arguments[1];
84-
}else{
85-
options=arguments[1];
86-
callback=arguments[2];
8781
}
8882

8983
// Make a shallow copy so we don't clobber the user's options object.
@@ -303,18 +297,15 @@ function _convertCustomFds(options) {
303297
}
304298
}
305299

306-
functionnormalizeSpawnArguments(file/*, args, options*/){
307-
varargs,options;
308-
309-
if(Array.isArray(arguments[1])){
310-
args=arguments[1].slice(0);
311-
options=arguments[2];
312-
}elseif(arguments[1]!==undefined&&
313-
(arguments[1]===null||typeofarguments[1]!=='object')){
300+
functionnormalizeSpawnArguments(file,args,options){
301+
if(Array.isArray(args)){
302+
args=args.slice(0);
303+
}elseif(args!==undefined&&
304+
(args===null||typeofargs!=='object')){
314305
thrownewTypeError('Incorrect value of args option');
315306
}else{
307+
options=args;
316308
args=[];
317-
options=arguments[1];
318309
}
319310

320311
if(options===undefined)

0 commit comments

Comments
 (0)