Skip to content

Code bloat from monomorphization of methods applied to arrays #77767

Description

@glandium

The following talks about Debug::fmt, but is likely true of a lot more methods applied to arrays.
Take the following code:

fn main() {
    let a = [1, 2, 3];
    let b = [1, 2, 3, 4];
    let c = [1, 2, 3, 4, 5];
    let d = [1, 2, 3, 4, 5, 6];
    println!("{:?}", a);
    println!("{:?}", b);
    println!("{:?}", c);
    println!("{:?}", d);
}

Compiled with --release, you end up with 4 different functions for core::array::<impl core::fmt::Debug for [T; N]>::fmt (BTW, shouldn't they each have their N replaced by the actual number?). To add insult to injury, each of these have their loops unrolled, so the longer the array, the larger the function, up to 59 (!).
Almost ironically, building with -C opt-level=1 generates 4 functions that create a slice and a separate, common, fmt function for it.
This is one of these cases where you'd probably want a #[inline(never)] at the call site in core::array::<impl core::fmt::Debug for [T; N]>::fmt if that were possible.

Activity

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-arrayArea: `[T; N]`A-fmtArea: `core::fmt`C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-heavyIssue: Problems and improvements with respect to binary size of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions