Monomorphized Generics vs Concrete Type Specialization for Binary Size
0 reputation · 26 Jul 2023, 20:41 UTC
Managing Binary Bloat in Large-Scale Crystal Projects
Crystal utilizes monomorphization to implement generics, generating specialized machine code for every concrete type used at compile time. While this approach eliminates runtime boxing and optimizes performance to levels comparable with C++, it introduces a trade-off regarding the final executable size.
In projects with deeply nested generic structures or a high volume of unique type instantiations, the resulting "code explosion" can significantly increase binary size and extend compilation times. This creates a design tension between maintaining a flexible, generic API and minimizing the memory footprint of the compiled artifact.
When scaling a codebase, it is unclear at what threshold the performance gains of monomorphized generics are outweighed by the overhead of binary bloat.
- Does the Crystal compiler provide mechanisms to limit specialization for infrequently used types?
- What are the primary indicators that a project should transition from generic polymorphism to concrete type definitions to optimize binary size?