Skip to content

Binary size regression of panic=immediate-abort since nightly-2025-09-24 #147257

Closed

Description

@FooIbar

-Cpanic=immediate-abort was added in #146317 to replace -Zbuild-std-features=panic_immediate_abort.
However, we've observed a double-digit binary size regression with that:

$ cargo +nightly-2025-09-23 bloat -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort --target $(rustc --print host-tuple) --features jvm --release --crates
[...]
 File  .text     Size Crate
15.9%  23.0%  86.5KiB core
 3.2%   4.6%  17.3KiB std
[...]
$ RUSTFLAGS="-Zunstable-options -Cpanic=immediate-abort" cargo +nightly-2025-09-24 bloat -Zbuild-std=std,panic_abort --target $(rustc --print host-tuple) --features jvm --release --crates
[...]
 File  .text     Size Crate
15.9%  21.8% 116.5KiB core
11.3%  15.5%  82.6KiB std
 5.1%   7.0%  37.4KiB gimli
 1.7%   2.3%  12.5KiB addr2line
 1.5%   2.0%  10.9KiB miniz_oxide
[...]

The size of core and std is notably increased, with gimli, miniz_oxide, and addr2line starting to show up.

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Activity

  1. added
    C-bugCategory: This is a bug.
    regression-untriagedUntriaged performance or correctness regression.
    on Oct 2, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    and removed
    regression-untriagedUntriaged performance or correctness regression.
    on Oct 2, 2025
  3. bjorn3 commented on Oct 2, 2025

    @bjorn3
    Member
  4. FooIbar commented on Oct 2, 2025

    @FooIbar
    Author

    For reference building without -Cpanic=immediate-abort:

    $ cargo +nightly-2025-09-24 bloat -Zbuild-std=std,panic_abort --target $(rustc --print host-tuple) --features jvm --release --crates
    [...]
     File  .text     Size Crate
    15.5%  22.5% 126.2KiB core
    11.2%  16.3%  91.4KiB std
     4.7%   6.8%  37.9KiB gimli
     1.5%   2.2%  12.6KiB addr2line
     1.4%   2.1%  11.8KiB miniz_oxide
    [...]
    

    Both core and std are increased, while gimli, miniz_oxide, and addr2line stay roughly the same.
    It seems that -Cpanic=immediate-abort does work to some degree.

    Update:

    I wonder if the panic = "abort" in https://github.com/FooIbar/EhViewer/blob/085e0938cba98a5e961431660d137d492b41c54a/app/src/main/rust/Cargo.toml#L37 overrides the -Cpanic=immediate-abort.

    I also tried removing the panic = "abort" but that didn't work.

  5. added
    I-heavyIssue: Problems and improvements with respect to binary size of generated code.
    on Oct 2, 2025
  6. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-panicArea: Panicking machinery
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 2, 2025
  7. saethlin commented on Oct 2, 2025

    @saethlin
    Member

    Where do I run those commands?

    ╭ ➜ ben@archlinux:/tmp/EhViewer/app/src/main/rust
    ╰ ➤ RUSTFLAGS="-Zunstable-options -Cpanic=immediate-abort" cargo +nightly bloat -Zbuild-std=std,panic_abort --target=aarch64-linux-android --all-features --release --crates
    ...
       Compiling ehviewer_rust v0.0.0 (/tmp/EhViewer/app/src/main/rust)
        Finished `release` profile [optimized] target(s) in 38.98s
    Error: only 'bin', 'dylib' and 'cdylib' crate types are supported.
    
  8. AstralZebulon commented on Oct 2, 2025

    @AstralZebulon

    We had a similar issue with our baremetal ARM executable (binary grew 25% going from nightly-2025-09-23 to nightly-2025-09-24 and after converting from the build-std-feature to rustflags with the unstable panic strategy). But it turned out to be an issue with Cargo and how we placed those flags into config.toml files for consumption. Don't know if that will help for your Android app builds (it is unclear to me how the Corrosion tool works). We thought we had the exact same experience, but it turned out to be a bit of Cargo user error.

  9. FooIbar commented on Oct 3, 2025

    @FooIbar
    Author

    For cargo bloat to work with https://github.com/FooIbar/EhViewer/blob/085e0938cba98a5e961431660d137d492b41c54a/app/src/main/rust/Cargo.toml:

     [lib]
    -crate-type = ["staticlib"]
    +crate-type = ["cdylib"]
     [profile.release]
    -strip = true

    It turns out it's not specific to Android, so I tweaked those commands so you don't need to set up the NDK:

    cargo +nightly-2025-09-23 bloat -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort --target $(rustc --print host-tuple) --features jvm --release --crates
    
    RUSTFLAGS="-Zunstable-options -Cpanic=immediate-abort" cargo +nightly-2025-09-24 bloat -Zbuild-std=std,panic_abort --target $(rustc --print host-tuple) --features jvm --release --crates
    

    I've also updated the numbers in the comments above with the updated commands.

  10. saethlin commented on Oct 3, 2025

    @saethlin
    Member

    I've minimized the crate to this:

    [package]
    name = "ehviewer_rust"
    version = "0.0.0"
    edition = "2024"
    
    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    jni = { version = "0.21" }
    jni_fn = { version = "0.1" }
    anyhow = "1.0"
    
    [profile.release]
    panic = "abort"
    lto = true
    codegen-units = 1
    use anyhow::Result;
    use jni::JNIEnv;
    use jni::objects::JByteBuffer;
    use jni::sys::jint;
    use jni_fn::jni_fn;
    use std::ptr::slice_from_raw_parts_mut;
    
    #[jni_fn("com.hippo.ehviewer.client.parser.UserConfigParser")]
    pub fn parseFavCat(mut env: JNIEnv, buffer: JByteBuffer) -> jint {
        dummy(&mut env, buffer)
    }
    
    fn dummy<'local>(env: &JNIEnv, buffer: JByteBuffer<'local>) -> jint {
        match deref_mut_direct_bytebuffer(env, buffer) {
            Ok(_) => 0,
            _ => 1,
        }
    }
    
    fn deref_mut_direct_bytebuffer<'local>(
        env: &JNIEnv,
        buffer: JByteBuffer<'local>,
    ) -> Result<&'local mut [u8]> {
        let ptr = env.get_direct_buffer_address(&buffer)?;
        let cap = env.get_direct_buffer_capacity(&buffer)?;
    
        // SAFETY: jni contract, buffer must alive through native call.
        Ok(unsafe { &mut *slice_from_raw_parts_mut(ptr, cap) })
    }

    At which point it's pretty clear to me that the problem is the anyhow backtrace code. I'm going to minimize a bit more, but at this point I'm a bit confused that the backtrace code optimized away before.

  11. saethlin commented on Oct 3, 2025

    @saethlin
    Member

    Yep, minimizes to this:

    #[unsafe(no_mangle)]
    pub extern "C" fn entrypoint() {
        anyhow::Error::msg("ouch");
    }
  12. saethlin commented on Oct 3, 2025

    @saethlin
    Member

    I think that the problem here is that the standard library crate std has an on-by-default feature called backtrace which is disabled when -Zbuild-std-features=panic_immediate_abort is passed. This overriding of default features is documented: https://doc.rust-lang.org/cargo/reference/unstable.html#build-std-features

    @FooIbar Can you confirm that adding -Zbuild-std-features= (which looks very silly) fixes this size regression?

  13. FooIbar commented on Oct 3, 2025

    @FooIbar
    Author

    @FooIbar Can you confirm that adding -Zbuild-std-features= (which looks very silly) fixes this size regression?

    Ah, true! Adding -Zbuild-std-features= does fix the regression.
    I think I've seen this trick somewhere (edit: found it google/bloaty#110 (comment)) before but totally forgot it.
    Feel free to close this issue.

  14. saethlin commented on Oct 3, 2025

    @saethlin
    Member

    Since you're already using immediate-aborting panics, you might want to pass -Zbuild-std-features=optimize_for_size. Might make your code even smaller.

  15. FooIbar commented on Oct 3, 2025

    @FooIbar
    Author

    Yeah I know that, but I'd prefer speed over size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-panicArea: Panicking machineryC-bugCategory: This is a bug.I-heavyIssue: Problems and improvements with respect to binary size of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions