Skip to contentSkip to conversation

rust: bitfield: mark `Debug` impl as `#[inline]`

PATCH Applied Gary Guo 4 participants 7 messages Jun 11 – Jun 16

Subsystems (2)
lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH 1/1 rust: bitfield: mark `Debug` impl as `#[inline]`

From: Gary Guo <[email protected]>

A Debug impl is for debugging and is normally not used, and therefore should ideally not be code-generated unless used. However, Rust has no way of knowing if a dependent crate is going to use the trait impl or not, so unless it is marked as #[inline], it will be code-generated in the defining crate (as it is not generic).

Mark the impl generated by bitfield macro #[inline], so they do not stay in the binary unless used.

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

---
 rust/kernel/bitfield.rs | 1 +
 1 file changed, 1 insertion(+)
rust/kernel/bitfield.rs +1 -0
index 554a5a2ff0ab..35ede53f2b8e 100644
@@ -535,6 +535,7 @@ const fn [<__with_ $field>](
     // `Debug` implementation.
     (@debug $name:ident { $($field:ident;)* }) => {
         impl ::kernel::fmt::Debug for $name {
+            #[inline]
             fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result {
                 f.debug_struct(stringify!($name))
                     .field("<raw>", &::kernel::prelude::fmt!("{:#x}", self.inner))

base-commit: 7f07865bda5f5770d2f7927bc76cb2b4acd46074

Signature · 1 line
2.54.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Gary Guo

From: Gary Guo <[email protected]>

A Debug impl is for debugging and is normally not used, and therefore should ideally not be code-generated unless used. However, Rust has no way of knowing if a dependent crate is going to use the trait impl or not, so unless it is marked as #[inline], it will be code-generated in the defining crate (as it is not generic).

Quoted 6 lines

Mark the impl generated by bitfield macro #[inline], so they do not stay in the binary unless used.

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Gary Guo
Quoted 8 lines · Gary Guo

From: Gary Guo <[email protected]>

A Debug impl is for debugging and is normally not used, and therefore should ideally not be code-generated unless used. However, Rust has no way of knowing if a dependent crate is going to use the trait impl or not, so unless it is marked as #[inline], it will be code-generated in the defining crate (as it is not generic).

Quoted 6 lines

Mark the impl generated by bitfield macro #[inline], so they do not stay in the binary unless used.

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

Ouch, well-spotted. Thanks for this.

#3 lore raw
Quoted 12 lines · Alexandre Courbot
Quoted 11 lines · Gary Guo

From: Gary Guo <[email protected]>

A Debug impl is for debugging and is normally not used, and therefore should ideally not be code-generated unless used. However, Rust has no way of knowing if a dependent crate is going to use the trait impl or not, so unless it is marked as #[inline], it will be code-generated in the defining crate (as it is not generic).

Mark the impl generated by bitfield macro #[inline], so they do not stay in the binary unless used.

Quoted 7 lines
Quoted 3 lines

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

Ouch, well-spotted. Thanks for this.

Btw, should this be

Fixes b7b8b4ccdad4 ("rust: extract `bitfield!` macro from `register!`")

?

#5 lore raw
In reply to Gary Guo
Quoted 2 lines · Gary Guo

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

That's definitely something :)

I wonder if we could lint this for fmt bits.

Cheers,
Miguel

#4 lore raw
In reply to Alexandre Courbot
Quoted 6 lines · Alexandre Courbot

Btw, should this be

Fixes b7b8b4ccdad4 ("rust: extract `bitfield!` macro from `register!`")

?

I think it is reasonable either way. One can see it as a "size improvement" or as a "fix" if it was unintentional to not have the annotation.

It also depends on the magnitude of the size reduction -- if it were very low, would we see it as a fix?

Cheers,
Miguel

Applied
#6 lore raw
In reply to Gary Guo
Quoted 9 lines · Gary Guo

From: Gary Guo <[email protected]>

A Debug impl is for debugging and is normally not used, and therefore should ideally not be code-generated unless used. However, Rust has no way of knowing if a dependent crate is going to use the trait impl or not, so unless it is marked as #[inline], it will be code-generated in the defining crate (as it is not generic).

Quoted 6 lines

Mark the impl generated by bitfield macro #[inline], so they do not stay in the binary unless used.

This reduces nova-core.o .text by 17% (from 151922 bytes to 125676 bytes).

Applied to rust-fixes -- thanks!

I am going to pick a couple actual fixes in the coming days, so I decided to pick this one too for the ride since it is trivial. I may still rebase though.

Cheers,
Miguel