Changeset 51566 in webkit


Ignore:
Timestamp:
Dec 1, 2009 4:05:30 PM (14 years ago)
Author:
snej@chromium.org
Message:

JavaScriptCore: Added variants of find/contains/add that allow a foreign key type to be used.
This will allow AtomicString-keyed maps to be queried by C string without
having to create a temporary AtomicString (see HTTPHeaderMap.)
The code for this is adapted from the equivalent in HashSet.h.

WebCore: Add convenience methods to Element and QualifiedName that take
char* instead of AtomicString, in preparation for removing the
implicit conversion between the two types (30187).
https://bugs.webkit.org/show_bug.cgi?id=31749

Reviewed by Darin Adler.

Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r51555 r51566  
     12009-12-01  Jens Alfke  <snej@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Added variants of find/contains/add that allow a foreign key type to be used.
     6        This will allow AtomicString-keyed maps to be queried by C string without
     7        having to create a temporary AtomicString (see HTTPHeaderMap.)
     8        The code for this is adapted from the equivalent in HashSet.h.
     9
     10        * wtf/HashMap.h:
     11        (WTF::HashMap::find):
     12        (WTF::HashMap::contains):
     13        (WTF::HashMap::add):
     14        * wtf/HashSet.h: Changed "method" to "function member" in a comment.
     15
    1162009-12-01  Gustavo Noronha Silva  <gustavo.noronha@collabora.co.uk>
    217
  • trunk/JavaScriptCore/wtf/HashMap.h

    r45120 r51566  
    8484        MappedType take(const KeyType&); // efficient combination of get with remove
    8585
     86        // An alternate version of find() that finds the object by hashing and comparing
     87        // with some other type, to avoid the cost of type conversion. HashTranslator
     88        // must have the following function members:
     89        //   static unsigned hash(const T&);
     90        //   static bool equal(const ValueType&, const T&);
     91        template<typename T, typename HashTranslator> iterator find(const T&);
     92        template<typename T, typename HashTranslator> const_iterator find(const T&) const;
     93        template<typename T, typename HashTranslator> bool contains(const T&) const;
     94
     95        // An alternate version of add() that finds the object by hashing and comparing
     96        // with some other type, to avoid the cost of type conversion if the object is already
     97        // in the table. HashTranslator must have the following function members:
     98        //   static unsigned hash(const T&);
     99        //   static bool equal(const ValueType&, const T&);
     100        //   static translate(ValueType&, const T&, unsigned hashCode);
     101        template<typename T, typename HashTranslator> pair<iterator, bool> add(const T&, const MappedType&);
     102
    86103    private:
    87104        pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&);
     
    108125    };
    109126
     127    template<typename ValueType, typename ValueTraits, typename T, typename Translator>
     128    struct HashMapTranslatorAdapter {
     129        typedef typename ValueType::first_type KeyType;
     130        typedef typename ValueType::second_type MappedType;
     131
     132        static unsigned hash(const T& key) { return Translator::hash(key); }
     133        static bool equal(const KeyType& a, const T& b) { return Translator::equal(a, b); }
     134        static void translate(ValueType& location, const T& key, const MappedType&, unsigned hashCode)
     135        {
     136            Translator::translate(location.first, key, hashCode);
     137        }
     138    };
     139
    110140    template<typename T, typename U, typename V, typename W, typename X>
    111141    inline void HashMap<T, U, V, W, X>::swap(HashMap& other)
     
    172202    {
    173203        return m_impl.contains(key);
     204    }
     205
     206    template<typename T, typename U, typename V, typename W, typename X>
     207    template<typename TYPE, typename HashTranslator>
     208    inline typename HashMap<T, U, V, W, X>::iterator
     209    HashMap<T, U, V, W, X>::find(const TYPE& value)
     210    {
     211        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     212        return m_impl.template find<TYPE, Adapter>(value);
     213    }
     214
     215    template<typename T, typename U, typename V, typename W, typename X>
     216    template<typename TYPE, typename HashTranslator>
     217    inline typename HashMap<T, U, V, W, X>::const_iterator
     218    HashMap<T, U, V, W, X>::find(const TYPE& value) const
     219    {
     220        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     221        return m_impl.template find<TYPE, Adapter>(value);
     222    }
     223
     224    template<typename T, typename U, typename V, typename W, typename X>
     225    template<typename TYPE, typename HashTranslator>
     226    inline bool
     227    HashMap<T, U, V, W, X>::contains(const TYPE& value) const
     228    {
     229        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     230        return m_impl.template contains<TYPE, Adapter>(value);
    174231    }
    175232
     
    192249        }
    193250        return result;
     251    }
     252
     253    template<typename T, typename U, typename V, typename W, typename X>
     254    template<typename TYPE, typename HashTranslator>
     255    pair<typename HashMap<T, U, V, W, X>::iterator, bool>
     256    HashMap<T, U, V, W, X>::add(const TYPE& key, const MappedType& value)
     257    {
     258        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     259        return m_impl.template addPassingHashCode<TYPE, MappedType, Adapter>(key, value);
    194260    }
    195261
  • trunk/JavaScriptCore/wtf/HashSet.h

    r48259 r51566  
    8282        // An alternate version of add() that finds the object by hashing and comparing
    8383        // with some other type, to avoid the cost of type conversion if the object is already
    84         // in the table. HashTranslator must have the following methods:
     84        // in the table. HashTranslator must have the following function members:
    8585        //   static unsigned hash(const T&);
    8686        //   static bool equal(const ValueType&, const T&);
  • trunk/WebCore/ChangeLog

    r51565 r51566  
     12009-12-01  Jens Alfke  <snej@chromium.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Add convenience methods to Element and QualifiedName that take
     6        char* instead of AtomicString, in preparation for removing the
     7        implicit conversion between the two types (30187).
     8        https://bugs.webkit.org/show_bug.cgi?id=31749
     9
     10        * dom/Element.cpp:
     11        (WebCore::Element::setCStringAttribute):  Equivalent to setAttribute.
     12        * dom/Element.h:
     13        * dom/QualifiedName.cpp:
     14        (WebCore::QualifiedName::init):  Shared impl of both constructors
     15        (WebCore::QualifiedName::QualifiedName):  New c'tor taking char*.
     16        * dom/QualifiedName.h:
     17        * platform/network/HTTPHeaderMap.cpp:
     18        (WebCore::CaseFoldingCStringTranslator):  Enables lookup by C string
     19        (WebCore::HTTPHeaderMap::get):  New variant that takes C string
     20        (WebCore::HTTPHeaderMap::contains):  New variant that takes C string
     21        (WebCore::HTTPHeaderMap::add):  New variant that takes C string
     22        * platform/network/HTTPHeaderMap.h:
     23        (WebCore::HTTPHeaderMap::get):
     24        (WebCore::HTTPHeaderMap::add):
     25        * platform/network/ResourceRequestBase.cpp:
     26        (WebCore::ResourceRequestBase::httpHeaderField):  New variant that takes C string
     27        * platform/network/ResourceRequestBase.h:
     28        (WebCore::ResourceRequestBase::setHTTPHeaderField):  Use symbolic names for headers
     29        * platform/network/ResourceResponseBase.cpp:
     30        (WebCore::ResourceResponseBase::httpHeaderField):  New variant that takes C string
     31        * platform/network/ResourceResponseBase.h:
     32
    1332009-12-01  Alexey Proskuryakov  <ap@apple.com>
    234
  • trunk/WebCore/dom/Element.cpp

    r50763 r51566  
    137137    setAttribute(name, value, ec);
    138138}
     139   
     140void Element::setCStringAttribute(const QualifiedName& name, const char* cStringValue)
     141{
     142    ExceptionCode ec;
     143    setAttribute(name, AtomicString(cStringValue), ec);
     144}
    139145
    140146void Element::setBooleanAttribute(const QualifiedName& name, bool b)
  • trunk/WebCore/dom/Element.h

    r48723 r51566  
    168168    void setAttribute(const QualifiedName&, const AtomicString& value);
    169169    void setBooleanAttribute(const QualifiedName& name, bool);
     170    // Please don't use setCStringAttribute in performance-sensitive code;
     171    // use a static AtomicString value instead to avoid the conversion overhead.
     172    void setCStringAttribute(const QualifiedName&, const char* cStringValue);
    170173
    171174    virtual NamedNodeMap* attributes() const;
  • trunk/WebCore/dom/QualifiedName.cpp

    r49798 r51566  
    5252static QNameSet* gNameCache;
    5353
    54 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
     54void QualifiedName::init(const AtomicString& p, const AtomicString& l, const AtomicString& n)
    5555{
    5656    if (!gNameCache)
     
    6161    if (!addResult.second)
    6262        m_impl->ref();
     63}
     64
     65QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
     66{
     67    init(p, l, n);
     68}
     69
     70QualifiedName::QualifiedName(const AtomicString& p, const char* l, const AtomicString& n)
     71{
     72    init(p, AtomicString(l), n);
    6373}
    6474
  • trunk/WebCore/dom/QualifiedName.h

    r50464 r51566  
    5858
    5959    QualifiedName(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI);
     60    QualifiedName(const AtomicString& prefix, const char* localName, const AtomicString& namespaceURI);
    6061    ~QualifiedName() { deref(); }
    6162#ifdef QNAME_DEFAULT_CONSTRUCTOR
     
    8990   
    9091private:
     92    void init(const AtomicString& prefix, const AtomicString& localName, const AtomicString& namespaceURI);
    9193    void ref() const { m_impl->ref(); }
    9294    void deref();
  • trunk/WebCore/platform/network/HTTPHeaderMap.cpp

    r49160 r51566  
    6060    }
    6161}
     62   
     63// Adapter that allows the HashMap to take C strings as keys.
     64struct CaseFoldingCStringTranslator {
     65    static unsigned hash(const char* cString)
     66    {
     67        return CaseFoldingHash::hash(cString, strlen(cString));
     68    }
     69   
     70    static bool equal(const AtomicString& key, const char* cString)
     71    {
     72        return equalIgnoringCase(key, cString);
     73    }
     74   
     75    static void translate(AtomicString& location, const char* cString, unsigned /*hash*/)
     76    {
     77        location = AtomicString(cString);
     78    }
     79};
     80
     81String HTTPHeaderMap::get(const char* name) const
     82{
     83    const_iterator i = find<const char*, CaseFoldingCStringTranslator>(name);
     84    if (i == end())
     85        return String();
     86    return i->second;
     87}
     88   
     89bool HTTPHeaderMap::contains(const char* name) const
     90{
     91    return find<const char*, CaseFoldingCStringTranslator>(name) != end();
     92}
     93
     94pair<HTTPHeaderMap::iterator, bool> HTTPHeaderMap::add(const char* name, const String& value)
     95{
     96    return HashMap<AtomicString, String, CaseFoldingHash>::add<const char*, CaseFoldingCStringTranslator>(name, value);
     97}
    6298
    6399} // namespace WebCore
  • trunk/WebCore/platform/network/HTTPHeaderMap.h

    r40162 r51566  
    4646
    4747        void adopt(std::auto_ptr<CrossThreadHTTPHeaderMapData>);
     48       
     49        String get(const AtomicString& name) const
     50        {
     51            return HashMap<AtomicString, String, CaseFoldingHash>::get(name);
     52        }
     53
     54        pair<iterator, bool> add(const AtomicString& name, const String& value)
     55        {
     56            return HashMap<AtomicString, String, CaseFoldingHash>::add(name, value);
     57        }
     58
     59        // Alternate accessors that are faster than converting the char* to AtomicString first.
     60        bool contains(const char*) const;
     61        String get(const char*) const;
     62        pair<iterator, bool> add(const char* name, const String& value);
     63       
    4864    };
    4965
  • trunk/WebCore/platform/network/ResourceRequestBase.cpp

    r50226 r51566  
    209209}
    210210
     211String ResourceRequestBase::httpHeaderField(const char* name) const
     212{
     213    updateResourceRequest();
     214   
     215    return m_httpHeaderFields.get(name);
     216}
     217
    211218void ResourceRequestBase::setHTTPHeaderField(const AtomicString& name, const String& value)
    212219{
     
    217224    if (url().protocolInHTTPFamily())
    218225        m_platformRequestUpdated = false;
     226}
     227
     228void ResourceRequestBase::setHTTPHeaderField(const char* name, const String& value)
     229{
     230    setHTTPHeaderField(AtomicString(name), value);
    219231}
    220232
  • trunk/WebCore/platform/network/ResourceRequestBase.h

    r51179 r51566  
    8080        const HTTPHeaderMap& httpHeaderFields() const;
    8181        String httpHeaderField(const AtomicString& name) const;
     82        String httpHeaderField(const char* name) const;
    8283        void setHTTPHeaderField(const AtomicString& name, const String& value);
     84        void setHTTPHeaderField(const char* name, const String& value);
    8385        void addHTTPHeaderField(const AtomicString& name, const String& value);
    8486        void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
  • trunk/WebCore/platform/network/ResourceResponseBase.cpp

    r49160 r51566  
    234234
    235235String ResourceResponseBase::httpHeaderField(const AtomicString& name) const
     236{
     237    lazyInit();
     238
     239    return m_httpHeaderFields.get(name);
     240}
     241
     242String ResourceResponseBase::httpHeaderField(const char* name) const
    236243{
    237244    lazyInit();
  • trunk/WebCore/platform/network/ResourceResponseBase.h

    r45461 r51566  
    7272   
    7373    String httpHeaderField(const AtomicString& name) const;
     74    String httpHeaderField(const char* name) const;
    7475    void setHTTPHeaderField(const AtomicString& name, const String& value);
    7576    const HTTPHeaderMap& httpHeaderFields() const;
Note: See TracChangeset for help on using the changeset viewer.