Change representation of dynamic arrays?

Chris Miller chris at dprogramming.com
Fri Oct 19 22:53:46 PDT 2007


On Fri, 19 Oct 2007 23:03:02 -0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Currently, arrays are represented under the hood as:
>
> 	size_t lengthOfArray;
> 	void* ptrToStartOfArray;
>
> Which works out reasonably well. The problem is if you want to use array  
> types as the basis of iterators, and you want to step through the array.  
> There's no escaping it being two operations:
>
> 	decrement the length
> 	increment the pointer
>
> This puts a brick in any fast implementation of iterators. To fix that,  
> we can change the representation to:
>
> 	void* ptrToStartOfArray;
> 	void* ptrPastEndOfArray;
>
> Then there's just one increment. Some tests show this can improve loop  
> performance by up to 70%.
>

It sounds interesting...

I wonder what kind of iterators you have in mind. Why couldn't the  
iterators just hold start and end pointers? It would only need to be  
calculated when creating an iterator from a non-iterator. Or perhaps you  
want a slice to be an iterator itself, so you could just do slice++ and it  
slices out the first element.



More information about the Digitalmars-d mailing list