How can I verify that the compiler is eliding my coroutine heap allocations?

396 views Asked by At

I'm having trouble understanding the memory allocations made by C++20 coroutines. For my code, I would like to verify that the compiler is eliding heap allocations, and if it isn't, to find out what data is being placed within that allocation. My strategy right now has been to inspect the assembly output, but I'm not sure what to look for.

How can I verify that heap allocations are elided?

Edit

A possible example to refer to would be Lewis Baker's code here: https://www.godbolt.org/z/EoovEEKvW

Respondents should feel free to refer to other code or libraries if they like.

1

There are 1 answers

0
Nicol Bolas On

The simplest thing you could do is give your promise type allocator/deallocator functions (ie: operator new/delete members). These functions are called to allocate storage for the coroutine if such storage is needed. As such, if they are not called for a particular use of a coroutine, storage was not needed.

However, this is not a guarantee. It is entirely possible that implementations will bypass elision if you provide such allocators. After all, your program might rely on having a short stack, so if you use allocators, they may be allocating from static storage instead of the actual program stack.

Then again, maybe not. The most you can do is try it and see what happens.