-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
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.
Thanks! Just one nitpick.
axum/src/routing/path_router.rs
Outdated
@@ -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(); |
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.
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.
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.
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
.
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.
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!
Motivation
There was an unnecessary arc clone which is always dropped right afterward.
The
try_unwrap
call returnsOk
only if theArc
has a strong count of exactly 1, but since we have just cloned it we know it will be more than 1. So thetry_unwrap
never works and the more expensiveclone
always happens anyway.Solution
I have removed the
Arc
operations.