Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I initialize an `Arc<[u8; 65536]>` efficiently?

I'm writing an application creating Arc objects of large arrays:

use std::sync::Arc

let buffer: Arc<[u8; 65536]> = Arc::new([0u8; 65536]);

After profiling this code, I've found that a memmove is occurring, making this slow. With other calls to Arc::new, the compiler seems smart enough to initialize the stored data without the memmove.

Believe it or not, the above code is faster than:

use std::sync::Arc;
use std::mem;

let buffer: Arc<[u8; 65536]> = Arc::new(unsafe {mem::uninitialized})

Which is a bit of a surprise.

Insights welcome, I expect this is a compiler issue.

like image 567
Greg Avatar asked Aug 14 '15 18:08

Greg


1 Answers

Yeah, right now, you have to lean on optimizations, and apparently, it isn't doing it in this case. I'm not sure why.

We are also still working on placement new functionality, which will be able to let you explicitly tell the compiler you want to initialize this on the heap directly. See https://github.com/rust-lang/rfcs/pull/809 (and https://github.com/rust-lang/rfcs/pull/1228 which proposes changes that are inconsequential for this question). Once this is implemented, this should work:

let buffer: Arc<_> = box [0u8; 65536];
like image 130
Steve Klabnik Avatar answered Oct 23 '22 22:10

Steve Klabnik