SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
542 stars 140 forks source link

Minimal printf() takes up more space than default printf() #1058

Closed CoryParsnipson closed 3 months ago

CoryParsnipson commented 4 months ago

You can select the implementation of printf() in the board options of megaTinyCore underneath the "Tools" menu. It appears that if I select "Minimal" for printf, it actually takes up more program space than the "Default" for printf. Judging from the descriptions on both options, it sounds like the opposite is intended.

printf

When compiling a certain program with "Default" printf():

Sketch uses 1906 bytes (46%) of program storage space. Maximum is 4096 bytes.

When compiling the same program with "Minimal" printf():

Sketch uses 2878 bytes (70%) of program storage space. Maximum is 4096 bytes.

Thank you!

SpenceKonde commented 3 months ago

Please refer to the documentation regarding the printf mode option. I clearly warn that if any option other than the default option is selected, AND vfprint (or whatever the central piece of machinery is there) is NOT actually being used, it will use more space if you select an option from that submenu, because that forces the linker to link in the selected version of vfprint regardless of whether it is ever called.

Thus: printf not used, menu on default = baseline binary size. fprintf not called, so not included in the binary. printf not used, menu on minimal = Larger than baseline, because in order to the the different printf behavior, the only approach is to tell the linker that it must include the desired version of the binary for vfprint. So it does - if it's never called, that's a waste of space.

As soon as your code uses printf in some way (cause they all call into that one vfprint or whatever function to do printf'y things), the default will take more flash than minimal: The one with default set will pull in the usual version of vfprint, and the one with minimal set will have already been forced to use that vfprint for everything, so neither has an unnecessary chunk of code in it.