Changeset 1791 in webkit for trunk/JavaScriptCore/kjs/ustring.cpp


Ignore:
Timestamp:
Aug 9, 2002, 9:31:50 PM (23 years ago)
Author:
darin
Message:

JavaScriptCore:

Some string speedups. Makes sony.com cached 11% faster.

  • kjs/ustring.h: Made it possible for UChar objects to be uninitialized, which gives a speed boost. Inlined CString's +=, UString's destructor, +=, and +.
  • kjs/ustring.cpp: (UString::UString): Optimize const char * version, which showed up heavily in performance analysis. Added new two-UString version, which makes the + operator fast. (UString::ascii): Remove thread safety changes. Change static buffer to remember its size, and to always be at least 4096 bytes long; that way we never have to reallocate unless it's for a long string. Also make code to extract the characters significantly faster by getting rid of two pointer dereferences per character. (UString::is8Bit): Avoid one pointer dereference per character. (UString::toDouble): Use ascii() instead of cstring() to avoid copying the string.
  • kjs/collector.cpp: Remove unneeded APPLE_CHANGES.
  • kjs/regexp.cpp: Remove ifdefs around some APPLE_CHANGES that we want to keep, because they just fix warnings.
  • kjs/value.h: Remove obsolete APPLE_CHANGES comment.
  • JavaScriptCore.pbproj/project.pbxproj: Project Builder decided to move a line around in the file.

WebCore:

  • force-clean-timestamp: JavaScriptCore headers changed that require a full build here.
File:
1 edited

