Remove RefPtrHashMap

The recently added 'HashTraits::PeekInType' type allows us to remove
HashMap<RefPtr..> template specialization without loosing the efficiency
and without bringing any significant changes and complexity to the HashMap
class.

To get rid of 'RefPtrHashMap' a new 'RefPtrValuePeeker' type was created
and used as 'HashTraits::PeekInType' for RefPtr. The 'RefPtrValuePeeker'
class can be constructed either from RefPtr, PassRefPtr or a plain pointer,
then it behaves like a plain pointer itself within HashTable methods.

The proposed change brings the following benefits:
1) Removes tons of duplicated code
2) Allows avoiding ref-count churn also at HashSet<RefPtr>

Review URL: https://codereview.chromium.org/184233006

git-svn-id: svn://svn.chromium.org/blink/trunk@168593 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent ac9b8162
...@@ -526,6 +526,4 @@ namespace WTF { ...@@ -526,6 +526,4 @@ namespace WTF {
using WTF::HashMap; using WTF::HashMap;
#include "wtf/RefPtrHashMap.h"
#endif /* WTF_HashMap_h */ #endif /* WTF_HashMap_h */
...@@ -170,7 +170,7 @@ namespace WTF { ...@@ -170,7 +170,7 @@ namespace WTF {
static const bool hasIsEmptyValueFunction = true; static const bool hasIsEmptyValueFunction = true;
static bool isEmptyValue(const RefPtr<P>& value) { return !value; } static bool isEmptyValue(const RefPtr<P>& value) { return !value; }
typedef const RefPtr<P>& PeekInType; typedef RefPtrValuePeeker<P> PeekInType;
typedef RefPtr<P>* IteratorGetType; typedef RefPtr<P>* IteratorGetType;
typedef const RefPtr<P>* IteratorConstGetType; typedef const RefPtr<P>* IteratorConstGetType;
typedef RefPtr<P>& IteratorReferenceType; typedef RefPtr<P>& IteratorReferenceType;
......
...@@ -188,6 +188,16 @@ namespace WTF { ...@@ -188,6 +188,16 @@ namespace WTF {
return p.get(); return p.get();
} }
template<typename T> class RefPtrValuePeeker {
public:
ALWAYS_INLINE RefPtrValuePeeker(T* p): m_ptr(p) { }
template<typename U> RefPtrValuePeeker(const RefPtr<U>& p): m_ptr(p.get()) { }
template<typename U> RefPtrValuePeeker(const PassRefPtr<U>& p): m_ptr(p.get()) { }
ALWAYS_INLINE operator T*() const { return m_ptr; }
private:
T* m_ptr;
};
} // namespace WTF } // namespace WTF
using WTF::RefPtr; using WTF::RefPtr;
......
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef RefPtrHashMap_h
#define RefPtrHashMap_h
#include "wtf/DefaultAllocator.h"
namespace WTF {
// This specialization is a copy of HashMap for use with RefPtr keys, with overloaded functions
// to allow for lookup by pointer instead of RefPtr, avoiding ref-count churn.
// FIXME: Find a way to do this with traits that doesn't require a copy of the HashMap template.
template<typename T, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
class HashMap<RefPtr<T>, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, DefaultAllocator> {
private:
typedef KeyTraitsArg KeyTraits;
typedef MappedTraitsArg MappedTraits;
typedef KeyValuePairHashTraits<KeyTraits, MappedTraits> ValueTraits;
public:
typedef typename KeyTraits::TraitType KeyType;
typedef T* RawKeyType;
typedef typename MappedTraits::TraitType MappedType;
typedef typename ValueTraits::TraitType ValueType;
private:
typedef typename MappedTraits::PassInType MappedPassInType;
typedef typename MappedTraits::PassOutType MappedPassOutType;
typedef typename MappedTraits::PeekOutType MappedPeekType;
typedef typename ReferenceTypeMaker<MappedPassInType>::ReferenceType MappedPassInReferenceType;
typedef HashArg HashFunctions;
typedef HashTable<KeyType, ValueType, KeyValuePairKeyExtractor,
HashFunctions, ValueTraits, KeyTraits, DefaultAllocator> HashTableType;
typedef HashMapTranslator<ValueTraits, HashFunctions>
Translator;
public:
typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
typedef typename HashTableType::AddResult AddResult;
void swap(HashMap&);
unsigned size() const;
unsigned capacity() const;
bool isEmpty() const;
// iterators iterate over pairs of keys and values
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
iterator find(const KeyType&);
iterator find(RawKeyType);
const_iterator find(const KeyType&) const;
const_iterator find(RawKeyType) const;
bool contains(const KeyType&) const;
bool contains(RawKeyType) const;
MappedPeekType get(const KeyType&) const;
MappedPeekType get(RawKeyType) const;
MappedPeekType inlineGet(RawKeyType) const;
// replaces value but not key if key is already present
// return value is a pair of the iterator to the key location,
// and a boolean that's true if a new value was actually added
AddResult set(const KeyType&, MappedPassInType);
AddResult set(RawKeyType, MappedPassInType);
// does nothing if key is already present
// return value is a pair of the iterator to the key location,
// and a boolean that's true if a new value was actually added
AddResult add(const KeyType&, MappedPassInType);
AddResult add(RawKeyType, MappedPassInType);
void remove(const KeyType&);
void remove(RawKeyType);
void remove(iterator);
void clear();
MappedPassOutType take(const KeyType&); // efficient combination of get with remove
MappedPassOutType take(RawKeyType); // efficient combination of get with remove
private:
AddResult inlineAdd(const KeyType&, MappedPassInReferenceType);
AddResult inlineAdd(RawKeyType, MappedPassInReferenceType);
HashTableType m_impl;
};
template<typename T, typename U, typename V, typename W, typename X>
inline void HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::swap(HashMap& other)
{
m_impl.swap(other.m_impl);
}
template<typename T, typename U, typename V, typename W, typename X>
inline unsigned HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::size() const
{
return m_impl.size();
}
template<typename T, typename U, typename V, typename W, typename X>
inline unsigned HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::capacity() const
{
return m_impl.capacity();
}
template<typename T, typename U, typename V, typename W, typename X>
inline bool HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::isEmpty() const
{
return m_impl.isEmpty();
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::begin()
{
return m_impl.begin();
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::end()
{
return m_impl.end();
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::const_iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::begin() const
{
return m_impl.begin();
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::const_iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::end() const
{
return m_impl.end();
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::find(const KeyType& key)
{
return m_impl.find(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::find(RawKeyType key)
{
return m_impl.template find<Translator>(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::const_iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::find(const KeyType& key) const
{
return m_impl.find(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::const_iterator HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::find(RawKeyType key) const
{
return m_impl.template find<Translator>(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline bool HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::contains(const KeyType& key) const
{
return m_impl.contains(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline bool HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::contains(RawKeyType key) const
{
return m_impl.template contains<Translator>(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::inlineAdd(const KeyType& key, MappedPassInReferenceType mapped)
{
return m_impl.template add<Translator>(key, mapped);
}
template<typename T, typename U, typename V, typename W, typename X>
inline typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::inlineAdd(RawKeyType key, MappedPassInReferenceType mapped)
{
return m_impl.template add<Translator>(key, mapped);
}
template<typename T, typename U, typename V, typename W, typename X>
typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::set(const KeyType& key, MappedPassInType mapped)
{
AddResult result = inlineAdd(key, mapped);
if (!result.isNewEntry) {
// The inlineAdd call above found an existing hash table entry; we need to set the mapped value.
MappedTraits::store(mapped, result.storedValue->value);
}
return result;
}
template<typename T, typename U, typename V, typename W, typename X>
typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::set(RawKeyType key, MappedPassInType mapped)
{
AddResult result = inlineAdd(key, mapped);
if (!result.isNewEntry) {
// The inlineAdd call above found an existing hash table entry; we need to set the mapped value.
MappedTraits::store(mapped, result.storedValue->value);
}
return result;
}
template<typename T, typename U, typename V, typename W, typename X>
typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::add(const KeyType& key, MappedPassInType mapped)
{
return inlineAdd(key, mapped);
}
template<typename T, typename U, typename V, typename W, typename X>
typename HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::AddResult
HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::add(RawKeyType key, MappedPassInType mapped)
{
return inlineAdd(key, mapped);
}
template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::MappedPeekType
HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::get(const KeyType& key) const
{
ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
if (!entry)
return MappedTraits::peek(MappedTraits::emptyValue());
return MappedTraits::peek(entry->value);
}
template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::MappedPeekType
inline HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::inlineGet(RawKeyType key) const
{
ValueType* entry = const_cast<HashTableType&>(m_impl).template lookup<Translator>(key);
if (!entry)
return MappedTraits::peek(MappedTraits::emptyValue());
return MappedTraits::peek(entry->value);
}
template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::MappedPeekType
HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::get(RawKeyType key) const
{
return inlineGet(key);
}
template<typename T, typename U, typename V, typename W, typename X>
inline void HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::remove(iterator it)
{
if (it.m_impl == m_impl.end())
return;
m_impl.remove(it.m_impl);
}
template<typename T, typename U, typename V, typename W, typename X>
inline void HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::remove(const KeyType& key)
{
remove(find(key));
}
template<typename T, typename U, typename V, typename W, typename X>
inline void HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::remove(RawKeyType key)
{
remove(find(key));
}
template<typename T, typename U, typename V, typename W, typename X>
inline void HashMap<RefPtr<T>, U, V, W, X, DefaultAllocator>::clear()
{
m_impl.clear();
}
template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::MappedPassOutType
HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::take(const KeyType& key)
{
iterator it = find(key);
if (it == end())
return MappedTraits::passOut(MappedTraits::emptyValue());
MappedPassOutType result = MappedTraits::passOut(it->value);
remove(it);
return result;
}
template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::MappedPassOutType
HashMap<RefPtr<T>, U, V, W, MappedTraits, DefaultAllocator>::take(RawKeyType key)
{
iterator it = find(key);
if (it == end())
return MappedTraits::passOut(MappedTraits::emptyValue());
MappedPassOutType result = MappedTraits::passOut(it->value);
remove(it);
return result;
}
} // namespace WTF
#endif // RefPtrHashMap_h
...@@ -99,7 +99,6 @@ ...@@ -99,7 +99,6 @@
'RefCountedLeakCounter.cpp', 'RefCountedLeakCounter.cpp',
'RefCountedLeakCounter.h', 'RefCountedLeakCounter.h',
'RefPtr.h', 'RefPtr.h',
'RefPtrHashMap.h',
'RetainPtr.h', 'RetainPtr.h',
'SHA1.cpp', 'SHA1.cpp',
'SHA1.h', 'SHA1.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment