Skip to content

&[u8].to_owned() does not compile to a memcpy #11015

Closed
@alexcrichton

Description

@alexcrichton

I imagine that something is happening along the way which is preventing this from becoming a memcpy.

~ $ cat foo.rs                                
use std::vec;

fn main() {
    let a = vec::from_fn(50 * 1024 * 1024, |_| 0u8);

    for _ in range(0, 30) {
        a.to_owned();
    }
}
~ $ cat bar.rs                                      
use std::vec;

fn main() {
    let a = vec::from_fn(50 * 1024 * 1024, |_| 0u8);

    for _ in range(0, 30) {
        let mut b = vec::with_capacity::<u8>(50 * 1024 * 1024);
        unsafe {
            vec::raw::set_len(&mut b, 50 * 1024 * 1024);
        }
        vec::bytes::copy_memory(b, a, a.len());
    }
}
~ $ rustc -O foo.rs                             
~ $ rustc -O bar.rs                                 
~ $ time ./foo                 
./foo  3.41s user 0.20s system 99% cpu 3.624 total
~ $ time ./bar                                        
./bar  0.38s user 0.20s system 97% cpu 0.593 total

The memcpy is a 6x speedup compared to to_owned()

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions