Skip to content

Speed up library builds by building many objects in one emcc invocation - #8795

Closed
quantum5 wants to merge 12 commits into
emscripten-core:mainfrom
quantum5:fast-lib-build
Closed

quantum5 wants to merge 12 commits into
emscripten-core:mainfrom
quantum5:fast-lib-build

Conversation

@quantum5

@quantum5 quantum5 commented Jun 13, 2019 •

Copy link
Copy Markdown
Member
  • Compile many object files in one call to emcc.
  • emcc: use a thread pool to build all arguments in parallel.

We have to use a thread pool because the function depends on a lot of unpicklable internal state.

This appears to build libc 3 times faster than before.

@kripken

kripken commented Jun 13, 2019

Copy link
Copy Markdown
Member

Interesting!

Btw, I think you can make this PR be intended to be merged into the PR it depends on (instead of incoming). Then the diff here is more meaningful (doesn't contain the other PR). And then after the other PR lands you can just change the branch target here (which may be simpler than what you'd need to do otherwise to disentangle it from the already-landed changes).

@quantum5

Copy link
Copy Markdown
Member Author

Diff can be found at quantum5#1.

@kripken

kripken commented Jun 13, 2019

Copy link
Copy Markdown
Member

I see, thanks!

Ok, so this makes emcc a.c b.c c.c compile those using a thread pool, so it makes any such invocation faster, and also it makes system libs build with a single such emcc invocation instead of one per source file?

I love this, but I do worry a little about user-noticeable side effects, in particular around error logging. If more than one of those files has an error, the logging would get mixed up in a racy way, it seems? That's probably a rare case. Maybe to reduce the risk, if more than one invocation returns an error code, we could log a warning to use build source files separately or to use EMCC_CORES=1 or EMCC_DEBUG=1 to avoid racing?

@quantum5 quantum5 changed the title Speed up library builds by building many objects on one emcc invocation Speed up library builds by building many objects in one emcc invocation Jun 13, 2019
@quantum5

Copy link
Copy Markdown
Member Author

Changed it so that LLVM output is buffered and then output in order.

Coloured output is preserved by passing -fcolor-diagnostics to LLVM if sys.stderr.isatty().

@quantum5
quantum5 marked this pull request as ready for review June 13, 2019 22:29
@kripken

kripken commented Jun 13, 2019

Copy link
Copy Markdown
Member

Sounds good!

Would be good to add a test of that in other, something like compiling 4 files with warnings, and seeing that the warnings are always in the same order, and that they have color (I hope that's possible? maybe hard to do in general, but testing that part only on linux would be good).

@quantum5

Copy link
Copy Markdown
Member Author

Added a test to make sure multiple processes don't print at once, and added a test to run emcc on a fake terminal to see if it actually generates the ANSI colour escapes.

@quantum5

Copy link
Copy Markdown
Member Author

Seems like when compiling many large C++ files, this is not necessarily faster than before in terms of wall clock time, while less user/system time is used. For now, this is on hold.

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We decided to table this change because there was only a benefit when building libraries composed of many tiny source files, and performs slightly worse for other libraries composed of large files. If we maintained both code paths we could save roughly three minutes per run of the emscripten CI, but we are not convinced that this is worth the additional complexity.

@kripken

kripken commented Jun 18, 2019

Copy link
Copy Markdown
Member

I think a 3x speedup in libc building is worth looking more into. Yeah, maybe this PR's approach is too risky, but maybe the system library code could have an option to auto-unify source files (it should be ok to generate a smaller number of larger .o files for libc, if the files don't contain things like static constructors).

@sbc100

sbc100 commented Jun 18, 2019

Copy link
Copy Markdown
Collaborator

I'd be interested in seeing a rebased version of this if its easy to produce? IIUC much of this landed as part of the the system library refactor?

@quantum5

Copy link
Copy Markdown
Member Author

@sbc100 rebased onto incoming.

@kripken

kripken commented Jun 18, 2019

Copy link
Copy Markdown
Member

@quantum5 did you have a chance to measure this on windows? hopefully the speedup is even bigger there. (We just got another bug report on the mailing list about the slowness there, we should really fix it.)

