-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core][chore] Use make_shared to create shared ptr if possible #49430
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: kaihsun <[email protected]>
Signed-off-by: kaihsun <[email protected]>
Signed-off-by: kaihsun <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-tidy actually auto-fixes this too, been going through and using to lint the codebase, https://clang.llvm.org/extra/clang-tidy/checks/modernize/make-shared.html
@@ -25,6 +25,8 @@ class DefaultCoreWorkerMemoryStoreWithThread : public CoreWorkerMemoryStore { | |||
std::unique_ptr<InstrumentedIOContextWithThread> io_context = | |||
std::make_unique<InstrumentedIOContextWithThread>( | |||
"DefaultCoreWorkerMemoryStoreWithThread"); | |||
// C++ limitation: std::make_unique cannot be used because std::unique_ptr cannot | |||
// invoke private constructors. | |||
return std::unique_ptr<DefaultCoreWorkerMemoryStoreWithThread>( | |||
new DefaultCoreWorkerMemoryStoreWithThread(std::move(io_context))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo not as necessary to comment for make_unique, same perf benefit doesn't apply because there's no separate allocation for ref counting, but still good to be consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on this YouTube video, make_unique
is still better compared to explicitly calling the constructor due to its exception safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know! This should be a better option. Still learning C++ 🙂 |
Why are these changes needed?
This YouTube video mentions that
make_shared
is better thanstd::shared_ptr<MyClass>(new MyClass(...))
in two aspects:make_shared
requires only one memory allocation.make_shared
is exception-safe.I quickly went through all lines of code matching
std::shared_ptr<
in the repo.make_shared
if the constructor is accessible.Related issue number
Checks
git commit -s
) in this PR.scripts/format.sh
to lint the changes in this PR.method in Tune, I've added it in
doc/source/tune/api/
under thecorresponding
.rst
file.