* [PATCH net-next] rust: net::phy inline if expressions to improve read_status
@ 2025-09-19 11:20 Thorsten Blum
2025-09-19 12:54 ` Andrew Lunn
0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-09-19 11:20 UTC (permalink / raw)
To: FUJITA Tomonori, Trevor Gross, Andrew Lunn, Heiner Kallweit,
Russell King, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Danilo Krummrich
Cc: Thorsten Blum, netdev, rust-for-linux, linux-kernel
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'.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
drivers/net/phy/ax88796b_rust.rs | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs
index bc73ebccc2aa..2dfd37936689 100644
--- a/drivers/net/phy/ax88796b_rust.rs
+++ b/drivers/net/phy/ax88796b_rust.rs
@@ -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()?;
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net-next] rust: net::phy inline if expressions to improve read_status
2025-09-19 11:20 [PATCH net-next] rust: net::phy inline if expressions to improve read_status Thorsten Blum
@ 2025-09-19 12:54 ` Andrew Lunn
2025-09-19 13:30 ` Thorsten Blum
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Lunn @ 2025-09-19 12:54 UTC (permalink / raw)
To: Thorsten Blum
Cc: FUJITA Tomonori, Trevor Gross, Heiner Kallweit, Russell King,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Danilo Krummrich, netdev, rust-for-linux, linux-kernel
On Fri, Sep 19, 2025 at 01:20:08PM +0200, Thorsten Blum wrote:
> 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
What is the issue of calling it twice, or 42 times??
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next] rust: net::phy inline if expressions to improve read_status
2025-09-19 12:54 ` Andrew Lunn
@ 2025-09-19 13:30 ` Thorsten Blum
2025-09-19 15:16 ` Andrew Lunn
0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Blum @ 2025-09-19 13:30 UTC (permalink / raw)
To: Andrew Lunn
Cc: FUJITA Tomonori, Trevor Gross, Heiner Kallweit, Russell King,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Danilo Krummrich, netdev, rust-for-linux, linux-kernel
On 19. Sep 2025, at 14:54, Andrew Lunn wrote:
> On Fri, Sep 19, 2025 at 01:20:08PM +0200, Thorsten Blum wrote:
>> 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
>
> What is the issue of calling it twice, or 42 times??
>
>> 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.
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next] rust: net::phy inline if expressions to improve read_status
2025-09-19 13:30 ` Thorsten Blum
@ 2025-09-19 15:16 ` Andrew Lunn
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2025-09-19 15:16 UTC (permalink / raw)
To: Thorsten Blum
Cc: FUJITA Tomonori, Trevor Gross, Heiner Kallweit, Russell King,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Danilo Krummrich, netdev, rust-for-linux, linux-kernel
> 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.
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-19 15:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-19 11:20 [PATCH net-next] rust: net::phy inline if expressions to improve read_status Thorsten Blum
2025-09-19 12:54 ` Andrew Lunn
2025-09-19 13:30 ` Thorsten Blum
2025-09-19 15:16 ` Andrew Lunn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).