This entry was posted on December 23, 2009 at 2:37 pm and is filed under Articles. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
4 Responses to “STM32 GPIOs and Template Metaprogramming”
As you might have guessed I didn’t inline the mode() member function partly because I didn’t know an elegant way to do it (I’m still a beginner in TMP), but partly I didn’t inline it on purpose.
I’ll explain this more in detail: in many applications the GPIO mode settings are performed once at startup and never changed, so the mode() member function is not performance critical. Given that, I decided to apply Scott Meyers’ advice to factor template independent code out to avoid possible code bloat (a primary concern in embedded development).
I’ll do some code size measurement to see if the eliminated branch compensates for the inline and decide whether to apply your patch.
Anyway thanks for the comment, I’ve learnt something new :D
As you can see in the non inline version there is a one time cost of 64bytes (the GpioBase::doSetMode() function), and every function call costs 16bytes. In the inline version the cost is simply 24bytes per function call inlined. This is going to increase code size, but only slightly.
Now to performance:
I made a simple test to count how many calls to Gpio::mode() could be done in a second, and here are the results (code running from external RAM, so both figures are rather small):
426033 calls to mode() per second (with inlining)
181360 calls to mode() per second (without inlining)
2.3x speed improvement
I think the large performance gain outweighs the small code size increase. Patch applied (your name has been added to the changelog).