Skip to content

feat(rollup): run terser in threads - #833

Closed
Toxicable wants to merge 3 commits into
bazel-contrib:masterfrom
Toxicable:terser/threads
Closed

Toxicable wants to merge 3 commits into
bazel-contrib:masterfrom
Toxicable:terser/threads

Conversation

@Toxicable

@Toxicable Toxicable commented Jun 8, 2019 •

Copy link
Copy Markdown

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

@alexeagle alexeagle left a comment

Copy link
Copy Markdown
Collaborator

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?

Comment thread 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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread 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`)

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

Comment thread internal/rollup/terser-wrapped.js Outdated
* @param {boolean} debug
*/
async function runTerserParallel(files, debug) {
// TODO: do some concurrency limiting here

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread 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

Copy link
Copy Markdown
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"

args.add("--beautify")

# we need this arg to enable worker_threads
args.add("--node_options=--experimental-worker")

Copy link
Copy Markdown
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
Copy Markdown
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?

@Globegitter Globegitter Jun 11, 2019 •

Copy link
Copy Markdown

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
Copy Markdown
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.

@Toxicable Toxicable left a comment

Copy link
Copy Markdown
Author

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.

"shelljs": "0.8.3",
"source-map-explorer": "^1.7.0",
"terser": "^3.16.1",
"p-limit": "^2.2.0",

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

@Toxicable Toxicable Jun 11, 2019 •

Copy link
Copy Markdown
Author

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

Comment thread 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();

@clydin clydin Jun 11, 2019 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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