Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3651

Show
Ignore:
Timestamp:
06/20/08 14:46:26 (5 months ago)
Author:
sean
Message:

Changed array compare to call TypeInfo?.equals and TypeInfo?.compare rather than looping on comparing values. This should allow optimized type-specific comparisons to be used where applicable.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/compiler/dmd/adi.d

    r2974 r3651  
    8181            auto chi = *hi; 
    8282 
    83        debug(adi) printf("lo = %d, hi = %d\n", lo, hi); 
     83            debug(adi) printf("lo = %d, hi = %d\n", lo, hi); 
    8484            if (clo <= 0x7F && chi <= 0x7F) 
    8585            { 
    86        debug(adi) printf("\tascii\n"); 
     86                debug(adi) printf("\tascii\n"); 
    8787                *lo = chi; 
    8888                *hi = clo; 
     
    104104                break; 
    105105 
    106        debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi); 
     106            debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi); 
    107107            if (stridelo == stridehi) 
    108108            { 
     
    120120            memcpy(tmp.ptr, hi, stridehi); 
    121121            memcpy(tmplo.ptr, lo, stridelo); 
    122        memmove(lo + stridehi, lo + stridelo , (hi - lo) - stridelo); 
     122            memmove(lo + stridehi, lo + stridelo , (hi - lo) - stridelo); 
    123123            memcpy(lo, tmp.ptr, stridehi); 
    124124            memcpy(hi + stridehi - stridelo, tmplo.ptr, stridelo); 
     
    336336    if (a.length > 1) 
    337337    { 
    338    dchar[] da = toUTF32(a); 
     338        dchar[] da = toUTF32(a); 
    339339        da.sort; 
    340340        size_t i = 0; 
     
    358358    if (a.length > 1) 
    359359    { 
    360    dchar[] da = toUTF32(a); 
     360        dchar[] da = toUTF32(a); 
    361361        da.sort; 
    362362        size_t i = 0; 
    363363        foreach (dchar d; da) 
    364364        {   wchar[2] buf; 
    365        auto t = toUTF16(buf, d); 
     365            auto t = toUTF16(buf, d); 
    366366            a[i .. i + t.length] = t[]; 
    367367            i += t.length; 
     
    379379{ 
    380380    debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); 
     381 
     382    if (a1.ptr == a2.ptr) 
     383        return 1; // equal 
    381384    if (a1.length != a2.length) 
    382         return 0;               // not equal 
    383     auto sz = ti.tsize(); 
    384     auto p1 = a1.ptr; 
    385     auto p2 = a2.ptr; 
    386  
    387 /+ 
    388     for (int i = 0; i < a1.length; i++) 
    389     { 
    390         printf("%4x %4x\n", (cast(short*)p1)[i], (cast(short*)p2)[i]); 
    391     } 
    392 +/ 
    393  
    394     if (sz == 1) 
    395         // We should really have a ti.isPOD() check for this 
    396         return (memcmp(p1, p2, a1.length) == 0); 
    397  
    398     for (size_t i = 0; i < a1.length; i++) 
    399     { 
    400         if (!ti.equals(p1 + i * sz, p2 + i * sz)) 
    401             return 0;           // not equal 
    402     } 
    403     return 1;                   // equal 
     385        return 0; // not equal 
     386 
     387    // We should really have a ti.isPOD() check for this 
     388    if (ti.tsize() != 1) 
     389        return ti.equals(&a1, &a2); 
     390    return memcmp(a1.ptr, a2.ptr, a1.length) == 0; 
    404391} 
    405392 
     
    424411{ 
    425412    debug(adi) printf("adCmp()\n"); 
     413 
     414    if (a1.ptr == a2.ptr) 
     415        return 0; 
    426416    auto len = a1.length; 
    427417    if (a2.length < len) 
    428418        len = a2.length; 
    429     auto sz = ti.tsize(); 
    430     void *p1 = a1.ptr; 
    431     void *p2 = a2.ptr; 
    432  
    433     if (sz == 1) 
    434     {   // We should really have a ti.isPOD() check for this 
    435         auto c = memcmp(p1, p2, len); 
    436         if (c) 
    437             return c; 
    438     } 
    439     else 
    440     { 
    441         for (size_t i = 0; i < len; i++) 
    442         { 
    443             auto c = ti.compare(p1 + i * sz, p2 + i * sz); 
    444             if (c) 
    445                 return c; 
    446         } 
    447     } 
     419 
     420    // We should really have a ti.isPOD() check for this 
     421    if (ti.tsize() != 1) 
     422        return ti.compare(&a1, &a2); 
     423    auto c = memcmp(a1.ptr, a2.ptr, len); 
     424    if (c) 
     425        return c; 
    448426    if (a1.length == a2.length) 
    449427        return 0; 
    450     return (a1.length > a2.length) ? 1 : -1; 
     428    return a1.length > a2.length ? 1 : -1; 
    451429} 
    452430 
  • trunk/lib/compiler/gdc/adi.d

    r2974 r3651  
    8787            auto chi = *hi; 
    8888 
    89        debug(adi) printf("lo = %d, hi = %d\n", lo, hi); 
     89            debug(adi) printf("lo = %d, hi = %d\n", lo, hi); 
    9090            if (clo <= 0x7F && chi <= 0x7F) 
    9191            { 
    92        debug(adi) printf("\tascii\n"); 
     92                debug(adi) printf("\tascii\n"); 
    9393                *lo = chi; 
    9494                *hi = clo; 
     
    110110                break; 
    111111 
    112        debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi); 
     112            debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi); 
    113113            if (stridelo == stridehi) 
    114114            { 
     
    126126            memcpy(tmp.ptr, hi, stridehi); 
    127127            memcpy(tmplo.ptr, lo, stridelo); 
    128        memmove(lo + stridehi, lo + stridelo , (hi - lo) - stridelo); 
     128            memmove(lo + stridehi, lo + stridelo , (hi - lo) - stridelo); 
    129129            memcpy(lo, tmp.ptr, stridehi); 
    130130            memcpy(hi + cast(int) stridehi - cast(int) stridelo, tmplo.ptr, stridelo); 
    131131 
    132132            lo += stridehi; 
    133        hi = hi - 1 + (cast(int) stridehi - cast(int) stridelo); 
     133            hi = hi - 1 + (cast(int) stridehi - cast(int) stridelo); 
    134134        } 
    135135    } 
     
    219219             */ 
    220220            memcpy(tmp.ptr, hi, stridehi * wchar.sizeof); 
    221        memcpy(hi + cast(int) stridehi - cast(int) stridelo, lo, stridelo * wchar.sizeof); 
     221            memcpy(hi + cast(int) stridehi - cast(int) stridelo, lo, stridelo * wchar.sizeof); 
    222222            memmove(lo + stridehi, lo + stridelo , (hi - (lo + stridelo)) * wchar.sizeof); 
    223223            memcpy(lo, tmp.ptr, stridehi * wchar.sizeof); 
    224224 
    225225            lo += stridehi; 
    226        hi = hi - 1 + (cast(int) stridehi - cast(int) stridelo); 
     226            hi = hi - 1 + (cast(int) stridehi - cast(int) stridelo); 
    227227        } 
    228228    } 
     
    490490    if (a.length > 1) 
    491491    { 
    492    dchar[] da = toUTF32(a); 
     492        dchar[] da = toUTF32(a); 
    493493        da.sort; 
    494494        size_t i = 0; 
     
    512512    if (a.length > 1) 
    513513    { 
    514    dchar[] da = toUTF32(a); 
     514        dchar[] da = toUTF32(a); 
    515515        da.sort; 
    516516        size_t i = 0; 
    517517        foreach (dchar d; da) 
    518518        {   wchar[2] buf; 
    519        auto t = toUTF16(buf, d); 
     519            auto t = toUTF16(buf, d); 
    520520            a[i .. i + t.length] = t[]; 
    521521            i += t.length; 
     
    532532extern (C) int _adEq(Array a1, Array a2, TypeInfo ti) 
    533533{ 
    534     debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); 
     534     debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); 
     535 
     536    if (a1.ptr == a2.ptr) 
     537        return 1; // equal 
    535538    if (a1.length != a2.length) 
    536         return 0;               // not equal 
    537     auto sz = ti.tsize(); 
    538     auto p1 = a1.ptr; 
    539     auto p2 = a2.ptr; 
    540  
    541 /+ 
    542     for (int i = 0; i < a1.length; i++) 
    543     { 
    544         printf("%4x %4x\n", (cast(short*)p1)[i], (cast(short*)p2)[i]); 
    545     } 
    546 +/ 
    547  
    548     if (sz == 1) 
    549         // We should really have a ti.isPOD() check for this 
    550         return (memcmp(p1, p2, a1.length) == 0); 
    551  
    552     for (size_t i = 0; i < a1.length; i++) 
    553     { 
    554         if (!ti.equals(p1 + i * sz, p2 + i * sz)) 
    555             return 0;           // not equal 
    556     } 
    557     return 1;                   // equal 
     539        return 0; // not equal 
     540 
     541    // We should really have a ti.isPOD() check for this 
     542    if (ti.tsize() != 1) 
     543        return ti.equals(&a1, &a2); 
     544    return memcmp(a1.ptr, a2.ptr, a1.length) == 0; 
    558545} 
    559546 
     
    578565{ 
    579566    debug(adi) printf("adCmp()\n"); 
     567 
     568    if (a1.ptr == a2.ptr) 
     569        return 0; 
    580570    auto len = a1.length; 
    581571    if (a2.length < len) 
    582572        len = a2.length; 
    583     auto sz = ti.tsize(); 
    584     void *p1 = a1.ptr; 
    585     void *p2 = a2.ptr; 
    586  
    587     if (sz == 1) 
    588     {   // We should really have a ti.isPOD() check for this 
    589         auto c = memcmp(p1, p2, len); 
    590         if (c) 
    591             return c; 
    592     } 
    593     else 
    594     { 
    595         for (size_t i = 0; i < len; i++) 
    596         { 
    597             auto c = ti.compare(p1 + i * sz, p2 + i * sz); 
    598             if (c) 
    599                 return c; 
    600         } 
    601     } 
     573 
     574    // We should really have a ti.isPOD() check for this 
     575    if (ti.tsize() != 1) 
     576        return ti.compare(&a1, &a2); 
     577    auto c = memcmp(a1.ptr, a2.ptr, len); 
     578    if (c) 
     579        return c; 
    602580    if (a1.length == a2.length) 
    603581        return 0; 
    604     return (a1.length > a2.length) ? 1 : -1; 
     582    return a1.length > a2.length ? 1 : -1; 
    605583} 
    606584