Dynamic array comparison optimization: check for equal pointers?
Steven Schveighoffer
schveiguy at gmail.com
Wed Jun 3 00:04:43 UTC 2020
On 6/2/20 7:35 PM, Stanislav Blinov wrote:
> 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.
I'm saying the compiler can unwrite druntime, if it knows a better way.
Happens all the time with LDC, it will remove loops that it knows don't
do anything.
And the upside is that compilers that don't do the "smarter" thing will
not needlessly call memcmp.
-Steve
More information about the Digitalmars-d
mailing list