SpenceKonde/megaTinyCore

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

CoryParsnipson opened this issue · 1 comments

  • Using Arduino IDE 1.18.13, as recommended.
  • MegaTinyCore version 2.6.10.
  • Compiling to ATTiny402. (Also tried on ATTiny202, so this looks to happen independent of target chip.)

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!

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.