Dynamic array comparison optimization: check for equal pointers?

Stanislav Blinov stanislav.blinov at gmail.com
Tue Jun 2 23:35:39 UTC 2020


On Tuesday, 2 June 2020 at 22:44:24 UTC, Steven Schveighoffer 
wrote:

> Let's assume LDC knows how to optimize this. How do you think 
> it would do so? I'd imagine very similarly to how Johan wrote 
> it.
>
> What makes you think LDC can't optimize out the check if 
> there's a better way? Then the check is there in case the 
> optimizer you are dealing with is going to call an opaque 
> memcmp in all cases.

That actually is irrelevant. Whether it gets inlined, whether 
even it's loop is unrolled by the compiler - that is all beside 
the point. If you hardcode the check in the library, that check 
is there for everyone using that library. On all hardware and for 
all data sizes. Irrespective of whether it can, on a given 
platform with a given array size, be actually faster to just do 
the call/loop/etc. than to do a check first. Irrespective of what 
a given compiler is capable of. There cannot be a universal 
optimization here.

Users can always write a compare_arrays_ptrcheck if so required, 
but they cannot "unwrite" druntime. Same goes for actual memcmp. 
So let them do the minimal work required, and let users make 
higher-level decisions at higher level.

If a program is attempting to "compare" identical memory ranges 
so often as to warrant a test - it's up to that program to deal 
with it, not the whole community.


More information about the Digitalmars-d mailing list