Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Disable V8/ICU whole-archive flag, improving file size #477

Merged
merged 1 commit into from
Apr 11, 2023

Conversation

fhanau
Copy link
Collaborator

@fhanau fhanau commented Mar 25, 2023

This patches V8 to not use the alwayslink parameter (corresponding to the --whole-archive linker flag on Linux) for several V8 and ICU-related libraries. This reduces binary sizes by ~1MB on Linux and ~19MB on macOS for opt binaries. macOS sees a much larger improvement since bazel uses --ffunction-sections and section garbage collection by default for opt Linux binaries it while we do not yet use the equivalent -dead_strip linker flag on macOS.
I will take a deeper look at V8's code base to confirm this is safe before converting to a non-draft PR.

@fhanau fhanau requested review from mikea and ohodson March 25, 2023 00:57
@fhanau fhanau force-pushed the felix/v8-no-alwayslink branch from 44e9c06 to 1029dab Compare March 25, 2023 01:13
@ohodson
Copy link
Contributor

ohodson commented Mar 25, 2023

The title of the change suggests removing a patch, but that's not in the current draft.

Copy link
Contributor

@ohodson ohodson left a comment

Choose a reason for hiding this comment

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

It would be good to have some data in the commit message, e.g. what is the saving?

Please sync the title with the PR (e.g. remove a patch or fix the title), thanks.

@fhanau
Copy link
Collaborator Author

fhanau commented Mar 27, 2023

@ohodson Sorry, I didn't realize GitHub notified reviewers right away on draft PRs. I got some numbers and will update the description later on, it can probably wait until #478 is merged though.

@kentonv
Copy link
Member

kentonv commented Mar 27, 2023

Do we know why these were marked alwayslink in the first place? Removing alwayslink can break things e.g. if the code depends on startup-time initializers to populate global registries. However, I thought the Chrome project was adverse to startup-time initializers in the first place so it'd be slightly surprising to me if V8 relies on them.

@fhanau
Copy link
Collaborator Author

fhanau commented Mar 27, 2023

Was going to look into that a bit more before turning it into a non-draft PR – tcmalloc uses alwayslink and probably depends on it, I believe in V8's case it's be more about preventing linking issues with shared libraries (which we don't use).

@fhanau fhanau changed the title Disable v8 alwayslink to improve file size, remove redundant patch Disable V8 and ICU alwayslink to improve file size Mar 27, 2023
@kentonv
Copy link
Member

kentonv commented Mar 27, 2023

We do use shared libraries in debug builds FWIW.

@fhanau fhanau force-pushed the felix/v8-no-alwayslink branch from 1029dab to b7fe084 Compare April 11, 2023 19:00
@fhanau fhanau marked this pull request as ready for review April 11, 2023 19:00
@fhanau
Copy link
Collaborator Author

fhanau commented Apr 11, 2023

Took a deep dive in V8's code base and this should be safe:

Within its primary build system GN, V8 only uses --Wl,-whole-archive when creating shared libraries (https://github.com/chromium/chromium/blob/main/build/toolchain/gcc_toolchain.gni#L507) – this is likely used so that all symbols from a static library added to a shared library are included, preventing possible compile errors due to missing symbols. It is not used when compiling binaries or static libraries, meaning that V8 does not rely on the flag being present to compile its binaries and static libraries. Bazel's alwayslink flag does more than that – if it is added to a cc_library, every target depending on it will import the library with whole-archive. V8 adds alwayslink to all the libraries it compiles, which results in whole-archive being present in more places than needed.
As far as I can see we currently link V8 statically, thus alwayslink won't be needed – if we were to link V8 dynamically in some capacity where we do need whole-archive to not omit symbols this would cause link errors.

Looking at the code itself, there are some instances where dlopen, dlsym, atexit, __attribute__((constructor)) and __attribute__((destructor)) are used, functions which can require whole-archive in some situations. However, these uses are harmless (i.e. using dlsym to load libc symbols, atexit referencing functions in the same file they are called in, destructors in ICU code not compiled in V8). V8 does use and depend on whole-archive when compiling some GTest tests, where test initializers would be omitted otherwise. Note that this change is V8 and ICU specific – tcmalloc depends on whole-archive and is unaffected.

@fhanau fhanau force-pushed the felix/v8-no-alwayslink branch from b7fe084 to df8c353 Compare April 11, 2023 19:07
@fhanau fhanau changed the title Disable V8 and ICU alwayslink to improve file size Disable V8/ICU whole-archive flag, improving file size Apr 11, 2023
@ohodson
Copy link
Contributor

ohodson commented Apr 11, 2023

Thanks Felix, nice write up.

It looks like for workerd we link all the v8 object files directly into the workerd binary (including debug), but this is not the same for ew (which I think Kenton was alluding to when he wrote "We do use shared libraries in debug builds FWIW.")

@fhanau
Copy link
Collaborator Author

fhanau commented Apr 11, 2023

Thank you!
I'll look at the internal build too, I focused on workerd first since binary size matters more within wrangler, although the size should be acceptable now.

@fhanau fhanau merged commit a4a045e into main Apr 11, 2023
@fhanau fhanau deleted the felix/v8-no-alwayslink branch April 11, 2023 20:50
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants