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

axum: remove unnecessary Arc::clone #2675

Merged
merged 2 commits into from Mar 25, 2024
Merged

Conversation

mladedav
Copy link
Contributor

Motivation

There was an unnecessary arc clone which is always dropped right afterward.

The try_unwrap call returns Ok only if the Arc has a strong count of exactly 1, but since we have just cloned it we know it will be more than 1. So the try_unwrap never works and the more expensive clone always happens anyway.

Solution

I have removed the Arc operations.

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

Thanks! Just one nitpick.

@@ -111,8 +111,7 @@ where
}

fn set_node(&mut self, path: &str, id: RouteId) -> Result<(), String> {
let mut node =
Arc::try_unwrap(Arc::clone(&self.node)).unwrap_or_else(|node| (*node).clone());
let mut node = (*self.node).clone();
Copy link
Member

Choose a reason for hiding this comment

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

Could you still use fully qualified syntax here? Just so it's clear that this is a 'shallow' clone. I suspect we're not consistent about that overall, but would probably use fully qualified if we were to make things consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was actually a deep clone.

However, I took a minute to think about how to actually do what the code probably wanted to do in the first place (which I guess I should have done at the start) and changed it to use Arc::make_mut. So as long as the router hasn't been cloned, it'll cheaply update it and otherwise fall back to a clone of Node.

@mladedav mladedav requested a review from jplatte March 25, 2024 18:33
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

Huh, I wouldn't have noticed that this was meant to be make_mut (semantics-wise), but now that you said it it makes perfect sense 😅

Thanks!

@jplatte jplatte merged commit 170f877 into tokio-rs:main Mar 25, 2024
18 checks passed
@mladedav mladedav deleted the dm/arc-clone branch March 25, 2024 20:53
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.

None yet

2 participants