Dynamic array comparison optimization: check for equal pointers?

Johan j at j.nl
Tue Jun 2 22:05:06 UTC 2020


On Tuesday, 2 June 2020 at 21:27:44 UTC, Dominikus Dittes Scherkl 
wrote:
> On Tuesday, 2 June 2020 at 19:38:04 UTC, Johan wrote:
>> Hi all,
>>   I thought that we optimized dynamic array comparison with a 
>> check to see if the pointers are equal, but I don't see it in 
>> our array comparison implementation in druntime. Quick 
>> checking of glibc memcmp also showed that there is no short 
>> circuit for when the pointers of the slices are equal.
>>
>> I was expecting something like:
>> ```
>>   if ( sizes are not equal )
>>      return false;
>>   if ( pointers are equal  )
>>      return true;
>>   return memcmp;
>> ```
>>
>> I am surprised that memcmp itself does not implement the 
>> pointers are equal check.
> I also would expect this kind of optimization in memcmp and not 
> in the functions using it. Anything at higher level should be 
> unaware of pointers wherever possible.

Pointers are nothing to be scared of.

> And how do you know this optimization is NOT in memcmp? It's 
> extern C and the source may be not available - have you 
> examined it?

That's what I wrote, yes.

> There are many implementations of memcmp available - maybe on 
> you're platform the cost for this extra check is too high? 
> (especially given the very low probability that it is ever 
> called with src==dst).

glibc is a very commonly used library for memcmp. It does many 
other checks before starting the comparison (e.g. optimizing for 
pointer alignment), so the cost of the extra check is perhaps not 
even measurable.

-Johan




More information about the Digitalmars-d mailing list