TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Honey, I shrunk {fmt}: bringing binary size to 14k and ditching the C++ runtime

244 pointsby karagenit9 months ago

9 comments

magnio9 months ago
&gt; All the formatting in {fmt} is locale-independent by default (which breaks with the C++’s tradition of having wrong defaults)<p><i>Chuckles</i>
评论 #41415607 未加载
h4ck_th3_pl4n3t9 months ago
It&#x27;s kind of mindblowing to see how much code floating point formatting needs.<p>The linked dragonbox [1] project is also worth a read. Pretty optimized for the least used branches.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;jk-jeon&#x2F;dragonbox">https:&#x2F;&#x2F;github.com&#x2F;jk-jeon&#x2F;dragonbox</a>
评论 #41418960 未加载
评论 #41419113 未加载
评论 #41416357 未加载
评论 #41417015 未加载
评论 #41422807 未加载
pzmarzly9 months ago
&gt; However, since it may be used elsewhere, a better solution is to replace the default allocator with one that uses malloc and free instead of new and delete.<p>C++ noob here, but is libc++&#x27;s default allocator (I mean, the default implementation of new and delete) actually doing something different than calling libc&#x27;s malloc and free under the hood? If so, why?
评论 #41417717 未加载
评论 #41416039 未加载
评论 #41415681 未加载
评论 #41420453 未加载
londons_explore9 months ago
I kinda hoped a formatting library designed to be small and able to print strings, and ints ought to be ~50 bytes...<p>strings are ~4 instructions (test for null terminator, output character, branch back two).<p>Ints are ~20 instructions. Check if negative and if so output &#x27;-&#x27; and invert. Put 1000000000 into R1. divide input by R1, saving remainder. add ASCII &#x27;0&#x27; to result. Output character. Divide R1 by 10. put remainder into input. Loop unless R1=0.<p>Floats aren&#x27;t used by many programs so shouldn&#x27;t be compiled unless needed. Same with hex and pointers and leading zeros etc.<p>I can assure you that when writing code for microcontrollers with 2 kilobytes of code space, we don&#x27;t include a 14 kilobyte string formatting library...
评论 #41415665 未加载
评论 #41417807 未加载
评论 #41418206 未加载
评论 #41416206 未加载
评论 #41415698 未加载
评论 #41417879 未加载
评论 #41421238 未加载
评论 #41416851 未加载
评论 #41416121 未加载
评论 #41415729 未加载
评论 #41415673 未加载
评论 #41425043 未加载
评论 #41415895 未加载
评论 #41420775 未加载
ptspts9 months ago
Shameless plug: printf(Hello, World!\n&quot;); is possible with an executable size of 1008 bytes, including libc with output buffering: <a href="https:&#x2F;&#x2F;github.com&#x2F;pts&#x2F;minilibc686">https:&#x2F;&#x2F;github.com&#x2F;pts&#x2F;minilibc686</a><p>Please note that a direct comparison would be apples-to-oranges though.
评论 #41420417 未加载
a1o9 months ago
&gt; Considering that a C program with an empty main function is 6kB on this system, {fmt} now adds less than 10kB to the binary.<p>Interesting, I&#x27;ve never done this test!
评论 #41416433 未加载
neonsunset9 months ago
It&#x27;s always fmt. Incredibly funny that <i>this exact problem</i> now happens in .NET. If you touch enough numeric (esp. fp and decimal) formatting&#x2F;parsing bits, linker ends up rooting a lot of floating point and BigInt related code, bloating binary size.
评论 #41416050 未加载
msephton9 months ago
Very enjoyable. I love these sort of thinking outside the box optimisations.
rty329 months ago
Maybe I am slow, it took me a while to realize the &quot;14k&quot; in the title refers to &quot;14kB&quot;
评论 #41416509 未加载