Skip to content

[core] Lint cpp files in core_worker#48789

Merged
rynewang merged 20 commits intoray-project:masterfrom
dayshah:tidy-core-worker
Dec 24, 2024
Merged

[core] Lint cpp files in core_worker#48789
rynewang merged 20 commits intoray-project:masterfrom
dayshah:tidy-core-worker

Conversation

@dayshah
Copy link
Copy Markdown
Contributor

@dayshah dayshah commented Nov 19, 2024

Why are these changes needed?

Running clang-tidy on files in core-worker, mostly auto-fixes from clang-tidy, some manual fixes. Want it to be part of ci or pre-commit hook once we lint the core src codebase.

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 :(

Signed-off-by: dayshah <dhyey2019@gmail.com>
@dentiny
Copy link
Copy Markdown
Contributor

dentiny commented Nov 21, 2024

These changes looks valid and great, do we plan to merge this PR? Let me know if there's anything I could help!

Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
@dayshah
Copy link
Copy Markdown
Contributor Author

dayshah commented Nov 21, 2024

These changes looks valid and great, do we plan to merge this PR? Let me know if there's anything I could help!

ya just needed it to go through headers separately and do some manual stuff

Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
@dayshah dayshah requested review from dentiny and rynewang November 21, 2024 20:25
@dayshah dayshah marked this pull request as ready for review November 21, 2024 20:25
Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>

void ActorManager::MarkActorKilledOrOutOfScope(
std::shared_ptr<ActorHandle> actor_handle) {
const std::shared_ptr<ActorHandle> &actor_handle) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the ownership is not transferred, what about const ActorHandle&?
Reasons:

  • Const reference ensures immutability and non-nullability;
  • From my limited programming experience, I almost never see people using const std::shared_ptr<>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

agree I think const ActorHandle& makes sense, and shared ptr copy avoidance makes basically no effect
But it was just a clang-tidy fix where it's copied and not moved so can be ref, and linters don't consider it to be trivially copyable, i assume because copy means you're still accessing ref counter and doing the atomic increment which can be a little bit of a hit on heavily threaded code (which we don't really have), but still just for the linter.

Later I think it could also make things easier to review to cut down on # of shared ptrs if we see paths where there's no real copies being made, cutting down on # of shared ptrs will make a diff

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But it was just a clang-tidy fix where it's copied and not moved so can be ref

I'm sure using ref instead of shared pointer is better, and clang tidy will not complain about it.
My take on clang-tidy is, it's useful to provide good-enough feedbacks, but not perfect; that's why we need NOLINT.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll make a follow-up pr off of this to change all const shared_ptr& that were changed on this to const &, but want to minimize more manual changes on this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Feel free to leave a TODO if you plan to followup (just in case we forget :) )

Copy link
Copy Markdown
Contributor

@dentiny dentiny left a comment

Choose a reason for hiding this comment

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

Would be nice to document the linting command (step-by-step) in the PR descriptor, or somewhere more formal for better visibility and educational purpose :)

// instance, the sem_unlink() calls below will both fail with ENOENT.
int ret = sem_unlink(GetSemaphoreHeaderName(name).c_str());
if (ret) {
if (ret != 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel like we should have macros to handle syscall errors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added todo comment for follow-up, wanna keep changes minimal to clang-tidy stuff here, ruiyang's comment mentioned trying to keep this limited for review purposes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a nit on comment, I really prefer to have a macro for syscall, since I think we heavily do syscall in our codebase.

@dayshah
Copy link
Copy Markdown
Contributor Author

dayshah commented Nov 21, 2024

Would be nice to document the linting command (step-by-step) in the PR descriptor, or somewhere more formal for better visibility and educational purpose :)

installed llvm
for file in *.cpp; do /opt/homebrew/opt/llvm@19/bin/clang-tidy --fix-errors --fix-notes $file; done
let it run for a while, while you do other things
look out for specific checks that failed in output that clangd can't see using clang-tidy such as use-after-move

i also tried various --header-filter but didn't get great results, i found putting extra .clang-tidy file with just -* in directories you don't want to lint can be a potential solution to speed it up

And then I go through manually, have clangd setup locally from https://github.com/ray-project/ray/blob/master/BUILD.bazel#L102-L108. And then manually fix things it highlights / related things i see

I'm also trying to add this (https://github.com/lljbash/clangd-tidy) as a pre-commit hook or in ci and will put these directions on fixing there

: spec(std::move(spec_arg)),
num_retries_left(num_retries_left_arg),
counter(counter),
counter(&counter),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bad naming... Avoid naming ctor argument and data member the same.

Copy link
Copy Markdown
Contributor Author

@dayshah dayshah Dec 19, 2024

Choose a reason for hiding this comment

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

can do in a follow-up want to minimize more changes here

Copy link
Copy Markdown
Contributor

@dentiny dentiny left a comment

Choose a reason for hiding this comment

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

Thanks for the effort! Just a few suggestions/comments, happy to discuss.

Signed-off-by: Dhyey Shah <dhyey2019@gmail.com>
@rynewang
Copy link
Copy Markdown
Contributor

Whoa this is impressive. I took a glance and generally LGTM, though I did not read every line.

Can you do a clang-tidy-only patch and another manual patch, so that we can merge the tooling one fast, and then take a deep look at the manual ones?

Is the .clang-tidy change affecting CI?

Lint failure in microcheck

Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
@dayshah
Copy link
Copy Markdown
Contributor Author

dayshah commented Nov 22, 2024

Whoa this is impressive. I took a glance and generally LGTM, though I did not read every line.

Can you do a clang-tidy-only patch and another manual patch, so that we can merge the tooling one fast, and then take a deep look at the manual ones?

Is the .clang-tidy change affecting CI?

Lint failure in microcheck

  1. The tried to keep the manual changes I made here minimal, a lot involved just things clang-tidy may have not fixed "correctly" or things that had to change to make it compile with its auto-fixes. I think the main things I did manually was adding a couple extra moves into function captures and removing const from member variables.
  2. .clang-tidy isn't used at all in ci rn, I just missed formatting a file, updated

Signed-off-by: Dhyey Shah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
@rynewang
Copy link
Copy Markdown
Contributor

merge conflict

Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
Signed-off-by: dayshah <dhyey2019@gmail.com>
@dayshah dayshah added the go add ONLY when ready to merge, run all tests label Dec 19, 2024
Signed-off-by: Dhyey Shah <dhyey2019@gmail.com>
Copy link
Copy Markdown
Contributor

@dentiny dentiny left a comment

Choose a reason for hiding this comment

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

Thanks for the effort!

If you don't mind, when you have some time, could you please have a short doc how to play with clang tidy and do some education? playing clang-tidy with bazel requires some setup, for example, the compilation server.

Signed-off-by: dayshah <dhyey2019@gmail.com>
@rynewang
Copy link
Copy Markdown
Contributor

merge conflict

@dayshah
Copy link
Copy Markdown
Contributor Author

dayshah commented Dec 23, 2024

merge conflict

updated

@rynewang rynewang enabled auto-merge (squash) December 23, 2024 22:44
@rynewang rynewang merged commit 49a07a5 into ray-project:master Dec 24, 2024
@dayshah dayshah deleted the tidy-core-worker branch December 28, 2024 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-backlog go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants