Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kevin85421
Copy link
Member

@kevin85421 kevin85421 commented Dec 25, 2024

Why are these changes needed?

This YouTube video mentions that make_shared is better than std::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.

  • Use make_shared if the constructor is accessible.
  • Add comments similar to this if the constructor is private or protected.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@kevin85421 kevin85421 changed the title [core] [core][chore] Use make_shared to create shared ptr if possible Dec 25, 2024
Signed-off-by: kaihsun <[email protected]>
Signed-off-by: kaihsun <[email protected]>
@kevin85421 kevin85421 added the go add ONLY when ready to merge, run all tests label Dec 25, 2024
@kevin85421 kevin85421 marked this pull request as ready for review December 25, 2024 08:56
Signed-off-by: kaihsun <[email protected]>
Copy link
Contributor

@dayshah dayshah left a 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)));
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kevin85421
Copy link
Member Author

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

Good to know! This should be a better option. Still learning C++ 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants