Skip to content

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

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

Closed
alexcrichton opened this issue Dec 17, 2013 · 3 comments
Closed

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

alexcrichton opened this issue Dec 17, 2013 · 3 comments
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@alexcrichton
Copy link
Member

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()

@Aatch
Copy link
Contributor

Aatch commented Apr 15, 2014

Looks like the write to the length in push when copying the elements over is making LLVM wary and preventing some optimisation opportunities.

Better alias analysis would help here.

@alexcrichton
Copy link
Member Author

Closed by #13539, thanks @Aatch!

@alexcrichton
Copy link
Member Author

(as in, the Vec case was fixed, and the ~[u8] case will become irrelevant in the near future)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

2 participants