Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tuplecats opened this issue Nov 22, 2021 · 1 comment
Closed

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

tuplecats opened this issue Nov 22, 2021 · 1 comment

Comments

@tuplecats
Copy link

tuplecats commented Nov 22, 2021

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"
@tuplecats tuplecats changed the title Default::default() has bad optimization for String from 1.52.0 Default::default() has bad optimization for String since 1.52.0 Nov 22, 2021
@hkratz
Copy link
Contributor

hkratz commented Nov 22, 2021

String::default() just calls String::new(), so this is a duplicate of #86106.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants