Skip to content

Use v0 symbol mangling for Rust libraries - #98446

Merged
alexey-milovidov merged 7 commits into
masterfrom
use-rust-v0-symbol-mangling
Mar 3, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
use-rust-v0-symbol-mangling

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Two changes to reduce Rust symbol bloat in the ClickHouse binary, particularly from the PRQL library:

  1. v0 symbol mangling (-C symbol-mangling-version=v0 in RUSTFLAGS): Uses Rust's newer mangling scheme with backreference compression for repeated type paths. Reduces the longest symbol from 165 KB to 2.3 KB (72x reduction) and eliminates all symbols over 10 KB.

  2. Post-build symbol stripping for PRQL: After cargo builds the PRQL static library, a post-build step partial-links all .o files into one, localizes all symbols except prql_to_sql and prql_free_pointer, and strips unneeded symbols. This reduces the library from 69 MB to 51 MB and leaves only 2 visible global symbols (down from ~25K).

Metric Before After v0 mangling After strip
Library size 66 MB 69 MB 51 MB
Longest symbol 165 KB 2.3 KB 2.3 KB
Symbols >= 10 KB 25 0 0
Global defined symbols ~25K ~25K 2

Motivated by https://clickhouse.com/blog/rust

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Use Rust v0 symbol mangling and strip internal symbols from PRQL library to reduce symbol name bloat from parser combinator libraries.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.3.1.332

The `chumsky` parser combinator library (used by `prqlc`) creates deeply
nested generic types. With Rust's legacy symbol mangling, these produce
extremely long symbol names — the longest was 165 KB for a single symbol.
25 symbols exceeded 10 KB, and total symbol name bytes in the PRQL static
library were 6 MB across 67K symbols.

Rust v0 mangling uses backreference compression for repeated type paths.
Since the giant symbols consist almost entirely of repeated `chumsky::*`
type paths (e.g. `chumsky::input::MappedSpan` appeared 412 times in the
worst case), v0 mangling reduces the longest symbol from 165 KB to 2.3 KB
— a 72x reduction. All symbols over 10 KB are eliminated.

This is motivated by https://clickhouse.com/blog/rust

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [81d3b37]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added pr-build Pull request with build/testing/packaging improvement submodule changed At least one submodule changed in this PR. labels Mar 2, 2026
alexey-milovidov and others added 3 commits March 2, 2026 04:45
After cargo builds the PRQL library, run a post-build step that:
1. Extracts .o files from the .a archive
2. Partial-links them into a single .o (resolving all internal references)
3. Localizes all symbols except `prql_to_sql` and `prql_free_pointer`
4. Strips symbols not needed for relocations
5. Repackages as .a

This reduces the library from 69 MB to 51 MB and leaves only 2 visible
global symbols. The ~67K internal symbols (including deeply nested chumsky
parser combinator types) are eliminated from the symbol table.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The tidy build uses a dummy `CMAKE_AR` script that doesn't actually
extract archive members. Skip the strip step when `CMAKE_AR` matches
`dummy_compiler_linker`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sanitizer instrumentation (ASan, TSan, MSan) adds special symbols like
`___asan_globals_registered` that are referenced by relocations in the
instrumented code. The partial link + strip process breaks these symbols,
causing linker errors like:
  ld.lld: error: relocation R_X86_64_PC32 cannot refer to absolute symbol: ___asan_globals_registered

Skip the stripping step entirely when SANITIZE is set, since sanitizer
builds are for development/testing and don't need this optimization.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
alexey-milovidov and others added 3 commits March 2, 2026 16:55
The Darwin builds failed because the strip script used `CMAKE_LINKER`
(`/usr/bin/ld` — GNU ld) which cannot handle Mach-O object files.
The same issue could affect other cross-compilation targets (ppc64le,
riscv64, s390x, loongarch64) where the host `ld` doesn't support the
target ELF architecture.

Two fixes:
- Skip stripping on Darwin where objects are Mach-O (`ld.lld -r` only
  handles ELF)
- Use `ld.lld` explicitly instead of `CMAKE_LINKER` — it's a
  cross-linker that handles all ELF architectures regardless of host

Co-Authored-By: Claude Opus 4.6 <[email protected]>
`ld.lld -r` partial linking produces broken `.eh_frame` relocations on
ppc64le, causing linker errors like "relocation is not in any piece"
during the final link.

https://s3.amazonaws.com/clickhouse-test-reports/PRs/98446/f5e77a7baf79573b248cb58abf6d1e6363ebb21e/build_ppc64le/build_clickhouse/build_clickhouse.log

Co-Authored-By: Claude Opus 4.6 <[email protected]>
# The legacy mangling produces extremely long symbols (up to 165 KB) from deeply nested
# generic types in parser combinator libraries like chumsky (used by prqlc).
# v0 mangling uses backreference compression for repeated type paths.
list(APPEND RUSTFLAGS "-C" "symbol-mangling-version=v0")

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.

@alexey-milovidov
alexey-milovidov merged commit 34c6d70 into master Mar 3, 2026
145 checks passed
@alexey-milovidov
alexey-milovidov deleted the use-rust-v0-symbol-mangling branch March 3, 2026 15:09
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants