Ignore:
Timestamp:
Dec 22, 2005, 5:52:43 PM (19 years ago)
Author:
mjs
Message:

JavaScriptCore:

Reviewed by Darin.

The changes for support are relatively simple, but I also made extensive changes to
avoid copying, so that there isn't refcount thrash when you put RefPtrs into a HashMap.

  • kxmlcore/HashTable.h: (KXMLCore::swap): specialize swap for pairs, to swap elements individually, so that excess copies can be avoided. (KXMLCore::Mover::move): Template function to either copy or swap, used when transferring elements from old table to new. (KXMLCore::IdentityHashTranslator::hash): The old "converting lookup" templates that took two or three function parameters now take a class parameter, this is the class used to do a normal lookup. (KXMLCore::IdentityHashTranslator::equal): Ditto. (KXMLCore::IdentityHashTranslator::translate): Ditto. Translate now takes a reference to write into instead of returning a value to avoid redundant copies. (KXMLCore::HashTable::~HashTable): Use deallocateTable instead of freeing directly. (KXMLCore::HashTable::insert): Based on HashTranslator now instead of separate functions. Added a FIXME about a remaining rare excess copy. (KXMLCore::HashTable::isEmptyBucket): Use KeyTraits directly instead of unwrapping the key from Traits, to avoid creating and destroying pair, which copies. (KXMLCore::HashTable::isDeletedBucket): ditto (KXMLCore::HashTable::lookup): Use HashTranslator now instead of separate functions. (KXMLCore::HashTable::initializeBucket): Renamed from emptyBucket. Use placement new to work right for non-POD types. (KXMLCore::HashTable::deleteBucket): Use assignDeleted to avoid excess copies. (KXMLCore::HashTable::reinsert): use Mover template to copy or swap as appropriate (KXMLCore::HashTable::allocateTable): Initialize every bucket if calloc won't do. (KXMLCore::HashTable::deallocateTable): Destruct every bucket if needed. (KXMLCore::HashTable::rehash): Avoid copy before reinserting, so that swap can do its magic. (KXMLCore::HashTable::clear): use deallocateTable instead of freeing directly. (KXMLCore::HashTable::HashTable): be more dumb when copying to ensure that non-POD types work right
  • kxmlcore/HashFunctions.h: (KXMLCore::PointerHash): Specialize PointerHash for RefPtr
  • kxmlcore/HashMap.h: (KXMLCore::extractFirst): Return a reference not a full object to avoid copies. (KXMLCore::HashMapTranslator::hash): Use a special translator for insertion to defer making the pair as long as possible, thus avoiding needless copies. (KXMLCore::HashMapTranslator::equal): ditto (KXMLCore::HashMapTranslator::translate): ditto (KXMLCore::::inlineAdd): Shared by set and add to insert using HashMapTranslator (KXMLCore::::set): Use inlineAdd (KXMLCore::::add): Use inlineAdd
  • kxmlcore/HashMapPtrSpec.h: (KXMLCore::): Pass KeyTraits along
  • kxmlcore/HashSet.h: (KXMLCore::identityExtract): Return a reference not a full object to avoid copies. (KXMLCore::HashSetTranslatorAdapter::hash): Redo adapter stuff to work with the new HashTranslator approach. (KXMLCore::HashSetTranslatorAdapter::equal): ditto (KXMLCore::HashSetTranslatorAdapter::translate): ditto (KXMLCore::::insert): ditto
  • kxmlcore/HashTraits.h: (KXMLCore::GenericHashTraits): This is intended be used as a base class for customized traits: sensible defaults. (KXMLCore::): Use it a bunch (KXMLCore::assignDeleted): template function to allow pairs to be assigned the deleted value w/o excess copies. (KXMLCore::PairHashTraits::emptyValue): Updated (KXMLCore::PairHashTraits::deletedValue): Updated (KXMLCore::PairHashTraits::assignDeletedValue): part of assignDeleted hack (KXMLCore::DeletedValueAssigner::assignDeletedValue): Use template magic to either use use deletedValue or assignDeletedValue for the cases where we care.
  • kxmlcore/RefPtr.h: (KXMLCore::RefPtr::swap): Added swap method. (KXMLCore::swap): Added swap free function.
  • kjs/identifier.cpp: (KJS::CStringTranslator::hash): Use new HashTranslator class approach to alternate type based insertion. (KJS::CStringTranslator::equal): ditto (KJS::CStringTranslator::translate): ditto (KJS::Identifier::add): ditto (KJS::UCharBufferTranslator::hash): ditto (KJS::UCharBufferTranslator::equal): ditto (KJS::UCharBufferTranslator::translate): ditto
  • irrelevant change:
  • kjs/array_object.cpp: (ArrayProtoFunc::callAsFunction): Removed a stray space.

WebCore:

