3 ms·
It is a featureful formatting library, not simply a library for slow printing of ints and strings without any modifiers. You can't create a library which is ful
by vient 2y ago
It is a featureful formatting library, not simply a library for slow printing of ints and strings without any modifiers. You can't create a library which is full of features, fast, and small simultaneously.
- jstimpfle 2y agoYou'd hope the unused stuff gets stripped out but I don't know much about this topic so not going to argue.
- vlovich123 2y agoFfunction-sections and fdata-sections would need at a minimum to be used to strip dead code. But even with LTO it’s highly unlikely this could be trimmed unless all format strings are parsed at compile time because the compiler wouldn’t know that the code wouldn’t be asked to format a floating point number at some point. There could be other subtle things that hide it from the compiler as dead code. The surest bet would be a compile time feature flag to disable floating point formatting support which it does have. Still, that’s 8kib of string formatting library code without floating point and a bunch of other optimizations which is really heavy in a microcontroller context
- CoastalCoder 2y agoI think this is one scenario where C++ type-templated string formatters could shine. Especially if you extended them to indicate assumptions about the values at compile time. E.g., possible ranges for integers, whether or not a floating point value can have certain special values, etc.
- vlovich123 2y agoYou’d be surprised. I’m pretty sure std::format is templated. That doesn’t mean that it’s still easy to convince the compiler to delete that code.
- josephg 2y ago> it’s highly unlikely this could be trimmed unless all format strings are parsed at compile time They probably should be passed at compile time, like how zig does it. It seems so weird to me that in C & C++ something as simple as format strings are handled dynamically. Clang even parses format strings anyway, to look for mismatched arguments. It just - I suppose - doesn’t do anything with that.
- vlovich123 2y agoThat’s passed at compile time via template arguments and/or constexpr/consteval. Even still, there can be all sorts of reasons a compiler isn’t able to omit something as deeply integrated as floating point formatting from a generic floating point library. Rust handles this more elegantly with cargo features so that you could explicitly guarantee you’ve disabled floating point altogether in a generically reusable and intentional way (and whatever other features might take up space). It’s also important to note that the floating point code only contributed ~44kib out of 75kib but they stopped once the library got down to ~23kib and then removed the c++ runtime completely to shave off another ~10kib. However, it’s also equally important to remember that these shavings are interesting and completely useless: 1. In a typical codebase this would contribute 0% of overall size and not be important at all 2. A codebase where this would be important and you care about it (ie embedded) is not served well by this library eating up at least 10kib even after significant optimization as that 10kib that is intractible is still too large for this space when you’re working with a max ~128-256kib binary size (or even less sometimes).
- jstimpfle 2y agoThe usual case is that libc is linked dynamically so it's not a problem spending a few KB for the library. And run time format strings are a concise encoding for calling this functionality. I would assume that compile time alternatives take more space.
- jcelerier 2y agofmt support full compile-time processing of strings with FMT_COMPILE though: https://fmt.dev/latest/api/#format-string-compilation https://fmt.dev/latest/api/#format-string-compilation
- vitaut 2y agoIt is indeed possible to remove unused code with techniques like format string compilation but that's a topic for another post.