DRNadler / FreeRTOS_helpers

Helpers for some common problems when using FreeRTOS, GCC, and newlib.
139 stars 21 forks source link

Optimize _sbrk_r when no increment is needed #14

Open SilentButeo2 opened 1 year ago

SilentButeo2 commented 1 year ago

During debug I hit the _sbrk_r() function, but the incr parameter was 0. Callstack points out it is mallinfo() that initiates the call. Because the current code goes into a critical section (ST) or suspends all tasks (NXP) but doesn't change anything in between, this is optimized away.

Unless the _sbrk_r is also used do some limit checks, I think this adjustment is safe. Tested with an ST setup.

StefanBalt commented 2 months ago
1. How often is mallinfo called? Is this really worth adding code to optimize?

I do not know why mallinfo() should be relevant as only sbrk() is changed.

2. If so, please add a comment about WHY this is worthwhile optimizing ie _Happens when called from mallinfo twice per week_

I think mallinfo() does not matter here. sbrk() is called any time an allocation does not fit into the memory previously requested via sbrk(). Typically this happens a lot at the start of the program and fewer the longer it runs.

I am not sure if this optimization is needed but it seems cheap with only a few lines added.

3. Didn't you mean to cast the return value to (void*), not (char*)?

All returns in sbrk() cast to (char*), so I guess this is for consistency reasons.