X[]==Y[] is 7X slower than it should be -- why?

Bruce Adams tortoise_74 at yeah.who.co.uk
Fri Jun 27 00:35:02 PDT 2008


On Fri, 20 Jun 2008 18:40:25 +0100, Jarrett Billingsley  
<kb3ctd2 at yahoo.com> wrote:

> "Don" <nospam at nospam.com.au> wrote in message
> news:g3ggq5$ui5$1 at digitalmars.com...
>
>> What's it doing? Is it doing a byte-by-byte comparison? (it shouldn't do
>> that even for strings). Is it doing bounds checking for every array
>> element?? (even with -release -O)
>
> See dmd/src/phobos/internal/adi.d  The important function here is:
>
> extern (C) int _adEq(Array a1, Array a2, TypeInfo ti)
> {
>     if (a1.length != a2.length)
>         return 0;
>
>     auto sz = ti.tsize();
>     auto p1 = a1.ptr;
>     auto p2 = a2.ptr;
>
>     if (sz == 1)
>         // We should really have a ti.isPOD() check for this
>         return (memcmp(p1, p2, a1.length) == 0);
>
>     for (size_t i = 0; i < a1.length; i++)
>     {
>         if (!ti.equals(p1 + i * sz, p2 + i * sz))
>             return 0;
>     }
>
>     return 1;
> }
>
Eeek. This is no longer just a performance issues its a proper bug. For  
user defined types
there is no guarantee that memcpy will return the same result as opEquals.

E.g. Imagine a class with an id field to identify specific instances.

class foo
{
    int objectId;
    int value;
}

memcmp will compare objectId whereas your op equals would only compare the  
value.
Worse than that memcmp will fail for any object with a pointer.

Why has no-one screamed about this before?

Regards,

Bruce.





More information about the Digitalmars-d mailing list