Issue 132139
Summary [libc++] `std::function` with small object optimization copies instead of moves
Labels libc++
Assignees
Reporter BenjaminSchaaf
    When using `std::function` I noticed that *sometimes* the captured data is copied even if the `std::function` is only ever moved.
I've tracked this down to the following code:
https://github.com/llvm/llvm-project/blob/6003c3055a4630be31cc3d459cdbb88248a007b9/libcxx/include/__functional/function.h#L394

It looks like when the small object optimization is in use for `std::function`, then the captured data is copied instead of moved whenever the function is moved. In extreme cases this could result in massively more work being done.

The issue reproduces trivially, see godbolt: https://godbolt.org/z/czTev8jfc

Note that the captured data needs to be < `3*sizeof(void *)` and have `noexcept` on its copy constructor to trigger the small object optimization. When that's the case the data is copied instead of moved.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to