Legend:

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

    r1623 r1791  
    104104}
    105105
    106 CString &CString::operator+=(const CString &str)
    107 {
    108   return append(str.c_str());
    109 }
    110 
    111106int CString::size() const
    112107{
     
    120115
    121116UChar UChar::null;
    122 #ifdef APPLE_CHANGES
    123117UString::Rep UString::Rep::null = { 0, 0, 0, 1 };
    124 #else
    125 UString::Rep UString::Rep::null = { 0, 0, 1 };
    126 #endif
    127118UString UString::null;
    128 #ifdef APPLE_CHANGES
    129 static pthread_once_t statBufferKeyOnce = PTHREAD_ONCE_INIT;
    130 static pthread_key_t statBufferKey;
    131 #else
    132 static char *statBuffer = 0L;
    133 #endif
     119const int normalStatBufferSize = 4096;
     120static char *statBuffer = 0;
     121static int statBufferSize = 0;
    134122
    135123UChar::UChar(const UCharReference &c)
     
    140128UChar UChar::toLower() const
    141129{
    142   // ### properly supprot unicode tolower
     130  // ### properly support unicode tolower
    143131  if (uc >= 256 || islower(uc))
    144132    return *this;
    145133
    146   return UChar(tolower(uc));
     134  return (unsigned char)tolower(uc);
    147135}
    148136
     
    152140    return *this;
    153141
    154   return UChar(toupper(uc));
     142  return (unsigned char)toupper(uc);
    155143}
    156144
     
    177165  r->dat = d;
    178166  r->len = l;
    179 #ifdef APPLE_CHANGES
    180167  r->capacity = l;
    181 #endif
    182168  r->rc = 1;
    183169
     
    194180{
    195181    UChar *d = new UChar[1];
    196     d[0] = UChar(0, c);
     182    d[0] = c;
    197183    rep = Rep::create(d, 1);
    198184}
     
    200186UString::UString(const char *c)
    201187{
    202   attach(&Rep::null);
    203   operator=(c);
     188  if (!c) {
     189    attach(&Rep::null);
     190    return;
     191  }
     192  int length = strlen(c);
     193  UChar *d = new UChar[length];
     194  for (int i = 0; i < length; i++)
     195    d[i].uc = c[i];
     196  rep = Rep::create(d, length);
    204197}
    205198
     
    227220}
    228221
    229 UString::~UString()
    230 {
    231   release();
     222UString::UString(const UString &a, const UString &b)
     223{
     224  int aSize = a.size();
     225  int bSize = b.size();
     226  UChar *d = new UChar[aSize + bSize];
     227  memcpy(d, a.data(), aSize * sizeof(UChar));
     228  memcpy(d + aSize, b.data(), bSize * sizeof(UChar));
     229  rep = Rep::create(d, aSize + bSize);
    232230}
    233231
     
    275273UString &UString::append(const UString &t)
    276274{
    277 #ifdef APPLE_CHANGES
    278275  int l = size();
    279276  int tLen = t.size();
     
    292289  rep = Rep::create(n, newLen);
    293290  rep->capacity = newCapacity;
    294 #else
    295   int l = size();
    296   UChar *n = new UChar[l+t.size()];
    297   memcpy(n, data(), l * sizeof(UChar));
    298   memcpy(n+l, t.data(), t.size() * sizeof(UChar));
    299   release();
    300   rep = Rep::create(n, l + t.size());
    301 #endif
    302291
    303292  return *this;
     
    306295CString UString::cstring() const
    307296{
    308   return CString(ascii());
    309 }
    310 
    311 #ifdef APPLE_CHANGES
    312 static void statBufferKeyCleanup(void *statBuffer)
    313 {
    314   if (statBuffer != NULL)
    315     delete [] (char *)statBuffer;
    316 }
    317 
    318 static void statBufferKeyInit(void)
    319 {
    320   pthread_key_create(&statBufferKey, statBufferKeyCleanup);
    321 }
    322 #endif
     297  return ascii();
     298}
    323299
    324300char *UString::ascii() const
    325301{
    326 #ifdef APPLE_CHANGES
    327   pthread_once(&statBufferKeyOnce, statBufferKeyInit);
    328   char *statBuffer = (char *)pthread_getspecific(statBufferKey);
    329 #endif
    330   if (statBuffer)
     302  // Never make the buffer smaller than normalStatBufferSize.
     303  // Thus we almost never need to reallocate.
     304  int length = size();
     305  int neededSize = length + 1;
     306  if (neededSize < normalStatBufferSize) {
     307    neededSize = normalStatBufferSize;
     308  }
     309  if (neededSize != statBufferSize) {
    331310    delete [] statBuffer;
    332 
    333   statBuffer = new char[size()+1];
    334   for(int i = 0; i < size(); i++)
    335     statBuffer[i] = data()[i].low();
    336   statBuffer[size()] = '\0';
    337 
    338 #ifdef APPLE_CHANGES
    339   pthread_setspecific(statBufferKey, statBuffer);
    340 #endif
     311    statBuffer = new char [neededSize];
     312    statBufferSize = neededSize;
     313  }
     314 
     315  const UChar *p = data();
     316  char *q = statBuffer;
     317  const UChar *limit = p + length;
     318  while (p != limit) {
     319    *q = p->uc;
     320    ++p;
     321    ++q;
     322  }
     323  *q = '\0';
     324
    341325  return statBuffer;
    342326}
     
    346330{
    347331  delete [] statBuffer;
    348   statBuffer = 0L;
     332  statBuffer = 0;
     333  statBufferSize = 0;
    349334}
    350335#endif
     
    352337UString &UString::operator=(const char *c)
    353338{
    354 #ifdef APPLE_CHANGES
    355339  int l = c ? strlen(c) : 0;
    356340  UChar *d;
     
    363347  }
    364348  for (int i = 0; i < l; i++)
    365     d[i].uc = (uchar)c[i];
    366 #else
    367   release();
    368   int l = c ? strlen(c) : 0;
    369 
    370   UChar *d = new UChar[l];
    371   for (int i = 0; i < l; i++)
    372349    d[i].uc = c[i];
    373   rep = Rep::create(d, l);
    374 #endif
    375350
    376351  return *this;
     
    386361}
    387362
    388 UString &UString::operator+=(const UString &s)
    389 {
    390   return append(s);
    391 }
    392 
    393363bool UString::is8Bit() const
    394364{
    395365  const UChar *u = data();
    396   for(int i = 0; i < size(); i++, u++)
     366  const UChar *limit = u + size();
     367  while (u < limit) {
    397368    if (u->uc > 0xFF)
    398369      return false;
     370    ++u;
     371  }
    399372
    400373  return true;
     
    422395    return NaN;
    423396
    424   CString str = cstring();
    425   const char *c = str.c_str();
     397  const char *c = ascii();
    426398
    427399  // skip leading white space
     
    615587  return (l1 < l2);
    616588}
    617 
    618 UString KJS::operator+(const UString& s1, const UString& s2)
    619 {
    620   UString tmp(s1);
    621   tmp.append(s2);
    622 
    623   return tmp;
    624 }
Note: See TracChangeset for help on using the changeset viewer.