Eliminate code bloat with SD.h - how?

h4yn0nnym0u5e

Well-known member
This could be the shortest piece of code posted under the Forum Rule ever:
Code:
#include "Audio.h"
//#include "SD.h"
void setup() {}
void loop() {}
I could have made it shorter, but that would have been silly. And it really is the complete source code.

So. I've compiled the above for Teensy 3.5, with various USB types and the #include lines commented out or not, depending. Arduino 1.8.15, Teensyduino 1.56. Code and global variable sizes come out as follows.

#includesUSBCodevariablesComment
NoneSerial70323020
None Serial+MIDI+Audio135686472
SD.hSerial+MIDI+Audio484407916
Audio.h Serial+MIDI+Audio504249004dynamic audio
Audio.hSerial+MIDI+Audio505529004TD1.56 audio
The problem is the code size bloat which adds 34k when SD.h is just mentioned. I don't even have to use it (as you see - I'm not using anything...), but it seems to result in pulling in SDFile, SDClass, SPIClass, ExFat, Fat, SdSpiCard, SdioCard, etc. etc., and the link optimisation for some reason isn't eliminating them right back out when it finds they're not used. You can't say "just don't include it when you're not using it", because Audio.h has a nested #include. 34k isn't a major loss for the larger Teensies, but for the LC it's about 50% of the total code space.

Short of the author fixing the SD library to make it behave properly, does anyone have an idea how to eliminate this?
 
SD.h uses SdFat
So suggest to modify the SdFat config.h to meet your requirements
Maybe you could also ask Bill Greiman to advice on convering SdFat into a basic one
 
Back
Top