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] change all callbacks to move to save copies. #46971

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

rynewang
Copy link
Contributor

@rynewang rynewang commented Aug 5, 2024

We have many callbacks in gRPC async handling, each of them takes a shape std::function<void(Status, const T&)>. Notice the const ref may be later copied again into caller's data structures, some times multiple times for nested callbacks.

Changes the signature to pass by rvalue reference (T&&) to save copies. This involves GcsClient, RayletClient and GcsSubscriber callers. This should work well since almost all replies are used only once, including the SubscriberCallback, except for NodeInfoAccessor::HandleNotification which both saves it to node_cache_ and passes it to the listeners, where we have to make a copy.

It's a pity we don't have a one-click performance test to verify gains of this PR.

@rynewang rynewang requested a review from a team as a code owner August 5, 2024 22:03
@rkooo567 rkooo567 self-assigned this Aug 5, 2024
@rkooo567
Copy link
Contributor

rkooo567 commented Aug 5, 2024

It's a pity we don't have a one-click performance test to verify gains of this PR.

what about just microbenchmark release tests?

@rynewang
Copy link
Contributor Author

rynewang commented Aug 5, 2024

started the microbenchmark

@rynewang rynewang added the go add ONLY when ready to merge, run all tests label Aug 5, 2024
@rynewang
Copy link
Contributor Author

rynewang commented Aug 6, 2024

Here is the result for microbenchmark (BEFORE = ray 2.22.0, AFTER = this PR, higher is better)

microbenchmark.aws (None) (1)
image

microbenchmark.aws (None) (2)
image

The numbers are persistent across runs (assume all microbenchmarks are the same thing). Notably, there's significant improvement (40%) for 1_1_async_actor_calls_async, but there also is a -10% regress for client__get_calls.

@rkooo567 do you have a gut feeling on why there are regressions and how we may save it?

Signed-off-by: Ruiyang Wang <[email protected]>

fix cpp test

Signed-off-by: Ruiyang Wang <[email protected]>

more moves

Signed-off-by: Ruiyang Wang <[email protected]>

change all callbacks to move to save copies

Signed-off-by: Ruiyang Wang <[email protected]>

move in cython (unfortunately not 0 copy)

Signed-off-by: Ruiyang Wang <[email protected]>
@rynewang
Copy link
Contributor Author

rynewang commented Aug 6, 2024

Latest PR, similar gains, same client__get_calls regression

image

@jjyao
Copy link
Collaborator

jjyao commented Aug 6, 2024

Can we check why some non-client tests have regressed?

Signed-off-by: Ruiyang Wang <[email protected]>
@rynewang
Copy link
Contributor Author

rynewang commented Aug 6, 2024

looks like the numbers that are fluctuating < +-10% are flaky. I suggest we first merge and wait for several day's numbers to see if there are consistent regressions.

Comment on lines +85 to +86
auto copied = reply;
callback(status, std::move(copied));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to avoid the manual copy by supporting both

callback(status, reply) // The reply is still needed, don't move, make a copy

and

callback(status, std::move(reply)) // The reply is no longer needed, can move

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the purpose is to force a move. If we support both const& and &&, we can always forget to move and accidentally make copies. So I'd stay with explicit copies.

Copy link
Contributor

@rkooo567 rkooo567 left a comment

Choose a reason for hiding this comment

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

guess a lot of reply is just a pointer, so there's not much perf benefit there. But no objection just merging it

@rynewang rynewang merged commit 5452c75 into ray-project:master Aug 7, 2024
5 checks passed
@rynewang rynewang deleted the move-callbacks branch August 7, 2024 21:46
dev-goyal pushed a commit to dev-goyal/ray that referenced this pull request Aug 8, 2024
We have many callbacks in gRPC async handling, each of them takes a
shape `std::function<void(Status, const T&)>`. Notice the const ref may
be later copied again into caller's data structures, some times multiple
times for nested callbacks.

Changes the signature to pass by rvalue reference (T&&) to save copies.
This involves GcsClient, RayletClient and GcsSubscriber callers. This
should work well since almost all replies are used only once, including
the SubscriberCallback, except for
`NodeInfoAccessor::HandleNotification` which both saves it to
`node_cache_` and passes it to the listeners, where we have to make a
copy.

It's a pity we don't have a one-click performance test to verify gains
of this PR.

Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Dev <[email protected]>
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.

3 participants