pavel-kirienko / o1heap

Constant-complexity deterministic memory allocator (heap) for hard real-time high-integrity embedded systems. There is very little activity because the project is finished and does not require further changes.
MIT License
253 stars 29 forks source link

Question about specific use of O1HEAP_LIKELY #19

Closed yurivish closed 9 months ago

yurivish commented 9 months ago

Hello! Thank you for publishing this code – I'm learning a lot from it! I noticed a line in o1heapAllocate that made me curious:

https://github.com/pavel-kirienko/o1heap/blob/aa3c253923db36eee7d73a4a5e30884a5fe7a6eb/o1heap/o1heap.c#L376

If I understand correctly, this conditional checks for allocation failure with an allocation size greater than zero. In practice, we expect most allocations to succeed. If the purpose of O1HEAP_LIKELY is to signal to the compiler that an expression is likely to be true, why is this a good place to put O1HEAP_LIKELY?

It seems to me that that amount > 0 is likely, but out == NULL is not, and so the conjunction is unlikely.

pavel-kirienko commented 9 months ago

This branching probability hint reduces the difference in the execution time between the success case and the OOM case. The effect depends on the specific platform, optimization level, and other build options. You can measure the effect on your platform.