Reviewed by Darin.

  • update for new HashTranslator stuff
  • khtml/xml/dom_atomicstring.cpp: (DOM::CStringTranslator::hash): (DOM::CStringTranslator::equal): (DOM::CStringTranslator::translate): (DOM::AtomicString::equal): (DOM::AtomicString::add): (DOM::QCharBufferTranslator::hash): (DOM::QCharBufferTranslator::equal): (DOM::QCharBufferTranslator::translate):
  • khtml/xml/dom_qname.cpp: (DOM::QNameComponentsTranslator::hash): (DOM::QNameComponentsTranslator::equal): (DOM::QNameComponentsTranslator::translate): (DOM::QualifiedName::QualifiedName):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kxmlcore/HashMap.h

    r11561 r11739  
    3131
    3232template<typename PairType>
    33 inline typename PairType::first_type extractFirst(const PairType& value)
     33inline typename PairType::first_type const& extractFirst(const PairType& value)
    3434{
    3535    return value.first;
    3636}
     37
     38template<typename Key, typename Mapped, typename HashFunctions>
     39class HashMapTranslator
     40{
     41    typedef pair<Key, Mapped> ValueType;
     42       
     43public:
     44    static unsigned hash(const Key& key)
     45    {
     46        return HashFunctions::hash(key);
     47    }
     48   
     49    static bool equal(const Key& a, const Key& b)
     50    {
     51        return HashFunctions::equal(a, b);
     52    }
     53   
     54    static void translate(ValueType& location, const Key& key, const Mapped& mapped, unsigned)
     55    {
     56        ValueType tmp(key, mapped);
     57        swap(tmp, location);
     58    }
     59};
     60
    3761
    3862template<typename Key, typename Mapped, typename HashFunctions = DefaultHash<Key>, typename KeyTraits = HashTraits<Key>, typename MappedTraits = HashTraits<Mapped> >
     
    4165    typedef Key KeyType;
    4266    typedef Mapped MappedType;
    43     typedef std::pair<Key, Mapped> ValueType;
     67    typedef pair<Key, Mapped> ValueType;
    4468    typedef PairHashTraits<KeyTraits, MappedTraits> ValueTraits;
    4569 private:
    46     typedef HashTable<KeyType, ValueType, extractFirst<ValueType>, HashFunctions, ValueTraits> ImplType;
     70    typedef HashTable<KeyType, ValueType, extractFirst<ValueType>, HashFunctions, ValueTraits, KeyTraits> ImplType;
     71    typedef HashMapTranslator<Key, Mapped, HashFunctions> TranslatorType;
    4772 public:
    4873    typedef typename ImplType::iterator iterator;
     
    6994    // return value is a pair of the iterator to the key location,
    7095    // and a boolean that's true if a new value was actually added
    71     std::pair<iterator, bool> set(const KeyType &key, const MappedType &mapped);
     96    pair<iterator, bool> set(const KeyType &key, const MappedType &mapped);
    7297
    7398    // does nothing if key is already present
    7499    // return value is a pair of the iterator to the key location,
    75100    // and a boolean that's true if a new value was actually added
    76     std::pair<iterator, bool> add(const KeyType &key, const MappedType &mapped);
     101    pair<iterator, bool> add(const KeyType &key, const MappedType &mapped);
    77102
    78103    void remove(const KeyType& key);
     
    81106
    82107 private:
     108    pair<iterator, bool> inlineAdd(const KeyType &key, const MappedType &mapped);
     109
    83110    ImplType m_impl;
    84111};
     
    145172
    146173template<typename Key, typename Mapped, typename HashFunctions, typename KeyTraits, typename MappedTraits>
    147 std::pair<typename HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::iterator, bool> HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::set(const KeyType &key, const MappedType &mapped)
    148 {
    149     pair<iterator, bool> result = m_impl.insert(ValueType(key, mapped));
    150     // the insert call above won't change anything if the key is
     174pair<typename HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::iterator, bool> HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::inlineAdd(const KeyType &key, const MappedType &mapped)
     175{
     176    return m_impl.template insert<KeyType, MappedType, TranslatorType>(key, mapped);
     177}
     178
     179template<typename Key, typename Mapped, typename HashFunctions, typename KeyTraits, typename MappedTraits>
     180pair<typename HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::iterator, bool> HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::set(const KeyType &key, const MappedType &mapped)
     181{
     182    pair<iterator, bool> result = inlineAdd(key, mapped);
     183    // the add call above won't change anything if the key is
    151184    // already there; in that case, make sure to set the value.
    152185    if (!result.second)
     
    156189
    157190template<typename Key, typename Mapped, typename HashFunctions, typename KeyTraits, typename MappedTraits>
    158 std::pair<typename HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::iterator, bool> HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::add(const KeyType &key, const MappedType &mapped)
    159 {
    160     return m_impl.insert(ValueType(key, mapped));
     191pair<typename HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::iterator, bool> HashMap<Key, Mapped, HashFunctions, KeyTraits, MappedTraits>::add(const KeyType &key, const MappedType &mapped)
     192{
     193    return inlineAdd(key, mapped);
    161194}
    162195
Note: See TracChangeset for help on using the changeset viewer.