KEMBAR78
`<functional>`: make `move_only_function` do less work by AlexGuteniev · Pull Request #5328 · microsoft/STL · GitHub
Skip to content

Conversation

@AlexGuteniev
Copy link
Contributor

@AlexGuteniev AlexGuteniev commented Mar 7, 2025

Primarily I treat this more as a clarity improvement.
Yet, the benchmark results prove this PR deserving performance label.

Resolves #5316

⏱️ Benchmark results

Benchmark before after
mof_none 0.475 ns 0.479 ns
mof_construct 0.799 ns 0.714 ns
mof_move 1.06 ns 1.07 ns
mof_construct_and_move 1.68 ns 1.66 ns

🥇 Results interpretation

mof_construct is the only that changed more than results variation, it shows some improvement due to doing less work.

mof_construct_and_move addresses the concern of potential slowdown due to failed store-to-load forwarding.

The concern was due to storing zero as one pointer but then loading two pointers, which can be loaded in the same vector register. If the load happens shortly after the store, it has to wait for the store to completely reach the cache, as the wider value cannot be obtained from the narrower store buffer entry.

The situation apparently does not happen, as before the actual two-pointers move, there's vtable obtaining and inspection, which takes enough time for the store to complete.

mof_none and mof_move give some idea on the corresponding baseline timings. They are not expected to change.

@AlexGuteniev AlexGuteniev requested a review from a team as a code owner March 7, 2025 22:00
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Mar 7, 2025
@AlexGuteniev AlexGuteniev changed the title <functiona>: make move_only_function doing less work <functional>: make move_only_function doing less work Mar 7, 2025
@StephanTLavavej StephanTLavavej added the performance Must go faster label Mar 8, 2025
@StephanTLavavej StephanTLavavej changed the title <functional>: make move_only_function doing less work <functional>: make move_only_function do less work Mar 8, 2025
@StephanTLavavej
Copy link
Member

Thanks! 😻

On my 5950X I actually observe mof_construct_and_move becoming slower:

Benchmark Before After Speedup
mof_none 1.46 ns 1.26 ns 1.16
mof_construct 1.91 ns 1.48 ns 1.29
mof_move 4.21 ns 4.18 ns 1.01
mof_construct_and_move 3.38 ns 7.25 ns 0.47

However, this case seems unrealistic (how often are users default-constructing and immediately moving), and if the code had originally been written this way, I would never have asked for an additional pointer to be nulled.

@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Mar 8, 2025
@StephanTLavavej

This comment was marked as resolved.

@AlexGuteniev

This comment was marked as resolved.

@StephanTLavavej StephanTLavavej self-assigned this Mar 12, 2025
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Mar 12, 2025
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 5dcd917 into microsoft:main Mar 13, 2025
39 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Mar 13, 2025
@StephanTLavavej
Copy link
Member

Thanks for improving this code and benchmarking it! ⏱️ 🚀 😻

@AlexGuteniev AlexGuteniev deleted the memcpy_only_function branch March 13, 2025 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

<functional>: _Move_only_function_base::_Construct_with_null() appears to be doing too much work

2 participants