Skip to content

bad performance of char literal to_string() vs str literal to_string() #73462

Closed
@matthiaskrgr

Description

@matthiaskrgr

I was quite surprised when I found out that

pub fn conv() -> String {
    'a'.to_string()
}

generates several times the instruction count of

pub fn conv() -> String {
    "a".to_string()
}

(according to godbold).
Checking the (debug) build size of println!("{}", "a".to_string()); vs println!("{}", 'a'.to_string()); revealed the str variant being ~20kb lighter.

When doing some quick-and-dirty benchmarks, the str variant was more than 2x faster than the char-variant. benchmark code

test tests::bench_char_to_string ... bench:      39,890 ns/iter (+/- 42,078)
test tests::bench_str_to_string  ... bench:      14,422 ns/iter (+/- 2,427)

Is this something that could be optimized?

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions