Skip to content

Spawn child process for rimraf (speeds up lerna clean) - #547

Merged
gigabo merged 1 commit into
lerna:masterfrom
roblg:externalize-rimraf
Jan 31, 2017
Merged

gigabo merged 1 commit into
lerna:masterfrom
roblg:externalize-rimraf

Conversation

@roblg

@roblg roblg commented Jan 29, 2017 •

Copy link
Copy Markdown
Contributor

Improves performance of lerna clean on large projects.

I fully recognize that this could be contentious, but I also didn't see an issue/conversation about it, so I figured it couldn't hurt to submit. This shaves ~19s (21% improvement!) off our monorepo build with lerna:

# external rimraf (this PR)
lerna clean --concurrency 6 --loglevel debug --yes  81.95s user 114.90s system 281% cpu 1:10.00 total
lerna clean --concurrency 6 --loglevel debug --yes  78.54s user 117.52s system 282% cpu 1:09.41 total

lerna clean --concurrency 3 --loglevel debug --yes  71.90s user 108.78s system 249% cpu 1:12.42 total


#internal rimraf
lerna clean --concurrency 6 --loglevel debug --yes  55.34s user 85.40s system 156% cpu 1:29.84 total

lerna clean --concurrency 1 --loglevel debug --yes  59.14s user 94.69s system 142% cpu 1:48.14 total

For the purposes of conversation, I did the simplest thing I could think of, but I'm also open to exploring alternative solutions. I suspect that shelling out to rm and/or the windows equivalent would be even faster still, but I'll resist the urge to do that. 😁

Update: One downside to this approach is that rimraf isn't a require-time dependency anymore, which might cause static analysis (or a human with a restrictive regex) to erroneously conclude that it's not actually being used.

Update 2: Added a couple of additional timings for posterity (--concurrency 3 for external, --concurrency 1 for internal).

Comment thread src/FileSystemUtilities.js Outdated
@logger.logifyAsync()
static rimraf(filePath, callback) {
rimraf(filePath, callback);
ChildProcessUtilities.spawn(join(__dirname, "../node_modules/.bin/rimraf"), [filePath], {}, callback);

@doug-wade doug-wade Jan 30, 2017 •

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.

nit: I think we should use $(npm bin) here

@roblg roblg Jan 30, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do. Something like this?

const npmBin = ChildProcessUtilities.execSync("npm bin", {cwd: __dirname});
ChildProcessUtilities.spawn(join(npmBin, "rimraf"), [filePath], {}, callback)

@roblg
roblg force-pushed the externalize-rimraf branch 2 times, most recently from 7a6195d to 91e29f4 Compare January 30, 2017 17:25
Improves performance of `lerna clean` on large projects.
@roblg
roblg force-pushed the externalize-rimraf branch from 91e29f4 to 04aed0f Compare January 30, 2017 17:35
@roblg

roblg commented Jan 30, 2017

Copy link
Copy Markdown
Contributor Author

@doug-wade updated to use npm bin. I waffled on exec vs. execSync for that call, and ended up settling on exec. The perf is basically the same, and I think they're both roughly equally readable (surprisingly), but waiting for a synchronous exec in a function that's called in loops seems... wrong somehow.

@doug-wade doug-wade left a comment

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.

This seems reasonable to me

@logger.logifyAsync()
static rimraf(filePath, callback) {
rimraf(filePath, callback);
ChildProcessUtilities.exec("npm bin", {cwd: __dirname}, (err, npmBinPath) => {

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.

Can/should we cache this? Seems wasteful to shell out for it every time when it's constant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that thought too, and then decided that this was probably roughly equivalent to a shell script that did something like $(npm bin)/rimraf /some/dir/node_modules, so I didn't bother fixing it. It seems like the cost of running rimraf (node-based filesystem walking and unlinking) would dwarf the npm bin exec for most projects anyway?

If we want to cache it, I guess we'd do that in a module-level variable in FileSystemUtilities? Wouldn't be hard to add.

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.

Yeah, maybe premature optimization.

@gigabo

gigabo commented Jan 31, 2017

Copy link
Copy Markdown
Contributor

Thanks @roblg!

@gigabo
gigabo merged commit 0b016de into lerna:master Jan 31, 2017
@gigabo gigabo changed the title Proposal: Spawn child process for rimraf Spawn child process for rimraf (speeds up lerna clean) Jan 31, 2017
@gigabo

gigabo commented Jan 31, 2017

Copy link
Copy Markdown
Contributor

@roblg I've edited the title of the PR as prep for inclusion in the changlog. Please tweak as you see fit.

gigabo added a commit to gigabo/lerna that referenced this pull request Feb 1, 2017
This is necessary to make lerna#547 work when Lerna is installed locally.

Lerna is looking for the `rimraf` executable in its own `$(npm bin)`
directory, but npm 3+ hoists the executable to the _containing_ package's
`.bin` so `rimraf` winds up not being found.

This patch bundles `rimraf` _with_ Lerna so it's always where we expect it.
@rtsao rtsao mentioned this pull request Feb 1, 2017
gigabo added a commit to gigabo/lerna that referenced this pull request Feb 2, 2017
These noop rimrafs were cheap before lerna#547, but they're expensive now.
gigabo added a commit to gigabo/lerna that referenced this pull request Feb 2, 2017
These noop rimrafs were cheap before lerna#547, but they're expensive now.
hzoo pushed a commit that referenced this pull request Feb 3, 2017
These noop rimrafs were cheap before #547, but they're expensive now.
@lock

lock Bot commented Dec 27, 2018

Copy link
Copy Markdown

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock Bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants