SuperHouse / esp-open-rtos

Open source FreeRTOS-based ESP8266 software framework
BSD 3-Clause "New" or "Revised" License
1.52k stars 491 forks source link

ssd1306: Reduce device descriptor size #636

Closed quietboil closed 6 years ago

quietboil commented 6 years ago

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:

ssd1306_protocol_t protocol : 4;
ssd1306_screen_t screen : 4;

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.