Dynamic array comparison optimization: check for equal pointers?

Dominikus Dittes Scherkl dominikus at scherkl.de
Tue Jun 2 21:27:44 UTC 2020


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.
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? 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).


More information about the Digitalmars-d mailing list