Array!T and find are slow

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 14 10:42:21 PDT 2014


On Wednesday, 14 May 2014 at 15:42:13 UTC, Damian Day wrote:
> On Wednesday, 14 May 2014 at 14:54:57 UTC, David Nadlinger 
> wrote:
>
>> Could you post a short benchmark snippet explicitly showing 
>> the problem?
>
> Benchmark found here:
> http://dpaste.dzfl.pl/0058fc8341830

FYI, that implementation is wrong: Array.Range keeps `_a` and 
`_b`, which represents the "internal" bounds of the payload 
proper (and not "low" and "length"). You want something along the 
lines of:

Range searchForward(E)(E needle)
	if (isImplicitlyConvertible!(E, T))
{
	   assert(_data.refCountedStore.isInitialized);

    	auto haystack = _data._payload[_a .. _b];
     immutable len = _b - _a;

	   for (size_t index = 0; index < len; ++index)
	   {
		      if (haystack[index] is needle)
	      	   	return Range(this, _b - len, _b);
	   }
	
	   return Range(this, _b, _b);
}

But even then, as I suggested above, while I think having 
searchForward could improve the situation, implementing it "in 
terms of" find would be better: This way, you'd still benefit 
from some of the highly optimized cases in find.


More information about the Digitalmars-d-learn mailing list