Skip to content

Commit

Permalink
Merge pull request emberjs#165 from babel/worker-threads
Browse files Browse the repository at this point in the history
If available use node workerThreads
  • Loading branch information
stefanpenner authored Feb 26, 2019
2 parents c0e7518 + 2483538 commit 56c45d0
Show file tree
Hide file tree
Showing 8 changed files with 1,563 additions and 294 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: node_js
node_js:
- "lts/boron"
- "lts/carbon"
- "6"
- "8"
- "10"
- "11"

cache:
yarn: true
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
environment:
matrix:
- nodejs_version: "10"
- nodejs_version: "11"

# Install scripts. (runs after repo cloning)
install:
Expand Down
11 changes: 2 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,8 @@ function Babel(inputTree, _options) {
let errors = result.errors;

if ((this.throwUnlessParallelizable || process.env.THROW_UNLESS_PARALLELIZABLE) && isParallelizable === false) {
try {
throw new Error(this.toString() +
' was configured to `throwUnlessParallelizable` and was unable to parallelize a plugin. \nplugins:\n' + joinCount(errors) + '\nPlease see: https://github.com/babel/broccoli-babel-transpiler#parallel-transpilation for more details');
} catch(e) {
debugger;

let result = transformIsParallelizable(options);
throw e;
}
throw new Error(this.toString() +
' was configured to `throwUnlessParallelizable` and was unable to parallelize a plugin. \nplugins:\n' + joinCount(errors) + '\nPlease see: https://github.com/babel/broccoli-babel-transpiler#parallel-transpilation for more details');
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/parallel-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getWorkerPool() {
if (existingPool) {
pool = existingPool;
} else {
pool = workerpool.pool(path.join(__dirname, 'worker.js'), { maxWorkers: JOBS });
pool = workerpool.pool(path.join(__dirname, 'worker.js'), { nodeWorker: 'auto', maxWorkers: JOBS });
process[globalPoolID] = pool;
}
return pool;
Expand Down Expand Up @@ -268,5 +268,6 @@ module.exports = {
serialize,
buildFromParallelApiInfo,
transformString,
humanizePlugin
humanizePlugin,
getWorkerPool
};
7 changes: 4 additions & 3 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const ParallelApi = require('./parallel-api');
function transform(string, options) {
return new Promise(resolve => {
let result = transpiler.transform(string, ParallelApi.deserialize(options));
// this is large, not used, and can't be serialized anyway
delete result.ast;

resolve(result);
resolve({
code: result.code,
metadata: result.metadata
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"heimdalljs-logger": "^0.1.9",
"json-stable-stringify": "^1.0.1",
"rsvp": "^4.8.4",
"workerpool": "^3.1.0"
"workerpool": "^3.1.1"
},
"devDependencies": {
"@babel/plugin-transform-block-scoping": "^7.2.0",
Expand Down
47 changes: 32 additions & 15 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require('fs');
const os = require('os');
const expect = require('chai').expect;
const path = require('path');
const ps = require('ps-node');
const Babel = require('../');
const helpers = require('broccoli-test-helpers');
const stringify = require('json-stable-stringify');
Expand Down Expand Up @@ -1480,26 +1479,44 @@ describe('workerpool', function() {
delete require.cache[parallelApiPath];
let ParallelApiTwo = require(PATH);

let lookup = RSVP.denodeify(ps.lookup);

return Promise.all([
ParallelApiOne.transformString(stringToTransform, options),
ParallelApiOne.transformString(stringToTransform, options),
ParallelApiTwo.transformString(stringToTransform, options),
ParallelApiTwo.transformString(stringToTransform, options),
]).then(() => {
// for ps-node,
// unix paths look like 'broccoli-babel-transpiler/lib/worker.js'
// windows paths look like 'broccoli-babel-transpiler\\lib\\worker.js' (2 path separators)
const processMatch = (os.platform() === 'win32')
? 'broccoli-babel-transpiler\\\\lib\\\\worker.js'
: path.join('broccoli-babel-transpiler', 'lib', 'worker.js');
return lookup({
command: 'node',
arguments: processMatch,
});
}).then((resultList) => {
expect(resultList.length).to.eql(2);
const pool = ParallelApi.getWorkerPool();
expect(pool).to.eql(ParallelApiTwo.getWorkerPool());
expect(pool.workers.length).to.eql(2);
});
});

it('if available should use workerThreads', function() {
this.timeout(5*1000);

return Promise.all([
ParallelApi.transformString(stringToTransform, options),
ParallelApi.transformString(stringToTransform, options),
]).then(() => {
const pool = ParallelApi.getWorkerPool();
let hasWorkerThreads = false;
try {
require('worker_threads');
hasWorkerThreads = true;
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
hasWorkerThreads = false;
} else {
throw e;
}
}

expect(pool.workers.length).to.eql(2);
if (hasWorkerThreads) {
pool.workers.forEach(worker => expect(worker.worker.isWorkerThread).to.eql(true));
} else {
pool.workers.forEach(worker => expect(worker.worker.isChildProcess).to.eql(true));
}
});
});
});
Loading

0 comments on commit 56c45d0

Please sign in to comment.