rust: net::phy inline if expressions to improve read_status
Subsystems (5)
PATCH 1/1 rust: net::phy inline if expressions to improve read_status
Inline the if expressions for dev.set_speed() and dev.set_duplex() to improve read_status(). This ensures dev.set_speed() is called only once and allows us to remove the local variable 'duplex'.
--- drivers/net/phy/ax88796b_rust.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/net/phy/ax88796b_rust.rs +6 -7
@@ -56,18 +56,17 @@ fn read_status(dev: &mut phy::Device) -> Result<u16> {// linkmode so use MII_BMCR as default values. let ret = dev.read (C22 ::BMCR )?; -if ret &BMCR_SPEED100 !=0 { - dev.set_speed (uapi::SPEED_100 ); + dev.set_speed (if ret &BMCR_SPEED100 !=0 { + uapi::SPEED_100 }else { - dev.set_speed (uapi::SPEED_10 ); - } + uapi::SPEED_10 + }); -let duplex =if ret &BMCR_FULLDPLX !=0 { + dev.set_duplex (if ret &BMCR_FULLDPLX !=0 { phy::DuplexMode ::Full }else { phy::DuplexMode ::Half - }; - dev.set_duplex (duplex); + }); dev.genphy_read_lpa ()?;
Signature · 1 line
2.51.0
Andrew Lunn
maintainerQuoted 2 lines · Thorsten Blum
Inline the if expressions for
dev.set_speed()anddev.set_duplex()to improveread_status(). This ensuresdev.set_speed()is called only once
What is the issue of calling it twice, or 42 times??
Quoted 1 line
and allows us to remove the local variable 'duplex'.
And what is wrong with local variables?
And did you disassemble the code? What is the compiler actually doing? Does it actually have a stack variable, or is it just a register? Does the optimiser end up with just a single call to set_speed()?
This is slow path code. It gets called at most once per second. Performance does not matter. Which means readability has much higher preference. And i find the older version much easier to read.
Andrew
Quoted 10 lines · Andrew Lunn
Quoted 2 lines · Thorsten Blum
Inline the if expressions for
dev.set_speed()anddev.set_duplex()to improveread_status(). This ensuresdev.set_speed()is called only onceWhat is the issue of calling it twice, or 42 times??
Quoted 1 line
and allows us to remove the local variable 'duplex'.
And what is wrong with local variables?
Quoted 7 lines
And did you disassemble the code? What is the compiler actually doing? Does it actually have a stack variable, or is it just a register? Does the optimiser end up with just a single call to
set_speed()?This is slow path code. It gets called at most once per second. Performance does not matter. Which means readability has much higher preference. And i find the older version much easier to read.
There's obviously nothing wrong with local variables. This patch is not about performance improvements, but writing consistent and idiomatic Rust code.
Currently, dev.set_duplex() uses a local variable and is called once, whereas dev.set_speed() doesn't use a local variable and is called twice. This is just a cleanup patch using idiomatic (afaik) Rust code.
Thanks,
Thorsten
Andrew Lunn
maintainerQuoted 7 lines
There's obviously nothing wrong with local variables. This patch is not about performance improvements, but writing consistent and idiomatic Rust code.
Currently,
dev.set_duplex()uses a local variable and is called once, whereasdev.set_speed()doesn't use a local variable and is called twice.
I would suggest the opposite change if you want to make the code consistent:
let speed = if ret & BMCR_SPEED100 != 0 {
uapi::SPEED_100
} else {
uapi::SPEED_10
}
dev.set_speed(speed)
Andrew