rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Allow opting out of specific parts of core crate (e.g. core::fmt)

Open RealSuperRyn opened this issue 8 months ago • 11 comments

In embedded systems development, there's usually a small maximum binary size. (e.g. 8KiB). The size of core::fmt when compiled into the executable is too big for this.

Take a freestanding, x86_64-unknown-none, panic="abort" executable with 3 functions: A panic handler, a function named panik, and the _start function. In both the panic and panik functions, the only expression is loop {unsafe {asm!("hlt");}}. In the _start function, the only expression is either panic!(); or panik();

If you run cargo bloat --release --target=x86_64-unknown-none on the program's source when the only expression in _start is panic!();, the resulting executable size is 665.4KiB, which is 83 times the hypothetical size limit of our executable.

However, if you run the same command when the function in _start used is panik();, the executable size is now only 1.8KiB, which is only 25% of the limit: 332 times smaller.

It would be very nice if we could opt out of using parts of core like core::fmt instead of struggling to avoid compiling it. When opted out of part of core, the compiler will output an error (E0412) upon compiling something that is in or depends on the opted out portion, instead of silently compiling it in. My suggestion for the syntax is unuse core::foo::bar;, though other syntax is fine.

RealSuperRyn avatar Jun 24 '24 16:06 RealSuperRyn