Honey, I shrunk {fmt}: bringing binary size to 14k and ditching the C++ runtime
The {fmt} library has reduced its binary size to 23kB by using type erasure, ensuring runtime type safety, and optimizing for memory-constrained devices, including retro computing systems.
Read original articleThe {fmt} formatting library has achieved a significant reduction in binary size, now reaching as low as 23kB, while eliminating the need for the C++ runtime. This reduction is primarily due to the library's innovative use of type erasure, which minimizes template bloat and confines template usage to a minimal top-level layer. The library's design allows for efficient formatting without sacrificing runtime type safety, catching format string errors at compile time and managing runtime errors through exceptions. Recent optimizations include disabling locale support and utilizing an extension API for formatting arbitrary types, which further reduces the binary size. The library's performance remains robust, particularly in scenarios involving positional arguments. The latest version of {fmt} has also integrated the Dragonbox algorithm for floating-point formatting, enhancing its capabilities. The ongoing interest in using {fmt} for memory-constrained devices, including retro computing systems, underscores its versatility and efficiency. Overall, the library's development reflects a commitment to optimizing both size and performance, making it a suitable choice for various applications.
- The {fmt} library's binary size has been reduced to as low as 23kB.
- Type erasure is used to minimize template bloat and improve efficiency.
- The library maintains runtime type safety while allowing for compile-time error checking.
- Recent optimizations include disabling locale support and using an extension API for arbitrary types.
- The library is increasingly popular for use in memory-constrained environments, including retro computing.
Related
Some Tricks from the Scrapscript Compiler
The Scrapscript compiler implements optimization tricks like immediate objects, small strings, and variants for better performance. It introduces immediate variants and const heap to enhance efficiency without complexity, seeking suggestions for future improvements.
#F – A Minimalistic Scheme System
#F is a minimalistic Scheme system that compiles to portable C code for integration with C projects. It is not standard-compliant and has performance limitations, with installation requiring C file compilation.
Another variable-length integer encoding
The article presents two encoding schemes for small integers in binary formats: metric varint and imperial varint, highlighting their efficiency, advantages, and the use of zig-zag encoding for signed integers.
Small Strings in Rust: smolstr vs. smartstring
The article explores Rust's small string libraries `smolstr` and `smartstring`, demonstrating JSON parsing, a custom memory allocator, and a reporting subcommand for analyzing memory usage and allocations.
Honey, I shrunk {fmt}: bringing binary size to 14k and ditching the C++ runtime
The {fmt} library has reduced its binary size to 23kB by using type erasure, maintaining runtime type safety, and optimizing for memory-constrained environments, including retro computing applications.
- Several users express surprise at the complexity and size of floating-point formatting code.
- There are discussions about the efficiency of different memory allocation strategies in C++.
- Some commenters share alternative solutions for minimal binary sizes, including references to other libraries.
- Users highlight the importance of optimizing for memory-constrained environments, such as microcontrollers.
- There is a humorous acknowledgment of the challenges faced in numeric formatting across different programming languages.
Chuckles
The linked dragonbox [1] project is also worth a read. Pretty optimized for the least used branches.
C++ noob here, but is libc++'s default allocator (I mean, the default implementation of new and delete) actually doing something different than calling libc's malloc and free under the hood? If so, why?
strings are ~4 instructions (test for null terminator, output character, branch back two).
Ints are ~20 instructions. Check if negative and if so output '-' and invert. Put 1000000000 into R1. divide input by R1, saving remainder. add ASCII '0' to result. Output character. Divide R1 by 10. put remainder into input. Loop unless R1=0.
Floats aren't used by many programs so shouldn't be compiled unless needed. Same with hex and pointers and leading zeros etc.
I can assure you that when writing code for microcontrollers with 2 kilobytes of code space, we don't include a 14 kilobyte string formatting library...
Please note that a direct comparison would be apples-to-oranges though.
Interesting, I've never done this test!
Related
Some Tricks from the Scrapscript Compiler
The Scrapscript compiler implements optimization tricks like immediate objects, small strings, and variants for better performance. It introduces immediate variants and const heap to enhance efficiency without complexity, seeking suggestions for future improvements.
#F – A Minimalistic Scheme System
#F is a minimalistic Scheme system that compiles to portable C code for integration with C projects. It is not standard-compliant and has performance limitations, with installation requiring C file compilation.
Another variable-length integer encoding
The article presents two encoding schemes for small integers in binary formats: metric varint and imperial varint, highlighting their efficiency, advantages, and the use of zig-zag encoding for signed integers.
Small Strings in Rust: smolstr vs. smartstring
The article explores Rust's small string libraries `smolstr` and `smartstring`, demonstrating JSON parsing, a custom memory allocator, and a reporting subcommand for analyzing memory usage and allocations.
Honey, I shrunk {fmt}: bringing binary size to 14k and ditching the C++ runtime
The {fmt} library has reduced its binary size to 23kB by using type erasure, maintaining runtime type safety, and optimizing for memory-constrained environments, including retro computing applications.