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


Ignore:
Timestamp:
Apr 14, 2005, 6:26:26 PM (20 years ago)
Author:
mjs
Message:

Reviewed by Richard.

<rdar://problem/4089734> JavaScript iBench can be sped up ~10% with custom allocator

  • use custom single-threaded malloc for all non-GC JavaScriptCore allocations, for a 9.1% speedup on JavaScript iBench
  • JavaScriptCore.pbproj/project.pbxproj:
  • kjs/collector.cpp: (KJS::Collector::allocate): Use dlmalloc to allocate the collector blocks. (KJS::Collector::collect): And dlfree to free it.
  • kjs/fast_malloc.cpp: Added, just the standard dlmalloc here.
  • kjs/fast_malloc.h: Added. Declarations for the functions. Also added a handy macro to give a class custom operator new/delete
  • kjs/identifier.cpp: (KJS::Identifier::add): Use dlmalloc/dlfree.
  • kjs/nodes.h: make nodes KJS_FAST_ALLOCATED.
  • kjs/property_map.cpp: (KJS::PropertyMap::~PropertyMap): Use dlmalloc/dlfree. (KJS::PropertyMap::rehash): ditto
  • kjs/scope_chain.h:
  • kjs/ustring.cpp: (KJS::UString::Rep::createCopying): New named constructor that copies a passed-in buffer, to hide allocation details from webcore. (KJS::UString::UString): use createCopying when appropriate. (KJS::UString::Rep::destroy): Use dlmalloc/dlfree. (KJS::UString::expandedSize): likewise (KJS::UString::expandCapacity): likewise (KJS::UString::expandPreCapacity): likewise (KJS::UString::spliceSubstringsWithSeparators): likewise (KJS::UString::append): likewise (KJS::UString::operator=): likewise (KJS::UString::detach): likewise
  • kjs/ustring.h: make UString and UString::Rep KJS_FAST_ALLOCATED.
File:
1 edited

