Skip to content

Conversation

@moskupols
Copy link

@moskupols moskupols commented Oct 23, 2025

@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)

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

Additional Comments (if any)

@moskupols moskupols requested a review from Naios as a code owner October 23, 2025 13:11
@moskupols moskupols force-pushed the avoid-extra-move-in-box-construction branch from 4156a67 to a80ff2a Compare October 23, 2025 13:29
@moskupols moskupols force-pushed the avoid-extra-move-in-box-construction branch from a80ff2a to da75c3b Compare October 23, 2025 13:36
@moskupols
Copy link
Author

moskupols commented Oct 23, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant