Skip to content

Optimize desktop build and SDK packaging time - #2004

Open
a-maurice wants to merge 7 commits into
mainfrom
am-packaging_improvements
Open

a-maurice wants to merge 7 commits into
mainfrom
am-packaging_improvements

Conversation

@a-maurice

Copy link
Copy Markdown
Contributor

Description

Provide details of the change, and generalize the change in the PR title above.

This PR optimizes the slowest bottlenecks in cpp-packaging.yml, reducing end-to-end packaging time from ~4h 01m (241 min) down to ~1h 32m–1h 41m (92–101 min) (~60% faster).

Stage / Job Before (42983930100) After (46997570570) Delta
Slowest Windows build 49.5 min 34.4 min −31%
package-windows-x64-Debug-dynamic 152.9 min 29.7 min −81%
package-windows-x64-Debug-static 137.4 min 37.9 min −72%
package-windows-x86-Debug-* 70.3m – 85.2m 10.9m – 17.5m −80%
package-windows-*-Release-* 11.2m – 29.3m 5.0m – 7.0m −50% to −76%
package-linux 18.9 min 7.0 min −63%
Final merge + Windows-only zip 37.8 min 15.8 min −58%
Total End-to-End 241.3 min 91.9 min −62%

Changes

1. Parallel Windows compilation for external dependencies (scripts/gha/build_desktop.py)

  • Set CL=/MP on Windows before cmake --build. Previously -j only parallelized across .vcxproj projects in MSBuild, leaving large external projects (grpc.vcxproj, firestore_core.vcxproj) compiling single-threaded on 1 CPU core at the end of the build.
  • Cut grpc.vcxproj from ~25.7m to ~13.7m and firestore_core.vcxproj from ~5.7m to ~2.7m.

2. Faster symbol hiding & archive processing (scripts/merge_libraries.py, build_scripts/desktop/package.sh, .github/workflows/cpp-packaging.yml)

  • Single-pass archive member renaming: replace_strings_in_archive() previously ran a separate re.sub() pass over the entire archive for each member whose name contained path separators (819 full passes over the ~1 GB Windows Debug grpc.lib, taking ~40–45m). Replaced with a single-pass trie-regex replacement (~26–29s).
  • Batched duplicate extraction: extract_archive() now extracts archives with duplicate member names in rounds (one ar xN <n> call per duplicate level) instead of one ar invocation per object.
  • Parallel llvm-objcopy & per-archive rename filtering:
    • Parallelized per-object objcopy calls across CPUs (--jobs).
    • Filtered --redefine-syms per input archive to only symbols present in that archive (--filter_renames_per_archive).
    • Added package.sh -O to use llvm-objcopy (hash-table symbol lookup) alongside GNU nm/ar on Linux and Windows, instead of GNU binutils-2.35.1 objcopy (linear linked-list lookup).
    • Stripped .drectve alongside .pdata on Windows (llvm-objcopy preserves IMAGE_SCN_LNK_INFO on .drectve, unlike GNU objcopy, which otherwise causes MSVC link.exe to fail with LNK2001 on pre-rename /INCLUDE: and /EXPORT: directives).
  • Shared namespace & C++ rename cache: Stored auto-detected namespaces and C++ symbol renames in --cache so the initial firebase_analytics warm-up computes them once and the remaining 11 parallel product merges reuse them (cutting Windows x64 Debug Firestore compute C++ renames from ~4–6m to ~10s).
  • Phase-level timing logs: Added --log_timing (INFO level, enabled by default) to print per-phase timings and slow input archives.

3. Parallel multithreaded final SDK zipping (build_scripts/other/zip_package.sh, .github/workflows/cpp-packaging.yml)

  • Replaced single-threaded zip -9 with multithreaded 7-Zip (-mx=6 -mmt=on -snl, verifying file/symlink counts with automatic fallback to Info-ZIP).
  • Folded the sequential create-windows-only-package job into final-merge-packages, staging the Windows-only tree via hard links (cp -al) and zipping both archives in parallel.
  • Set compression-level: 0 on actions/upload-artifact for the already-compressed .zip outputs.
  • Note: At compression level 6 (vs. -9), final zip sizes are ~3–6% larger (~1893 MB vs. ~1837 MB full; ~1008 MB vs. ~953 MB Windows-only).

Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.


Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

Notes

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.

merge_libraries.py:
- Run objcopy on each archive's object files in parallel (--jobs, default
  = number of CPUs). Previously every .obj was processed serially, which left
  firebase_firestore.lib (Windows Debug) merging on a single core for ~90 min.
- Only pass objcopy the symbol renames that actually appear in each input
  archive (--filter_renames_per_archive, default on). objcopy's cost scales
  with the size of the redefinition list.
