Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

Toxicable
Copy link

@Toxicable Toxicable commented Jun 8, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Copy link
Collaborator

@alexeagle alexeagle left a 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?

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
Copy link
Collaborator

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?

Copy link
Contributor

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

Copy link
Author

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.

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`)
Copy link
Collaborator

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

* @param {boolean} debug
*/
async function runTerserParallel(files, debug) {
// TODO: do some concurrency limiting here
Copy link
Collaborator

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

} else {
console.warn(
`WARNING: worker_threads not are not avilable on your version of Nodejs: ${process.version}
Running Terser in serial mode
Copy link
Collaborator

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")
Copy link
Contributor

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

Copy link
Author

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?

Copy link
Contributor

@Globegitter Globegitter Jun 11, 2019

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.

Copy link
Contributor

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.

Copy link
Author

@Toxicable Toxicable left a 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",
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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;
Copy link
Author

@Toxicable Toxicable Jun 11, 2019

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


if (DEBUG) console.error(`Running node ${args.join(' ')}`);
const terserOptions = buildTerserConfig(sourceMapFile, debug)
const inputCode = (await fs.promises.readFile(inputFile)).toString();
Copy link
Contributor

@clydin clydin Jun 11, 2019

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.

Copy link
Author

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

@Toxicable
Copy link
Author

replaced with #1172

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants