Performance issues with using Slice indexing buffer[4..20]?


My application handling and processing a lot of binary data using Vec<u8> and seems performance is quite OK, but I’ve notices very weird thing!
Example when I’m doing something like this


It performs about 30% slower than when I’m doing without indexing

// it performs better even if buffer is 2x larger than Sliced part

Just for interest I’ve compared similar thing with C++ and it seems that there is absolutely no difference with index or without index in that implementation.

So just curious what is actually happening when I’m doing &buffer[4..2000] ?
What should I do to get most efficient way of getting reference of partial Slice ?


The problem is extend, not slicing. A slice in Rust is just pointer + length behind the scene.

Generic extend implementation reallocates many times while extending the collection. The performance difference is due to Vec::extend being specialized for Vec so that it can reserve necessary capacity first.

You can workaround this by using Vec::extend_from_slice, but upcoming Rust 1.14 release includes specialization of extend also for slices (rust#37094), which will fix this problem. This release also includes rust#37306 that enables more preallocations.