last day (15 days later) » 

10:43 PM
0
Q: Copy the end of a slice to its start

FabimaruAs a newbie to Rust, I am struggling to implement a simple problem in the rust-way. I would like to process from a file/pipe each utf-8 character, by reading chunks (slice) of N bytes (without other memory allocation than the read-buffer). Once I have processed all the characters of a chunk I re...

 
Is there something preventing you from using read_char?
I think you need to extend your code snippet. I tried out your code (with some tweaks) and it seemed to work.
 
Using read_char in a loop is 3 times slower than using the iterable ".chars()", which itself is slower than using ".lines()". I will try to extend the snippet to show the actual error.
 
FWIW, for a file with 116524419 Unicode characters, wc -m takes 39.178s and the equivalent Rust version took 40.441s. I'd expect that wc is fairly optimized.
 
Thanks to you snippet, I realized that the difference with my code is that you obtain "remaining" by doing a "clone()" of the "buffer". It works, but I don't get in what cloning a slice (which is a view, a pointer plus a length) is different than using directly "buffer.slice_from()".
 
I copied that directly from your example code: let mut remaining = buffer.clone();
 
10:43 PM
Argh, sorry! I tried to provide a short snippet/pseudo-code, but it did not reflect my real code. My original code did let mut remaining = buffer.slice_to(other_stuff); (buffer being a slice) and not let mut remaining = buffer.clone();. I thought it would be equivalent. I think I get the different now: the clone is not bound to the lifetime of its parent, and seems dangerous on slices [[is.gd/olxYw2]](is.gd/olxYw2])
Concerning your example with reader.chars(), I think that the bottleneck is the disk. On my machine, reading on a pipe that generate 1Go/s, I get 55Mo/s with reader.chars(), and the double with (lines)[[is.gd/h0n04C]](is.gd/h0n04C]) FWIW
 

  last day (15 days later) »