Reducing Binary sizes in Embedded Systems using C++ Exceptions

Khalil Estell

⏱ 90 minute session
14:00-15:30, Thursday, 18th April 2024
Universally, exceptions are avoided in embedded systems C++ code, with a preference for error codes as out parameters, return values, or sum types like std::expected<T,E>. This avoidance stems from two main issues: the size of the exception handling runtime and the performance cost of throwing an exception versus returning a status code. But what if I were to tell you that exceptions are the better choice for producing smaller binaries than using the alternatives?

In this talk, I will explore the space cost of exceptions on a Cortex M4 microcontroller using the ARM GNU Toolchain, how to reduce the space cost, compare it to result types and identifying the point where result types become more costly in terms of code size than exceptions. I will delve into the performance of exceptions on the same Cortex M4 microcontroller, demonstrating how I managed to cut the cycle count in half. I'll offer suggestions for improving exception performance on the sad path and explain how exceptions enhance performance on the happy path. Finally, I'll share why, after 11 years in embedded systems, 7 years teaching embedded systems, and 6 years in professional consumer electronics, I now believe that exceptions are the superior error handling mechanism for embedded systems.

🏷 embedded
🏷 exception handling
🏷 performance
🏷 optimizations

Khalil Estell

I am an African American who graduated with a degree in Computer Engineering from San Jose State University (SJSU) in 2017. Since March 2018, I have been working at Google, where I was a key firmware developer for the Google Pixel Watch, Google Pixel Buds Pro, and a few other projects that were ultimately not released. In addition to my work at Google, I served as a volunteer instructor at SJSU from 2017 to 2020 and have sponsored and mentored the SJSU Robotics team since 2017.

My passion lies in embedded systems, specifically in making embedded software more portable and user-friendly in C++. I believe that C++ is the ideal language for building the embedded applications I envision, offering the control and capability necessary to handle virtually all tasks in this domain. Currently, my focus is on demonstrating that exceptions are the most effective mechanism for error handling in C++ across all applications, particularly in embedded systems.