Presently, depending on the selected protocols, compiler allocates 12 or 16 bytes for the ssd1306_t struct. These changes shrink it to 4 or 8 bytes (8 bytes are needed with I2C present).
There are 3 individual changes in here:
Protocol and screen enums are turned into bit fields,
CS pin is also changed into a bit field and is moved, so it would be packed into 1 byte together with the protocol and the screen fields,
Union is removed.
The biggest space hogs were the enums. Changing them into bit fields just to make sure they are packed into a byte:
immediately reduced ssd1306_t to no more than 8 bytes (4 when only SPI3 support is selected).
The current use of the union seems superfluous, because when support for the I2C is included the absolute minimum required size for the ssd1306_t, with the enums packed, is 5 bytes, which turns into 8 allocated bytes with the compiler added padding. Without union the struct's payload will need 6 bytes, but as the allocation is still 8 the difference is only in how many padding bytes are used. And, IMHO, removing the union improves readability without sacrificing anything.
Changing cs_pin into a bit field allows not only SPI3, but also SPI4 to be as small as 4 bytes. The "penalty" for that is one instruction - EXTUI - that the compiler inserts to extract it.
Presently, depending on the selected protocols, compiler allocates 12 or 16 bytes for the
ssd1306_t
struct. These changes shrink it to 4 or 8 bytes (8 bytes are needed with I2C present).There are 3 individual changes in here:
The biggest space hogs were the enums. Changing them into bit fields just to make sure they are packed into a byte:
immediately reduced
ssd1306_t
to no more than 8 bytes (4 when only SPI3 support is selected).The current use of the
union
seems superfluous, because when support for the I2C is included the absolute minimum required size for thessd1306_t
, with the enums packed, is 5 bytes, which turns into 8 allocated bytes with the compiler added padding. Withoutunion
the struct's payload will need 6 bytes, but as the allocation is still 8 the difference is only in how many padding bytes are used. And, IMHO, removing theunion
improves readability without sacrificing anything.Changing
cs_pin
into a bit field allows not only SPI3, but also SPI4 to be as small as 4 bytes. The "penalty" for that is one instruction - EXTUI - that the compiler inserts to extract it.