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/HashTraits.h

    r11719 r11739  
    2424#define KXMLCORE_HASH_TRAITS_H
    2525
     26#include "RefPtr.h"
    2627#include <utility>
    2728
     
    4546       
    4647    template<typename T>
    47     struct HashTraits {
    48         typedef T traitType;
     48    struct GenericHashTraits {
     49        typedef T TraitType;
    4950        static const bool emptyValueIsZero = IsInteger<T>::value;
    50        
    51         static traitType emptyValue() {
    52             return traitType();
    53         }
     51        static const bool needsDestruction = !IsInteger<T>::value;
     52        static TraitType emptyValue() { return IsInteger<T>::value ? 0 : TraitType(); }
     53    };
     54
     55    template<typename T>
     56    struct HashTraits : GenericHashTraits<T> {
    5457    };
    5558
    5659    // may not be appropriate for all uses since it would disallow 0 and -1 as keys
    5760    template<>
    58     struct HashTraits<int> {
    59         typedef int traitType;
     61    struct HashTraits<int> : GenericHashTraits<int> {
     62        static TraitType deletedValue() { return -1; }
     63    };
     64
     65    template<typename P>
     66    struct HashTraits<P *> : GenericHashTraits<P *> {
     67        typedef P *TraitType;
    6068        static const bool emptyValueIsZero = true;
     69        static const bool needsDestruction = false;
     70        static TraitType emptyValue() { return reinterpret_cast<P *>(0); }
     71        static TraitType deletedValue() { return reinterpret_cast<P *>(-1); }
     72    };
     73
     74    template<typename P>
     75    struct HashTraits<RefPtr<P> > : GenericHashTraits<RefPtr<P> > {
     76        static const bool emptyValueIsZero = true;
     77    };
     78
     79    template<typename T, typename Traits>
     80    class DeletedValueAssigner;
     81
     82    template<typename T, typename Traits>
     83    inline void assignDeleted(T& location)
     84    {
     85        DeletedValueAssigner<T, Traits>::assignDeletedValue(location);
     86    }
     87
     88    template<typename FirstTraits, typename SecondTraits>
     89    struct PairHashTraits : GenericHashTraits<pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> > {
     90    private:
     91        typedef typename FirstTraits::TraitType FirstType;
     92        typedef typename SecondTraits::TraitType SecondType;
     93    public:
     94        typedef pair<FirstType, SecondType> TraitType;
     95
     96        static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
     97        static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
    6198       
    62         static traitType emptyValue() {
    63             return 0;
     99        static TraitType emptyValue()
     100        {
     101            return TraitType(FirstTraits::emptyValue(), SecondTraits::emptyValue());
    64102        }
    65         static traitType deletedValue() {
    66             return -1;
     103
     104        static TraitType deletedValue()
     105        {
     106            return TraitType(FirstTraits::deletedValue(), SecondTraits::emptyValue());
     107        }
     108
     109        static void assignDeletedValue(TraitType& location)
     110        {
     111            assignDeleted<FirstType, FirstTraits>(location.first);
     112            location.second = SecondTraits::emptyValue();
    67113        }
    68114    };
    69115
    70     template<typename P>
    71     struct HashTraits<P *> {
    72         typedef P *traitType;
    73         static const bool emptyValueIsZero = true;
    74        
    75         static traitType emptyValue() {
    76             return reinterpret_cast<P *>(0);
    77         }
    78         static traitType deletedValue() {
    79             return reinterpret_cast<P *>(-1);
     116    template<typename First, typename Second>
     117    struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > {
     118    };
     119
     120    template<typename T, typename Traits>
     121    struct DeletedValueAssigner
     122    {
     123        static void assignDeletedValue(T& location)
     124        {
     125            location = Traits::deletedValue();
    80126        }
    81127    };
    82128
    83129    template<typename FirstTraits, typename SecondTraits>
    84     struct PairHashTraits {
    85     private:
    86         typedef typename FirstTraits::traitType FirstType;
    87         typedef typename SecondTraits::traitType SecondType;
    88     public:
    89         typedef pair<FirstType, SecondType> traitType;
    90        
    91         static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
    92        
    93         static traitType emptyValue() {
    94             return traitType(FirstTraits::emptyValue(), SecondTraits::emptyValue());
    95         }
    96         static traitType deletedValue() {
    97             return traitType(FirstTraits::deletedValue(), SecondTraits::emptyValue());
     130    struct DeletedValueAssigner<pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType>, PairHashTraits<FirstTraits, SecondTraits> >
     131    {
     132        static void assignDeletedValue(pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType>& location)
     133        {
     134            PairHashTraits<FirstTraits, SecondTraits>::assignDeletedValue(location);
    98135        }
    99136    };
    100    
     137
    101138    template<typename First, typename Second>
    102     struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > {
     139    struct DeletedValueAssigner<pair<First, Second>, HashTraits<pair<First, Second> > >
     140    {
     141        static void assignDeletedValue(pair<First, Second>& location)
     142        {
     143            HashTraits<pair<First, Second> >::assignDeletedValue(location);
     144        }
    103145    };
     146
    104147
    105148} // namespace KXMLCore
Note: See TracChangeset for help on using the changeset viewer.