node icon indicating copy to clipboard operation
node copied to clipboard

degrading performance after using child_process

Open dannymarsland opened this issue 10 years ago • 44 comments

After running a child process ( using exec or spawn ) I have found that the performance of my node.js application decreases by a factor of 10. Below is a contrived example and output.

var exec = require('child_process').exec;

function runExpensiveOperation(times) {
  while(times > 0) {
    console.time('expensiveOperation');
    var str = 'lorem';
    for ( var i=0;i< 10000000; i++) {
      // string concatenation
      str = str.length < 1000 ? str + str : '';
      // math operation
      i * i * i;
    }
    console.timeEnd('expensiveOperation');
    times--;
  }
}

console.log('PRE EXEC');
runExpensiveOperation(10);
exec('echo "hello"');
console.log('POST EXEC');
runExpensiveOperation(10);

Output:

PRE EXEC
expensiveOperation: 66.458ms
expensiveOperation: 65.735ms
expensiveOperation: 69.237ms
expensiveOperation: 65.269ms
expensiveOperation: 69.133ms
expensiveOperation: 65.639ms
expensiveOperation: 67.944ms
expensiveOperation: 63.595ms
expensiveOperation: 64.153ms
expensiveOperation: 65.093ms
POST EXEC
expensiveOperation: 715.861ms
expensiveOperation: 739.671ms
expensiveOperation: 714.546ms
expensiveOperation: 714.845ms
expensiveOperation: 745.719ms
expensiveOperation: 743.240ms
expensiveOperation: 716.481ms
expensiveOperation: 732.916ms
expensiveOperation: 736.576ms
expensiveOperation: 742.416ms

In addition, this problem only occurs if the string concatenation AND math operation are run in the expensiveOperation - if either are commented out then there is no issue.

  • Version: 5.8.0
  • Platform: Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64 ( Macbook Air OS X El Capitan )
  • Subsystem: child_process

dannymarsland avatar Mar 10 '16 02:03 dannymarsland

Upon reflection, I think the above V8 issue might be worth backporting in full. The issue does includes a lot of commits, and does change the compiler to generate different machine instructions. The changes themselves aren't complex however. Furthermore these instructions have been getting a significant amount of testing on a huge number on macs through Chrome 48 and 49. I am feeling comfortable that this is safe to backport.

ofrobots avatar Mar 23 '16 16:03 ofrobots

I don't think it's a good idea to back-port a huge body of code for a relatively minor performance issue on a platform where top performance isn't that important.

bnoordhuis avatar Mar 23 '16 16:03 bnoordhuis

Fair enough.

ofrobots avatar Mar 23 '16 20:03 ofrobots

I'll close out the issue. v5.x is EOL and v6.x is not affected; affected v4.x users can use --noenable_avx as a workaround. The bug has been fixed in newer OS X releases, I believe.

bnoordhuis avatar Oct 12 '16 09:10 bnoordhuis