Skip to content

Conversation

mrousavy
Copy link
Contributor

@mrousavy mrousavy commented Jul 8, 2025

Summary:

Enables Link Time Optimization for performance critical libraries to optimize the native code across modules.

This might increase release build-time a tiny bit (not yet measured), but should improve runtime performance or startup performance a bit (60ms in the Discord app, not yet measured in other apps)

Comparisons

Native Libraries Size

  • Without -flto (current main): 19,4 MB for rn-tester apk (assembleRelease, arm64-v8a)
  • With -flto (current PR): 19,4 MB for rn-tester apk (assembleRelease, arm64-v8a)

..so no difference.

Release apk build time

  • Without -flto (current main): 8:40.24
  • With -flto (current PR): 10:19.57

...so 1min 20s increase in build time 😢

Release apk performance

Without -flto (current main) With -flto (current PR)

I'm not sure how accurate the flashlight measurements are, but according to the average of 10 tests in this CI, enabling -flto makes the app start ~70ms (or ~10%) faster... 👀

Release apk performance

  • Without -flto (current main): ...
  • With -flto (current PR): ...

Changelog:

[GENERAL] [ADDED] - Enabled link-time-optimization to improve performance of some parts of the C++ codebase

Test Plan:

  1. Build once with, and once without -flto (release builds!)
  2. Measure build time difference between clean builds
  3. Measure startup time difference
  4. Measure perceived performance when stress-testing something (maybe the Turbo Modules vs Nitro Modules benchmark got a bit tighter with this)

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 8, 2025
@mrousavy mrousavy marked this pull request as ready for review July 8, 2025 18:37
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 8, 2025
@NickGerleman
Copy link
Contributor

I think @cortinico had tried to enable this before, globally in the build, by setting INTERPROCEDURAL_OPTIMIZATION to true?

Are there places where this is not getting set? Or is this doing something like setting flto=thin maybe? Some of these should definitely already have some form of LTO enabled.

Comment on lines +252 to +253
# Enable aggressive compiler optimizations and link-time-optimizations
target_compile_options(reactnative PRIVATE -O3 -flto)
Copy link
Contributor

Choose a reason for hiding this comment

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

So in theory this should not be needed, because the Android NDK toolchain already should be setting those flags for us.

Specifically the release versions of React Native are already built with:

LANGUAGE_COMPILE_FLAGS = -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D__BIONIC_NO_PAGE_SIZE_MACRO -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O3 -DNDEBUG  -flto=thin

Copy link
Contributor

Choose a reason for hiding this comment

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

On top of this, this would set the compilation flag for both Debug and Release, which is something we don't really want

@cortinico
Copy link
Contributor

I think @cortinico had tried to enable this before, globally in the build, by setting INTERPROCEDURAL_OPTIMIZATION to true?

Are there places where this is not getting set? Or is this doing something like setting flto=thin maybe? Some of these should definitely already have some form of LTO enabled.

Yeah see my other comment. We already set -flto=thin on all the targets

@NickGerleman
Copy link
Contributor

@mrousavy how reproducible are the wins from using -flto instead of -flto=thin?

If there is a good win there, I could imagine it making sense to enable for anything prebuilt, since end-users won't often hit the build time impact. But we would want to reconcile the different ways this is enabled.

@mrousavy
Copy link
Contributor Author

let me get back to you on this after some more testing. :)

@cortinico
Copy link
Contributor

let me get back to you on this after some more testing. :)

Eager to hear your findings @mrousavy :)

@Palloxin

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants