-
Notifications
You must be signed in to change notification settings - Fork 522
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
feat(rollup): run terser in threads #833
Conversation
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.
cool! need any help to get the CI green?
internal/rollup/terser-wrapped.js
Outdated
const DEBUG = false; | ||
// worker_threads is only avilable on version > 10.5.0 | ||
const [major, minor] = process.version.replace('v', '').split('.'); | ||
const WORKER_THREADS_AVILABLE = major > 10 || major == 10 && minor > 5 |
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.
nit: why not just try/catch on the require
call?
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.
should also be minor >= 5
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.
Total guess here, but I would assume that doing arithmetic is faster than recovering from an exception.
However, in hindsight, it's probably the smallest amount of performance difference here.
internal/rollup/terser-wrapped.js
Outdated
console.warn( | ||
`WARNING: worker_threads not are not avilable on your version of Nodejs: ${process.version} | ||
Running Terser in serial mode | ||
Upgrade your Nodejs version to 10.5.0 minimum for worker_threads to be avilable`) |
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.
we should be more specific here, to update the /WORKSPACE file, otherwise users will update the nodejs on their machine and it still won't work
internal/rollup/terser-wrapped.js
Outdated
* @param {boolean} debug | ||
*/ | ||
async function runTerserParallel(files, debug) { | ||
// TODO: do some concurrency limiting here |
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 have to finish the TODO before we merge/release this?
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.
We should. context switching will destroy performance gains.
os.cpus().length - 1
is typically a good cap.
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 think a worker pool would also make sense here. There's no need to tear down and bring up new threads and re-initialize terser for each file.
internal/rollup/terser-wrapped.js
Outdated
} else { | ||
console.warn( | ||
`WARNING: worker_threads not are not avilable on your version of Nodejs: ${process.version} | ||
Running Terser in serial mode |
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.
"serial mode" is a bit obtuse for a novice user, how about just "optimizing one file at a time"
@@ -334,6 +334,9 @@ def _run_terser(ctx, input, output, map_output, debug = False, comments = True, | |||
args.add("--debug") | |||
args.add("--beautify") | |||
|
|||
# we need this arg to enable worker_threads | |||
args.add("--node_options=--experimental-worker") |
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.
If on Node 12+ this shouldn't be added
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.
Fair, not sure how to get the nodejs version in in skylark though.
@alexeagle You mentioned there's a way to do it the other day?
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.
Once we have toolchains in it would actually be possible to add the node version to the toolchain and then one could get it in starlark. Not sure though how it would be possible currently though.
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.
If problematic, node 12 currently won't error with the option present. So this could be addressed moving forward.
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.
Thanks for the review.
I do have a few points I'm not 100% on, I've commented on them below.
If we could resolve those then I think we're good to go here.
@@ -12,6 +12,7 @@ | |||
"shelljs": "0.8.3", | |||
"source-map-explorer": "^1.7.0", | |||
"terser": "^3.16.1", | |||
"p-limit": "^2.2.0", |
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.
This dosen't actually work, how do we add internal npm modules?
return {inputFile, outputFile, sourceMapFile: outputFile + '.map'}; | ||
}) | ||
|
||
// TODO: allow config to force serial workers here |
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.
@Globegitter Suggested this one.
It would be a good addition but probably not apart of this first cut.
* @param {boolean} debug | ||
*/ | ||
async function runTerserParallel(files, debug) { | ||
// TODO: make this configurable |
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.
Same here, would be a good candidate for config, but we'll worry about it later I think
if (result.error) { | ||
// TODO: figure out error reporting here | ||
// scroll up a little from this: https://github.com/terser-js/terser#minify-options | ||
throw result.error; |
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.
Im not certain of the format of this error object, we need to do some trial and error I think to figure out what kind of errors we get from Terser.
However, I'm not familiar enough with Terser to know how to cause an error :P
internal/rollup/terser-wrapped.js
Outdated
|
||
if (DEBUG) console.error(`Running node ${args.join(' ')}`); | ||
const terserOptions = buildTerserConfig(sourceMapFile, debug) | ||
const inputCode = (await fs.promises.readFile(inputFile)).toString(); |
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.
Are character encoding and BOM presence relevant here? This is currently assuming UTF-8.
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 really don't know. We could add a trim()
call which would remove the BOM if it is actually encoded as that.
I don't know the impact of stripping the BOM and re-writing it as plain UTF8 once we're done
replaced with #1172 |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information