Legend:

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

    r7798 r9009  
    3636#endif
    3737
     38#include "fast_malloc.h"
    3839#include "ustring.h"
    3940#include "operations.h"
     
    4546
    4647#include <unicode/uchar.h>
    47 
    48 // malloc_good_size is not prototyped anywhere!
    49 extern "C" {
    50   size_t malloc_good_size(size_t size);
    51 }
    5248
    5349#endif
     
    194190    return callerBetterNotModifyThis;
    195191  }
     192}
     193
     194UString::Rep *UString::Rep::createCopying(const UChar *d, int l)
     195{
     196  int sizeInBytes = l * sizeof(UChar);
     197  UChar *copyD = static_cast<UChar *>(kjs_fast_malloc(sizeInBytes));
     198  memcpy(copyD, d, sizeInBytes);
     199
     200  return create(copyD, l);
    196201}
    197202
     
    249254    baseString->deref();
    250255  } else {
    251     free(buf);
     256    kjs_fast_free(buf);
    252257  }
    253258  delete this;
     
    331336{
    332337  int s = (size * 11 / 10) + 1 + otherSize;
    333 #if APPLE_CHANGES
    334   s = malloc_good_size(s * sizeof(UChar)) / sizeof(UChar);
    335 #endif
    336338  return s;
    337339}
     
    353355  if (requiredLength > r->capacity) {
    354356    int newCapacity = expandedSize(requiredLength, r->preCapacity);
    355     r->buf = static_cast<UChar *>(realloc(r->buf, newCapacity * sizeof(UChar)));
     357    r->buf = static_cast<UChar *>(kjs_fast_realloc(r->buf, newCapacity * sizeof(UChar)));
    356358    r->capacity = newCapacity - r->preCapacity;
    357359  }
     
    369371    int delta = newCapacity - r->capacity - r->preCapacity;
    370372
    371     UChar *newBuf = static_cast<UChar *>(malloc(newCapacity * sizeof(UChar)));
     373    UChar *newBuf = static_cast<UChar *>(kjs_fast_malloc(newCapacity * sizeof(UChar)));
    372374    memcpy(newBuf + delta, r->buf, (r->capacity + r->preCapacity) * sizeof(UChar));
    373     free(r->buf);
     375    kjs_fast_free(r->buf);
    374376    r->buf = newBuf;
    375377
     
    389391UString::UString(char c)
    390392{
    391     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar)));
     393    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar)));
    392394    d[0] = c;
    393395    rep = Rep::create(d, 1);
     
    405407    return;
    406408  }
    407   UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * length));
     409  UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * length));
    408410  for (int i = 0; i < length; i++)
    409411    d[i].uc = c[i];
     
    417419    return;
    418420  }
    419   UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) *length));
    420   memcpy(d, c, length * sizeof(UChar));
    421   rep = Rep::create(d, length);
     421  rep = Rep::createCopying(c, length);
    422422}
    423423
     
    428428    return;
    429429  }
    430   UChar *d;
    431430  if (copy) {
    432     d = static_cast<UChar *>(malloc(sizeof(UChar) * length));
    433     memcpy(d, c, length * sizeof(UChar));
    434   } else
    435     d = c;
    436   rep = Rep::create(d, length);
     431    rep = Rep::createCopying(c, length);
     432  } else {
     433    rep = Rep::create(c, length);
     434  }
    437435}
    438436
     
    474472    // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
    475473    int newCapacity = expandedSize(length, 0);
    476     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
     474    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * newCapacity));
    477475    memcpy(d, a.data(), aSize * sizeof(UChar));
    478476    memcpy(d + aSize, b.data(), bSize * sizeof(UChar));
     
    620618  }
    621619
    622   UChar *buffer = static_cast<UChar *>(malloc(totalLength * sizeof(UChar)));
     620  UChar *buffer = static_cast<UChar *>(kjs_fast_malloc(totalLength * sizeof(UChar)));
    623621
    624622  int maxCount = MAX(rangeCount, separatorCount);
     
    673671    // this is shared with someone using more capacity, gotta make a whole new string
    674672    int newCapacity = expandedSize(length, 0);
    675     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
     673    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * newCapacity));
    676674    memcpy(d, data(), thisSize * sizeof(UChar));
    677675    memcpy(const_cast<UChar *>(d + thisSize), t.data(), tSize * sizeof(UChar));
     
    717715    // this is shared with someone using more capacity, gotta make a whole new string
    718716    int newCapacity = expandedSize(length, 0);
    719     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
     717    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * newCapacity));
    720718    memcpy(d, data(), thisSize * sizeof(UChar));
    721719    for (int i = 0; i < tSize; ++i)
     
    738736    // this is empty - must make a new rep because we don't want to pollute the shared empty one
    739737    int newCapacity = expandedSize(1, 0);
    740     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
     738    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * newCapacity));
    741739    d[0] = c;
    742740    release();
     
    761759    // this is shared with someone using more capacity, gotta make a whole new string
    762760    int newCapacity = expandedSize((length + 1), 0);
    763     UChar *d = static_cast<UChar *>(malloc(sizeof(UChar) * newCapacity));
     761    UChar *d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * newCapacity));
    764762    memcpy(d, data(), length * sizeof(UChar));
    765763    d[length] = c;
     
    823821  } else {
    824822    release();
    825     d = static_cast<UChar *>(malloc(sizeof(UChar) * l));
     823    d = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * l));
    826824    rep = Rep::create(d, l);
    827825  }
     
    11381136  if (rep->rc > 1 || rep->baseString) {
    11391137    int l = size();
    1140     UChar *n = static_cast<UChar *>(malloc(sizeof(UChar) * l));
     1138    UChar *n = static_cast<UChar *>(kjs_fast_malloc(sizeof(UChar) * l));
    11411139    memcpy(n, data(), l * sizeof(UChar));
    11421140    release();
Note: See TracChangeset for help on using the changeset viewer.