Skip to content

Conversation

@PatKamin
Copy link
Contributor

Copy a shared_ptr directly instead of using more expensive make_shared_from_this.

Copy a shared_ptr directly instead of using more expensive make_shared_from_this.
Copy link
Contributor

@lslusarczyk lslusarczyk Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best API for shared pointer (generally for moveable classes) is to take it by value, not "const &"
"const &" guarantees there will be at least one copy needed when storing this object, while taking by value and calling move allows called to pass rvalue and avoid copy at all

so, in ideal worlds we will take by value and call MContext = std::move(Context);

however getContextImplPtr returns "const &" so copy can not be avoided

Please explain why shared_from_this cost is bigger than copying.

@PatKamin
Copy link
Contributor Author

Closing as gains can be neglected

@PatKamin PatKamin closed this Dec 18, 2025
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.

2 participants