Skip to content

Inefficient code generated from ToString::to_string() for bool #106611

Closed
@Kmeakin

Description

@Kmeakin

The ToString::to_string() implementation for bool generates over 700 lines of LLVM IR and 500 lines of AArch64 assembly!
Using format! with Display or Debug is also unnecessarily inefficient.
https://godbolt.org/z/3M4jGYE9d

// Worst
pub fn to_string(b: bool) -> String {
    b.to_string()
}

// Better
pub fn format_display(b: bool) -> String {
    format!("{b}")
}

pub fn format_debug(b: bool) -> String {
    format!("{b:?}")
}

// Best
pub fn if_then_else(b: bool) -> String {
    if b {
        "true".into()
    } else {
        "false".into()
    }
}

Version

rustc 1.68.0-nightly (ee0412d1e 2023-01-07)
binary: rustc
commit-hash: ee0412d1ef81efcfabe7f66cd21476ca85d618b1
commit-date: 2023-01-07
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions