Skip to content

Default::default() has bad optimization for String since 1.52.0 #91142

Closed
@tuplecats

Description

@tuplecats

Since 1.52.0 Rust copy byte from static memory for creating String by Default::default(). But when uses "".to_string() it doesn't happen.

Additional instruction appears when use Default::default() (godbolt)

pub fn string_default() -> String {
    Default::default()
}

pub fn string_to_string() -> String {
    "".to_string()
}
example::string_default:
        mov     rax, rdi
        mov     rcx, qword ptr [rip + .L__unnamed_1]
        xorps   xmm0, xmm0
        movups  xmmword ptr [rdi + 8], xmm0
        mov     qword ptr [rdi], rcx
        ret

example::string_to_string:
        mov     rax, rdi
        xorps   xmm0, xmm0
        movups  xmmword ptr [rdi + 8], xmm0
        mov     qword ptr [rdi], 1
        ret

.L__unnamed_1:
        .asciz  "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions