Adding a read primitive to ranges
Alex Parrill via Digitalmars-d
digitalmars-d at puremagic.com
Tue May 5 08:09:11 PDT 2015
On Tuesday, 5 May 2015 at 01:28:03 UTC, Freddy wrote:
> Wait, Bad example,
> ----
> void func(R)(R range){//expects range of ubyte
> ubyte[] data=range.read(VERY_BIG_NUMBER);
> ubyte[] other_data=range.read(OTHER_VERY_BIG_NUMBER);
> }
> ----
> which would be more optimal for a file but still works for
> other ranges, compared to looping though the ranges read
> appending to data.
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;
More information about the Digitalmars-d
mailing list