Hasty Briefsbeta

Shrinking While Linking

13 days ago
  • #rust
  • #static-libraries
  • #optimization
  • The author discusses the challenges of distributing a large Rust static library for Go developers, highlighting the significant size difference between static (132MB) and dynamic (15MB) libraries.
  • Explains the compilation process, emphasizing that static libraries contain redundant code and metadata, which are usually optimized away in final binaries but problematic for distribution.
  • Describes a method to reduce static library size by unpacking, relinking with `ld --relocatable --gc-sections`, and repacking, reducing size from 132MB to 107MB.
  • Further optimizations include removing LLVM bitcode (`.llvmbc`), stripping debug info, and merging sections, achieving a final size of 19MB (85% reduction).
  • Addresses MacOS compatibility issues, proposing an alternative approach using LLVM tools to achieve similar size reductions by working with intermediate bitcode.
  • Mentions 'dragonfire', a tool for deduplicating object files in static libraries, though it may not combine well with the merging approach described.
  • Concludes with tradeoffs between classic bintools (faster, broader compatibility) and LLVM tools (works on MacOS), opting for LLVM for cross-platform support in the Nickel library.