Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl fmt::Display for u32 compiles to large binary #118940

Open
StackOverflowExcept1on opened this issue Dec 14, 2023 · 1 comment
Open

impl fmt::Display for u32 compiles to large binary #118940

StackOverflowExcept1on opened this issue Dec 14, 2023 · 1 comment
Labels
I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@StackOverflowExcept1on
Copy link
Contributor

StackOverflowExcept1on commented Dec 14, 2023

My target is wasm32-unknown-unknown but I think it can be applied to any embedded systems. I just want to define panic handler, but formatting core::panic::Location costs amlost 3 KiB in binary. This is because inefficient impl Display for u32. Also see: https:/dtolnay/itoa

[package]
name = "demo"
version = "0.1.0"
edition = "2021"

[lib]
name = "demo"
crate-type = ["cdylib"]

[dependencies]
arrayvec = { version = "0.7.4", default-features = false }

[profile.release]
opt-level = "z"
codegen-units = 1
lto = true
strip = true
#![no_std]
#![feature(panic_info_message)]

#[allow(improper_ctypes)]
extern "C" {
    pub fn sc_panic(msg: &str) -> !;
}

#[panic_handler]
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
    use core::fmt::Write;

    let mut msg = arrayvec::ArrayString::<1024>::new();

    // 2_009 bytes binary:
    /*let _ = write!(&mut msg, "{}", unsafe {
        panic_info.message().unwrap_unchecked()
    });*/

    // 5_076 bytes binary:
    let _ = write!(&mut msg, "{}", unsafe {
        panic_info.location().unwrap_unchecked()
    });
    // ^^^ +3 KiB to print location in format {file}:{line}:{column}???

    // exit from smart contract with msg
    unsafe { sc_panic(&msg) }
}

#[no_mangle]
extern "C" fn entry_point() {
    panic!("msg");
}

This is mostly because this line:

f.pad_integral(is_nonnegative, "", buf_slice)

Could we have something like specialization for more simpler impl Display for $num?

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 14, 2023
@clubby789 clubby789 added the I-heavy Issue: Problems and improvements with respect to binary size of generated code. label Dec 14, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 16, 2023
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 17, 2023
@GKFX
Copy link
Contributor

GKFX commented Apr 27, 2024

At present, both {} and (e.g.) {:06} pass the integer to Argument::new_display, which returns a pointer to Display::fmt unconditionally. new_display doesn't see whether this has extra flags or not so it has no opportunity to return a simplified formatting function. You would have to change format_args to give new_display visibility of the Placeholder struct to allow a specialized formatting function to be returned instead. (Edit: a new_simple_display was tried in #104525 but not merged, with tracking issue #99012.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants