mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: num: Add inline annotation to Bounded methods
@ 2026-10-19 10:57 Link Mauve
  2026-10-19 11:09 ` Gary Guo
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Link Mauve @ 2026-10-19 10:57 UTC (permalink / raw)
  To: Alexandre Courbot, Yury Norov, rust-for-linux
  Cc: Link Mauve, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan, linux-kernel

It was previously generating a ton of very short functions, such as
<<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
<<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.

These are obviously better inlined, although I wasn’t completely sure
between #[inline] and #[inline(always)], but the former worked fine in
my setup so I went for it, we can always adjust it later.

Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>
---
 rust/kernel/num/bounded.rs | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 0b32f4343922..d3486675046a 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -264,6 +264,7 @@ impl<const N: u32> Bounded<$type, N> {
                 ", 4>::new::<7>();")]
             /// assert_eq!(v.get(), 7);
             /// ```
+            #[inline]
             pub const fn new<const VALUE: $type>() -> Self {
                 // Statically assert that `VALUE` fits within the set number of bits.
                 const_assert!(fits_within!(VALUE, $type, N));
@@ -309,6 +310,7 @@ impl<T, const N: u32> Bounded<T, N>
     /// # Safety
     ///
     /// The caller must ensure that `value` can be represented within `N` bits.
+    #[inline]
     const unsafe fn __new(value: T) -> Self {
         // Enforce the type invariants.
         // `N` cannot be zero.
@@ -350,6 +352,7 @@ impl<T, const N: u32> Bounded<T, N>
     /// let v = Bounded::<i8, 4>::try_new(8);
     /// assert_eq!(v, None);
     /// ```
+    #[inline]
     pub fn try_new(value: T) -> Option<Self> {
         fits_within(value, N).then(|| {
             // SAFETY: `fits_within` confirmed that `value` can be represented within `N` bits.
@@ -412,6 +415,7 @@ pub fn from_expr(expr: T) -> Self {
     /// let v = Bounded::<u32, 4>::new::<7>();
     /// assert_eq!(v.get(), 7u32);
     /// ```
+    #[inline]
     pub const fn get(self) -> T {
         self.0
     }
@@ -430,6 +434,7 @@ pub const fn get(self) -> T {
     /// // The contained values are equal even though `larger_v` has a bigger capacity.
     /// assert_eq!(larger_v, v);
     /// ```
+    #[inline]
     pub const fn extend<const M: u32>(self) -> Bounded<T, M> {
         const_assert!(
             M >= N,
@@ -459,6 +464,7 @@ pub const fn extend<const M: u32>(self) -> Bounded<T, M> {
     /// // ... but doesn't fit within `2` bits.
     /// assert_eq!(v.try_shrink::<2>(), None);
     /// ```
+    #[inline]
     pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>> {
         Bounded::<T, M>::try_new(self.get())
     }
@@ -482,6 +488,7 @@ pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>> {
     /// // This won't build: a `u8` is smaller than the required 12 bits.
     /// // let _: Bounded<u8, 12> = v.cast();
     /// ```
+    #[inline]
     pub fn cast<U>(self) -> Bounded<U, N>
     where
         U: TryFrom<T> + Integer,
@@ -510,6 +517,7 @@ pub fn cast<U>(self) -> Bounded<U, N>
     ///
     /// assert_eq!(v_shifted.get(), 0xff);
     /// ```
+    #[inline]
     pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
         const_assert!(SHIFT < T::BITS);
         const_assert!(RES + SHIFT >= N);
@@ -563,6 +571,7 @@ pub fn shr_exact<const SHIFT: u32, const RES: u32>(self) -> Option<Bounded<T, RE
     ///
     /// assert_eq!(v_shifted.get(), 0xff00);
     /// ```
+    #[inline]
     pub fn shl<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
         const_assert!(RES >= N + SHIFT);
 

base-commit: 2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64
-- 
2.56.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: num: Add inline annotation to Bounded methods
  2026-10-19 10:57 [PATCH] rust: num: Add inline annotation to Bounded methods Link Mauve
@ 2026-10-19 11:09 ` Gary Guo
  2026-10-19 14:07 ` Alice Ryhl
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Gary Guo @ 2026-10-19 11:09 UTC (permalink / raw)
  To: Link Mauve, Alexandre Courbot, Yury Norov, rust-for-linux
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan, linux-kernel

On Mon Oct 19, 2026 at 11:57 AM BST, Link Mauve wrote:
> It was previously generating a ton of very short functions, such as
> <<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
> <<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.
> 
> These are obviously better inlined, although I wasn’t completely sure
> between #[inline] and #[inline(always)], but the former worked fine in
> my setup so I went for it, we can always adjust it later.
> 
> Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/num/bounded.rs | 9 +++++++++
>  1 file changed, 9 insertions(+)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: num: Add inline annotation to Bounded methods
  2026-10-19 10:57 [PATCH] rust: num: Add inline annotation to Bounded methods Link Mauve
  2026-10-19 11:09 ` Gary Guo
@ 2026-10-19 14:07 ` Alice Ryhl
  2026-10-19 14:25 ` Alexandre Courbot
  2026-10-19 14:51 ` Yury Norov
  3 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-10-19 14:07 UTC (permalink / raw)
  To: Link Mauve
  Cc: Alexandre Courbot, Yury Norov, rust-for-linux, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Onur Özkan, linux-kernel

On Mon, Oct 19, 2026 at 12:57:38PM +0200, Link Mauve wrote:
> It was previously generating a ton of very short functions, such as
> <<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
> <<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.
> 
> These are obviously better inlined, although I wasn’t completely sure
> between #[inline] and #[inline(always)], but the former worked fine in
> my setup so I went for it, we can always adjust it later.
> 
> Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>

I agree with using #[inline] here. You only want #[inline(always)] when
you are using build_assert!() in a way that depends on function
arguments.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: num: Add inline annotation to Bounded methods
  2026-10-19 10:57 [PATCH] rust: num: Add inline annotation to Bounded methods Link Mauve
  2026-10-19 11:09 ` Gary Guo
  2026-10-19 14:07 ` Alice Ryhl
@ 2026-10-19 14:25 ` Alexandre Courbot
  2026-10-19 14:51 ` Yury Norov
  3 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2026-10-19 14:25 UTC (permalink / raw)
  To: Link Mauve
  Cc: Yury Norov, rust-for-linux, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Onur Özkan, linux-kernel

On Mon Oct 19, 2026 at 7:57 PM JST, Link Mauve wrote:
> It was previously generating a ton of very short functions, such as
> <<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
> <<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.
>
> These are obviously better inlined, although I wasn’t completely sure
> between #[inline] and #[inline(always)], but the former worked fine in
> my setup so I went for it, we can always adjust it later.
>
> Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>

As Alice pointed out, `#[inline]` is the correct one here. Thanks for
fixing this!

Acked-by: Alexandre Courbot <acourbot@nvidia.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: num: Add inline annotation to Bounded methods
  2026-10-19 10:57 [PATCH] rust: num: Add inline annotation to Bounded methods Link Mauve
                   ` (2 preceding siblings ...)
  2026-10-19 14:25 ` Alexandre Courbot
@ 2026-10-19 14:51 ` Yury Norov
  2026-10-19 15:07   ` Alexandre Courbot
  3 siblings, 1 reply; 6+ messages in thread
From: Yury Norov @ 2026-10-19 14:51 UTC (permalink / raw)
  To: Link Mauve
  Cc: Alexandre Courbot, Yury Norov, rust-for-linux, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Onur Özkan, linux-kernel

On Mon, Oct 19, 2026 at 12:57:38PM +0200, Link Mauve wrote:
> It was previously generating a ton of very short functions, such as
> <<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
> <<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.
> 
> These are obviously better inlined, although I wasn’t completely sure
> between #[inline] and #[inline(always)], but the former worked fine in
> my setup so I went for it, we can always adjust it later.
> 
> Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>

Reviewed-by: Yury Norov <ynorov@nvidia.com>

Do you want me to take the patch with the bitops branch, or you've
a better route in mind?

Thanks,
Yury

> ---
>  rust/kernel/num/bounded.rs | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> index 0b32f4343922..d3486675046a 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -264,6 +264,7 @@ impl<const N: u32> Bounded<$type, N> {
>                  ", 4>::new::<7>();")]
>              /// assert_eq!(v.get(), 7);
>              /// ```
> +            #[inline]
>              pub const fn new<const VALUE: $type>() -> Self {
>                  // Statically assert that `VALUE` fits within the set number of bits.
>                  const_assert!(fits_within!(VALUE, $type, N));
> @@ -309,6 +310,7 @@ impl<T, const N: u32> Bounded<T, N>
>      /// # Safety
>      ///
>      /// The caller must ensure that `value` can be represented within `N` bits.
> +    #[inline]
>      const unsafe fn __new(value: T) -> Self {
>          // Enforce the type invariants.
>          // `N` cannot be zero.
> @@ -350,6 +352,7 @@ impl<T, const N: u32> Bounded<T, N>
>      /// let v = Bounded::<i8, 4>::try_new(8);
>      /// assert_eq!(v, None);
>      /// ```
> +    #[inline]
>      pub fn try_new(value: T) -> Option<Self> {
>          fits_within(value, N).then(|| {
>              // SAFETY: `fits_within` confirmed that `value` can be represented within `N` bits.
> @@ -412,6 +415,7 @@ pub fn from_expr(expr: T) -> Self {
>      /// let v = Bounded::<u32, 4>::new::<7>();
>      /// assert_eq!(v.get(), 7u32);
>      /// ```
> +    #[inline]
>      pub const fn get(self) -> T {
>          self.0
>      }
> @@ -430,6 +434,7 @@ pub const fn get(self) -> T {
>      /// // The contained values are equal even though `larger_v` has a bigger capacity.
>      /// assert_eq!(larger_v, v);
>      /// ```
> +    #[inline]
>      pub const fn extend<const M: u32>(self) -> Bounded<T, M> {
>          const_assert!(
>              M >= N,
> @@ -459,6 +464,7 @@ pub const fn extend<const M: u32>(self) -> Bounded<T, M> {
>      /// // ... but doesn't fit within `2` bits.
>      /// assert_eq!(v.try_shrink::<2>(), None);
>      /// ```
> +    #[inline]
>      pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>> {
>          Bounded::<T, M>::try_new(self.get())
>      }
> @@ -482,6 +488,7 @@ pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>> {
>      /// // This won't build: a `u8` is smaller than the required 12 bits.
>      /// // let _: Bounded<u8, 12> = v.cast();
>      /// ```
> +    #[inline]
>      pub fn cast<U>(self) -> Bounded<U, N>
>      where
>          U: TryFrom<T> + Integer,
> @@ -510,6 +517,7 @@ pub fn cast<U>(self) -> Bounded<U, N>
>      ///
>      /// assert_eq!(v_shifted.get(), 0xff);
>      /// ```
> +    #[inline]
>      pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
>          const_assert!(SHIFT < T::BITS);
>          const_assert!(RES + SHIFT >= N);
> @@ -563,6 +571,7 @@ pub fn shr_exact<const SHIFT: u32, const RES: u32>(self) -> Option<Bounded<T, RE
>      ///
>      /// assert_eq!(v_shifted.get(), 0xff00);
>      /// ```
> +    #[inline]
>      pub fn shl<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
>          const_assert!(RES >= N + SHIFT);
>  
> 
> base-commit: 2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64
> -- 
> 2.56.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rust: num: Add inline annotation to Bounded methods
  2026-10-19 14:51 ` Yury Norov
@ 2026-10-19 15:07   ` Alexandre Courbot
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2026-10-19 15:07 UTC (permalink / raw)
  To: Yury Norov
  Cc: Link Mauve, Yury Norov, rust-for-linux, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Onur Özkan, linux-kernel

On Mon Oct 19, 2026 at 11:51 PM JST, Yury Norov wrote:
> On Mon, Oct 19, 2026 at 12:57:38PM +0200, Link Mauve wrote:
>> It was previously generating a ton of very short functions, such as
>> <<kernel::num::bounded::Bounded<u32, 32>>::shr::<17, 15>> or
>> <<kernel::num::bounded::Bounded<u16, 16>>::new::<16384>> on use.
>> 
>> These are obviously better inlined, although I wasn’t completely sure
>> between #[inline] and #[inline(always)], but the former worked fine in
>> my setup so I went for it, we can always adjust it later.
>> 
>> Signed-off-by: Link Mauve <linkmauve@linkmauve.fr>
>
> Reviewed-by: Yury Norov <ynorov@nvidia.com>
>
> Do you want me to take the patch with the bitops branch, or you've
> a better route in mind?

So far all the patches touching the `bounded` module have been merged
through the rust tree.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-10-19 15:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 10:57 [PATCH] rust: num: Add inline annotation to Bounded methods Link Mauve
2026-10-19 11:09 ` Gary Guo
2026-10-19 14:07 ` Alice Ryhl
2026-10-19 14:25 ` Alexandre Courbot
2026-10-19 14:51 ` Yury Norov
2026-10-19 15:07   ` Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®