function2 icon indicating copy to clipboard operation
function2 copied to clipboard

Avoid extra move in box construction

Open moskupols opened this issue 2 months ago • 1 comments

@Naios

Hi, I noticed constructing function involves at least three moves of my callable. In my use-case an extra move costs 150ns+. One of the extra moves is easy to avoid by forwarding a reference to constructor of box. The other extra move caused by that box being temporary before a move into vtable_t is harder to remove. I'm not addressing it in this PR.


What was a problem?

Since box::box accepts T by value, make_box contructs a temporary T when passing callable to constructor of box.

How this PR fixes the problem?

I replace box::box(T, Allocator) with two overloads: box::box(const T&, Allocator) and box::box(T&&, Allocator). This way user-provided lvalue- or rvalue-reference gets forwarded all the way to construction of box::value_.

Check lists (check x in [ ] of list items)

  • [x] Additional Unit Tests were added that test the feature or regression
  • [x] Coding style (Clang format was applied)

Additional Comments (if any)

moskupols avatar Oct 23 '25 13:10 moskupols

By the way I see removal of these moves a prerequisite for supporting non-moveable callables as suggested in #31

moskupols avatar Oct 23 '25 13:10 moskupols