protocol for using InputRanges

Johannes Pfau nospam at example.com
Fri Mar 28 11:14:39 PDT 2014


Am Fri, 28 Mar 2014 17:22:26 +0000
schrieb "w0rp" <devw0rp at gmail.com>:

> 
> I think a key is to offer something with gives you chunks at a 
> time right at the top, and the use .joiner on that. I read files 
> this way currently.
> 
> auto fileByteRange = File("something").byChunk(chunkSize).joiner;
> 

byChunk is implemented on top of the file rawRead API though, and
that's a stream API ;-)
As said before implementing ranges on top of streams is fine, but if you
want ranges to replace streams as the lowest level interface you'll
either suffer from performance issues or you'll have to extend the
range interface and effectively make it a stream interface. (For example
byChunk doesn't offer a way to provide a buffer. I'd expect a low level
API to offer this, but it'll complicate range API a lot. File.rawRead
on the other hand provides exactly that and you can implement byChunk
on top of rawRead easily. The other way round is not as easy).

BTW: If this code performs well of course depends what you with that
fileByteRange range. For example if you only read the complete file into
a memory buffer joiner would reduce performance significantly.

> I believe this to be a very good way to get good performance 
> without losing the functionality of std.algorithm.

Yes, that's exactly how ranges/streams should interface, there's no
real problem for users.
stream.getSomeRange().rangeAPICalls....


More information about the Digitalmars-d mailing list