@sbc100

sbc100 commented Jun 18, 2019

Copy link
Copy Markdown
Collaborator

I also wonder if emcc could simply construct a single clang command line, and have clang take care of the parallelism internally? Presumably clang does this?

@quantum5

Copy link
Copy Markdown
Member Author

On Windows, this appears to build libc 4 times faster.

@sbc100

sbc100 commented Jun 18, 2019

Copy link
Copy Markdown
Collaborator

The IIUC this is not because we are increasing parallelism but because we are running N * clang.exe rather than N * emcc.py? So the overhead is the python startup time and the python code than runs before emcc forks clang?

@kripken

kripken commented Jun 19, 2019

Copy link
Copy Markdown
Member

The IIUC this is not because we are increasing parallelism but because we are running N * clang.exe rather than N * emcc.py? So the overhead is the python startup time and the python code than runs before emcc forks clang?

Yes. On libc that is very noticeable since musl has many many tiny C files. Loading a python VM (even with no code) is noticeably a slowdown on such a file.

Another option here is to make the library building code "unify" those tiny files into bigger ones. That should be fine for files with no static constructors etc., although it could make linking a little bit slower.

@sbc100

sbc100 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

I'm going back and forth on this. It could make debugging a little a harder since you'd end up with one giant failing build command in the case that something went wrong (it would be thousands of chars long presumably?). On the other hand we don't often debug issues with system libraries.

Also, most users should be using emsdk so won't be building system libraries themselves.

Also, what about the case of "foo/bar.c" + "baz/bar.c" won't they clobber each other?

@quantum5

Copy link
Copy Markdown
Member Author

On Windows, this also appears to be slightly slower for building large C++ libraries.

@floooh

floooh commented Jun 19, 2019 •

Copy link
Copy Markdown
Collaborator

...sorry for the drive-by comment, but attempting to build musl as a unity build sounds like the most simple option to me, IF it is guaranteed that linking with the resulting obj-file drops unused functions even if LTO is not used (aka function level linking).

...also: what about the other idea that was floating around to offer prebuilt packages for the things that are built under the hood on first emcc invocation? (so instead of compiling, download precompiled libs, maybe move the whole "magic" into emsdk too).

@sbc100

sbc100 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

Yes, most users should get prebuilt libraries since they use emsdk.

Even for users who do not use emsdk it s one-time cost and the results are caches, so build speed is not a huge issue. I think attempting unity build would be overkill for these reasons.

kripken added a commit to WebAssembly/waterfall that referenced this pull request Jun 21, 2019
So far just libc, to start with.

See emscripten-core/emscripten#8795 for background discussion
@sbc100 sbc100 closed this Jan 30, 2020
@sbc100

sbc100 commented Jan 30, 2020

Copy link
Copy Markdown
Collaborator

The deletion of the incoming branch triggered the automatic closing of this branch. My apologies. This was an unexpected side effect.

Re-opening and re-targeting to master.

@sbc100 sbc100 reopened this Jan 30, 2020
@sbc100
sbc100 changed the base branch from incoming to master January 30, 2020 19:49
@stale

stale Bot commented Jan 31, 2021

Copy link
Copy Markdown

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale Bot added the wontfix label Jan 31, 2021
Base automatically changed from master to main March 8, 2021 23:49
@stale stale Bot removed the wontfix label Mar 8, 2021
@stale

stale Bot commented Apr 16, 2022

Copy link
Copy Markdown

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale Bot added the wontfix label Apr 16, 2022
@tlively

tlively commented Apr 18, 2022

Copy link
Copy Markdown
Member

I wonder if we can use bare clang to compile (but not link) the system library sources these days to remove the Python startup overhead.

@stale stale Bot removed the wontfix label Apr 18, 2022
@sbc100

sbc100 commented Apr 18, 2022

Copy link
Copy Markdown
Collaborator

I wonder if we can use bare clang to compile (but not link) the system library sources these days to remove the Python startup overhead.

I like this idea

@sbc100 sbc100 closed this Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants