Dynamic array comparison optimization: check for equal pointers?
Patrick Schluter
Patrick.Schluter at bbox.fr
Wed Jun 3 05:48:56 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. Is there a big impact of adding the
> shortcircuit check to prevent scanning memory?
memcmp() doesn't implement the check to avoid memcmp(NULL, NULL,
length) returning with 0 instead of segfaulting as it does if any
of the 2 pointers in NULL.
It's a case of consistant behaviour, either you tolerate NULL
pointers in all cases or you segfault in all cases.
More information about the Digitalmars-d
mailing list