LWG-3899 co_yielding elements of an lvalue generator is unnecessarily inefficient #5303
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #5111
I decided to look into this issue because I like coroutines and wanted to try contributing something.
It turns out that the implementation is quite straight forward, just adding a
yield_valueoverload taking a generator by lvalue reference which is otherwise the exact same as the overload taking a generator by rvalue reference.Since the LWG paper doesn't state specific numbers and to test the implementation I wrote a short test which I tried with both the current STL (as shipped with VS 17.13.0) and my patched version and in both Debug and Release configurations.
Results on my machine (time per element):
The results are bit noisy across multiple runs but show clearly that the general overload of
yield_value(which is used in the current version) takes almost twice as much time as the generator specialised version (both unoptimised and optimised). This is unsurprising since the general overload wraps the range in an extra generator resulting in two coroutine calls per element. The results also show that the difference disappears in the patched version since the lvalue generator also uses the specialised overload.