Adding a read primitive to ranges
Freddy via Digitalmars-d
digitalmars-d at puremagic.com
Tue May 5 12:36:09 PDT 2015
> How would it be more optimal? As I said, if you pass in
> `file.byChunks(some_amount).joiner`, this will still read the
> file in large chunks. It's less optimal now because `read` has
> to allocate an array on every call (easily avoidable by passing
> in a reusable buffer, but still).
>
> Equivalent code with ranges:
>
> auto range = file.byChunks(4096).joiner;
> ubyte[] data = range.take(VERY_BIG_NUMBER).array;
> ubyte[] other_data =
> range.take(OTHER_VERY_BIG_NUMBER).array;
The range solution copies from a buffer to a newly allocated
array many times, doing many system calls.
The read(stream) solution allocates a new array and does one
system call.
Sorry for the miscommunication.
More information about the Digitalmars-d
mailing list