Skip to content

Conversation

@labbott
Copy link
Collaborator

@labbott labbott commented May 15, 2020

Text formatting is very expensive. With a few changes we can save a good chunk of text:

Before:

$ arm-none-eabi-size target/thumbv7em-none-eabihf/release/demo
   text	   data	    bss	    dec	    hex	filename
  26284	      0	     48	  26332	   66dc	target/thumbv7em-none-eabihf/release/demo`

After:

$ arm-none-eabi-size target/thumbv7em-none-eabihf/release/demo
   text	   data	    bss	    dec	    hex	filename
  16392	      0	     48	  16440	   4038	target/thumbv7em-none-eabihf/release/demo

This doesn't actually change anything to use ufmt but it sets up a few things so we can eventually convert klog over to something that uses ufmt

bloat output afterwards:

File  .text    Size             Crate Name
0.9%  25.4%  3.5KiB              kern syscall_entry
0.2%   5.5%    770B               std core::fmt::Formatter::pad
0.2%   4.3%    602B               std core::fmt::Formatter::pad_integral
0.1%   3.8%    540B              kern kern::startup::safe_start_kernel
0.1%   3.6%    504B              kern kern::syscalls::deliver
0.1%   3.2%    456B               std core::fmt::write
0.1%   3.2%    452B              kern kern::startup::start_kernel
0.1%   2.9%    404B              kern <kern::arch::arm_m::Mmfsr as core::fmt::Debug>::fmt
0.1%   2.7%    384B              kern kern::syscalls::borrow_lease
0.1%   2.3%    328B              kern DefaultHandler
0.1%   1.8%    252B               std core::fmt::num::imp::<impl core::fmt::Display for u32>::fmt
0.1%   1.5%    212B panic_semihosting <&T as core::fmt::Display>::fmt
0.1%   1.5%    210B               std core::fmt::num::<impl core::fmt::LowerHex for i32>::fmt
0.1%   1.5%    208B              kern kern::arch::arm_m::reinitialize
0.0%   1.3%    180B              kern mem_manage_fault
0.0%   1.2%    172B              kern SysTick
0.0%   1.2%    168B              kern kern::umem::safe_copy
0.0%   1.1%    158B               std __aeabi_memset4
0.0%   1.1%    156B              kern kern::arch::arm_m::start_first_task
0.0%   1.1%    156B              kern kern::task::select
1.1%  29.5%  4.1KiB                   And 78 smaller methods. Use -n N to show more.
3.7% 100.0% 13.8KiB                   .text section size, the file size is 369.8KiB

labbott added 2 commits May 15, 2020 14:17
The existing assert macro pretty prints the expression as part of
the panic message. This is useful but also very expensive in
terms of text size. Switch to a custom `uassert` macro which
prints out a generic error message instead. We still get the
file and line number for debugging.

There's not a nice way to be able to do a custom macro for
`debug_assert` but given the lack of focus on a debug
configuration, remove those asserts.

TODO: Need to shove these macros in another macro crate
explicit_panic is called from tasks when panicking. It prints out
the message from userspace as well as the task that is panicking.
This ends up being expensive for two reasons:

1) If the message isn't fully utf-8, we have to call fmt::Debug on
every char. This function is very expensive (almost 1.5K of code
size) due to validation.

2) The address of the task that panicked is printed out using {:p},
which isn't available in the smaller ufmt library. The physical
address of the task doesn't tell us that much more than the index.

Account for both of these cases by only printing the task index and
not printing the message if it's not utf-8.
($s:expr, $($tt:tt)*) => { let _ = cortex_m_semihosting::hprintln!($s, $($tt)*); };
}

macro_rules! uassert {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't look architecture specific, we could probably move them into kern/src/lib.rs.

@cbiffle cbiffle merged commit 710b99d into master Jun 9, 2020
@mx-shift mx-shift deleted the ufmt_may15 branch October 19, 2021 20:46
@wesolows wesolows mentioned this pull request Nov 23, 2022
rusty1968 added a commit to rusty1968/hubris that referenced this pull request Sep 17, 2025
[feat] Add slave mode capabilities to Hubris I2C subsystem for MCTP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants