Why ranges don't return vectors?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Feb 28 19:42:28 PST 2013


On Fri, Mar 01, 2013 at 03:35:23AM +0100, Chris Cain wrote:
> On Friday, 1 March 2013 at 01:02:17 UTC, Piotr Szturmaj wrote:
> >Seriously, nobody will ever get performance from single-element
> >iterator/range pattern - this makes CPU stall!
> 
> There's no reason you can't do that. In fact, some ranges already do
> this. Take a look at std.stdio.File.byChunk and byLine. This isn't
> appropriate for all ranges, though. Like arrays, for instance. You
> want to be able to access single elements and do things with them,
> otherwise the abstraction is pointless.

You can make a buffered range that reads in a large chunk of data at a
time, and .front and .popFront merely move an internal pointer until it
reaches the end of the buffer, then the next buffer page is loaded in.
In this case, .front is very simple (just a pointer lookup) and will
probably be inlined by an optimizing compiler.

Just because the abstraction is reading per-element, doesn't mean the
implementation has to literally do that!


T

-- 
"Maybe" is a strange word.  When mom or dad says it it means "yes", but
when my big brothers say it it means "no"! -- PJ jr.


More information about the Digitalmars-d mailing list