NRVO for coroutines
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
A common problem with typical task implementations in many coroutine libraries is that the memory for the value to be returned is stored both in the coroutine promise and in frame local variables where it is constructed and returned. This introduces duplication, increases the frame size, and adds a call to the move constructor.
This problem is especially acute for large values or types with non-trivial move constructors, such as std strings.
And, ofc, it prevents returning non-movable types from coroutines, which is sad (but actually its possible with new attribute)
This problem can be solved by introducing a new attribute—its design is subject to debate, but the idea is simple: to give the compiler a hint where to construct the value in cases where, in a regular function, it would use the NRVO (named return value optimization)
example:
```cpp
void bar(std::string&);
dd::task foo() {
std::string s;
co_await std::suspend_always{};
bar(s);
co_await std::suspend_always{};
// in normal function this is NRVO, but in coroutines its promise.return_value(std::move(s))
co_return s;
}
```
In my library i created additional tag this_coro::return_place, which is used like that:
```cpp
void bar(std::string&);
dd::task foo() {
// constructs value in-place in coro promise
std::string& s = co_await dd::this_coro::return_place;
co_await std::suspend_always{};
bar(s);
co_await std::suspend_always{};
co_return dd::rvo;
}
```
without "NRVO":
https://godbolt.org/z/8YfTrc6K9
with "NRVO":
https://godbolt.org/z/K9647ozGY
Contributor guide
Research direction
The issue does not identify repository files, tests, or compiler entry points. Start by comparing the two linked Compiler Explorer examples and reviewing the proposed return_place and rvo usage; done would require an agreed attribute or language design and corresponding coroutine support.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100