Ignore:
Timestamp:
Apr 13, 2004, 7:33:59 PM (21 years ago)
Author:
mjs
Message:

Reviewed by Darin.

  • fixed <rdar://problem/3600695>: String manipulation in JavaScript 24fun test is very slow (slow)
  • fixed <rdar://problem/3600691>: Table generation test is really slow
  • fixed <rdar://problem/3600661>: 24fun date test is really slow

80% speedup on the string test, lesser speedups on the other two.

Two different optimizations here:

1) Avoid large overhead of scanning strings to see if they are all
ASCII before numeric conversion.

  • kjs/nodes.cpp: (AssignNode::evaluate): Don't convert to integer until we know for sure the operation will need it. Attempting to convert strings to numbers is a waste when they are being appended with +=.

2) Avoid huge cost of appending strings.

This is done by allowing multiple strings to share a buffer but
actually use different ranges of it. The first time a string is
appended to, we start leaving at least 10% extra space in the
buffer, so doing N appends to the same string takes O(log N)
mallocs instead of O(N).

  • kjs/identifier.cpp: (KJS::Identifier::equal): (KJS::Identifier::add):
  • kjs/ustring.cpp: (KJS::): (KJS::UCharReference::operator=): (KJS::UCharReference::ref): (KJS::UString::Rep::create): (KJS::UString::Rep::destroy): (KJS::UString::expandedSize): (KJS::UString::usedCapacity): (KJS::UString::expandCapacity): (KJS::UString::UString): (KJS::UString::null): (KJS::UString::append): (KJS::UString::operator=): (KJS::UString::toStrictUInt32): (KJS::UString::detach): (KJS::KJS::operator==):
  • kjs/ustring.h: (KJS::UString::Rep::data): (KJS::UString::Rep::hash):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/identifier.cpp

    r4206 r6360  
    6868{
    6969    int length = r->len;
    70     const UChar *d = r->dat;
     70    const UChar *d = r->data();
    7171    for (int i = 0; i != length; ++i)
    7272        if (d[i].uc != (unsigned char)s[i])
     
    7979    if (r->len != length)
    8080        return false;
    81     const UChar *d = r->dat;
     81    const UChar *d = r->data();
    8282    for (int i = 0; i != length; ++i)
    8383        if (d[i].uc != s[i].uc)
     
    9191    if (length != b->len)
    9292        return false;
    93     const UChar *d = r->dat;
    94     const UChar *s = b->dat;
     93    const UChar *d = r->data();
     94    const UChar *s = b->data();
    9595    for (int i = 0; i != length; ++i)
    9696        if (d[i].uc != s[i].uc)
     
    123123    }
    124124   
    125     UChar *d = new UChar[length];
     125    UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * length));
    126126    for (int j = 0; j != length; j++)
    127127        d[j] = c[j];
    128128   
    129     UString::Rep *r = new UString::Rep;
    130     r->dat = d;
    131     r->len = length;
    132     r->capacity = UString::Rep::capacityForIdentifier;
     129    UString::Rep *r = UString::Rep::create(d, length);
     130    r->isIdentifier = 1;
    133131    r->rc = 0;
    134132    r->_hash = hash;
     
    164162    }
    165163   
    166     UChar *d = new UChar[length];
     164    UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * length));
    167165    for (int j = 0; j != length; j++)
    168166        d[j] = s[j];
    169167   
    170     UString::Rep *r = new UString::Rep;
    171     r->dat = d;
    172     r->len = length;
    173     r->capacity = UString::Rep::capacityForIdentifier;
     168    UString::Rep *r = UString::Rep::create(d, length);
     169    r->isIdentifier = 1;
    174170    r->rc = 0;
    175171    r->_hash = hash;
     
    186182UString::Rep *Identifier::add(UString::Rep *r)
    187183{
    188     if (r->capacity == UString::Rep::capacityForIdentifier)
     184    if (r->isIdentifier)
    189185        return r;
    190186    if (r->len == 0)
     
    207203    }
    208204   
    209     r->capacity = UString::Rep::capacityForIdentifier;
     205    r->isIdentifier = 1;
    210206   
    211207    _table[i] = r;
Note: See TracChangeset for help on using the changeset viewer.