- Support llvm-objcopy alongside GNU nm/ar (--binutils_objcopy_is_llvm,
  autodetected): don't pass GNU BFD target names to llvm-objcopy.

package.sh: add -O to use llvm-objcopy (from the tools dir) with GNU nm/ar.

cpp-packaging.yml:
- Linux and Windows packaging use the runner's llvm-objcopy via -O. GNU
  objcopy does a linear lookup per symbol in the redefinition list.
- Build firebase_cpp_sdk.zip and firebase_cpp_sdk_windows.zip in parallel in
  the merge job using multithreaded 7-Zip (new zip_package.sh, which verifies
  file/symlink counts and falls back to Info-ZIP), replacing single-threaded
  "zip -9" and the separate sequential create-windows-only-package job.
- Don't recompress the already-compressed zips when uploading artifacts.

build_desktop.py: on Windows, set CL=/MP so that external projects (gRPC,
Abseil, Protobuf, Firestore) compile their sources in parallel. Previously
grpc.vcxproj and firestore_core.vcxproj compiled on a single core at the end
of every Windows build.
Windows Debug packaging still spends 40-50 minutes merging
firebase_firestore.lib after all other libraries are done, but the logs
don't show where that time goes. Log (at INFO level, enabled by default via
--log_timing) how long each phase takes: loading the cache, scanning C and
C++ symbols, finding namespaces, computing renames, filtering renames,
extracting archives, objcopy, creating the output archive, and saving the
cache, along with symbol/demangle cache misses and counts. Also log each
input archive that takes longer than --log_timing_archive_threshold
seconds (default 10). Every line is tagged with the output library name,
since package.sh runs merges in parallel.
Extraction: when an archive contains duplicate object filenames (e.g.
grpc.lib), extract_archive() ran "ar xN <count> <archive> <member>" once per
object, and each invocation reads through the entire archive. For the
Windows x64 Debug grpc.lib (819 objects) this took ~45-50 minutes. Instead,
extract in rounds, one "ar xN <n>" invocation per duplicate level with all
the applicable member names at once. Output filenames are unchanged.

Caching: every product's merge in package.sh scans the same libraries, so
the auto-detected namespaces and the C++ symbol renames (938k symbols x 668
namespaces for Windows x64 Debug, 4-6 minutes per merge) are identical each
time. Store both in the shared --cache so they're computed once, during the
initial "populate cache" run, and reused by the parallel merges. Cache keys
include the platform, scanned libraries, namespaces and rename settings.
The previous commit's batched extraction didn't help: the slow part of
"extract archives" for the Windows Debug grpc.lib (~40 minutes) is actually
replace_strings_in_archive(). MSVC archive member names contain directory
separators, so every member name (819 of them for grpc.lib) gets a binary
replacement over the entire archive, and it ran a separate re.sub() pass
over the whole (very large) file for each name.

Do all the replacements in a single pass instead, using one regex built as
a trie of the patterns (so shared prefixes don't make the regex engine try
every alternative at every position), and look up each match's replacement.
The output is byte-for-byte identical to the old implementation.

Also time member renaming separately ("rename members") from extraction.
Switching Windows packaging to llvm-objcopy caused LNK2001 unresolved
external symbol errors in Windows Debug integration tests (app_check,
database, and firestore) for pre-rename symbols such as
p_thread_callback_boringssl, uWS::*, grpc_core::NoDestructSingleton<...>,
and grpc_core::arena_detail::ArenaContextTraits<...>.

MSVC embeds /INCLUDE: and /EXPORT: directives referencing pre-rename
symbol names in the COFF .drectve section. GNU objcopy inadvertently
cleared IMAGE_SCN_LNK_INFO on .drectve so MSVC link.exe ignored it,
whereas llvm-objcopy preserves IMAGE_SCN_LNK_INFO, causing link.exe to
process the stale directives.

Strip .drectve alongside .pdata when running objcopy on Windows.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces several optimizations to speed up the build and packaging processes. Key changes include adding support for llvm-objcopy, parallelizing MSBuild compilation on Windows, implementing a faster multithreaded zipping script using 7-Zip, and significantly optimizing merge_libraries.py through parallel object file processing, trie-based regex replacements, caching of namespaces/renames, and per-archive rename filtering. A performance optimization was suggested in scripts/merge_libraries.py to iterate over the smaller archive_symbols set instead of the entire rename_symbols dictionary when filtering renames per archive.

Comment thread scripts/merge_libraries.py Resolved
a-maurice and others added 2 commits December 3, 2026 16:15
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@a-maurice a-maurice changed the title Am packaging improvements Optimize desktop build and SDK packaging time Dec 4, 2026
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.

1 participant