use Chaining in OpenHashTable.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20822 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos
2007-04-25 18:55:05 +00:00
parent f96df43ff5
commit 2586c25e31
5 changed files with 100 additions and 86 deletions
+68 -75
View File
@@ -12,12 +12,14 @@
#include <KernelExport.h> #include <KernelExport.h>
// the Definition template must have three methods: `HashKey', `Hash' and // the Definition template must have three methods: `HashKey', `Hash',
// `Compare'. It must also define several types as shown in the following // `Compare' and `GetLink;. It must also define several types as shown in the
// example: // following example:
// //
// struct Foo { // struct Foo : HashTableLink<Foo> {
// int bar; // int bar;
//
// HashTableLink<Foo> otherLink;
// }; // };
// //
// struct HashTableDefinition { // struct HashTableDefinition {
@@ -29,14 +31,17 @@
// static size_t Hash(void *parent, Foo *value) { return HashKey(value->bar); } // static size_t Hash(void *parent, Foo *value) { return HashKey(value->bar); }
// static bool Compare(void *parent, int key, Foo *value) // static bool Compare(void *parent, int key, Foo *value)
// { return value->bar == key; } // { return value->bar == key; }
// static HashTableLink<Foo> *GetLink(void *parent, Foo *value)
// { return value; }
// }; // };
// This hash table implementation uses open addressing vs. the more common template<typename Type>
// chaining. This approach is advantageous as the number of expected collisions struct HashTableLink {
// is the same (property of the hash function) while not wasting one additional Type *fNext;
// word per item and having better cache locality. The usage of quadratic };
// probing reduces the effectiveness of cache locality but prevents clustering.
template<typename Definition, bool CheckDuplicates = false> template<typename Definition, bool AutoExpand = true,
bool CheckDuplicates = false>
class OpenHashTable { class OpenHashTable {
public: public:
typedef typename Definition::ParentType ParentType; typedef typename Definition::ParentType ParentType;
@@ -53,8 +58,7 @@ public:
// 50 / 256 = 19.53125% // 50 / 256 = 19.53125%
OpenHashTable(const ParentType &parent, size_t initialSize = kMinimumSize) OpenHashTable(const ParentType &parent, size_t initialSize = kMinimumSize)
: fParent(parent), fItemCount(0), fTable(NULL), : fParent(parent), fItemCount(0), fTable(NULL)
fDeletedToken((ValueType *)(((char *)0) - 1))
{ {
if (initialSize < kMinimumSize) if (initialSize < kMinimumSize)
initialSize = kMinimumSize; initialSize = kMinimumSize;
@@ -72,44 +76,39 @@ public:
ValueType *Lookup(const KeyType &key) const ValueType *Lookup(const KeyType &key) const
{ {
size_t index = Definition::HashKey(fParent, key) & (fTableSize - 1); size_t index = Definition::HashKey(fParent, key) & (fTableSize - 1);
size_t f = 0; ValueType *slot = fTable[index];
while (true) { while (slot) {
ValueType *slot = fTable[index]; if (Definition::Compare(fParent, key, slot))
break;
if (slot == NULL) slot = _Link(slot)->fNext;
return NULL;
else if (!_IsDeleted(slot)
&& Definition::Compare(fParent, key, slot))
return slot;
index = _NextSlot(f, index, fTableSize);
} }
return slot;
} }
bool Insert(ValueType *value) void Insert(ValueType *value)
{ {
if (fItemCount >= (fTableSize * 200 / 256)) { if (AutoExpand && fItemCount >= (fTableSize * 200 / 256))
if (!_Resize(fTableSize * 2)) _Resize(fTableSize * 2);
return false;
}
InsertUnchecked(value); InsertUnchecked(value);
return true;
} }
void InsertUnchecked(ValueType *value) void InsertUnchecked(ValueType *value)
{ {
if (CheckDuplicates) { if (CheckDuplicates) {
for (size_t i = 0; i < fTableSize; i++) { for (size_t i = 0; i < fTableSize; i++) {
if (fTable[i] == value) ValueType *bucket = fTable[i];
panic("HashTable: item already in table"); while (bucket) {
if (bucket == value)
panic("Hash Table: value already in table.");
bucket = _Link(bucket)->fNext;
}
} }
} }
ValueType *previous = _Insert(fTable, fTableSize, value); _Insert(fTable, fTableSize, value);
if (_IsDeleted(previous))
fDeletedCount--;
fItemCount++; fItemCount++;
} }
@@ -117,64 +116,52 @@ public:
{ {
RemoveUnchecked(value); RemoveUnchecked(value);
if (fTableSize > kMinimumSize && fItemCount < (fTableSize * 50 / 256)) if (AutoExpand && fTableSize > kMinimumSize
&& fItemCount < (fTableSize * 50 / 256))
_Resize(fTableSize / 2); _Resize(fTableSize / 2);
} }
void RemoveUnchecked(ValueType *value) void RemoveUnchecked(ValueType *value)
{ {
size_t index = Definition::Hash(fParent, value) & (fTableSize - 1); size_t index = Definition::Hash(fParent, value) & (fTableSize - 1);
size_t f = 0; ValueType *previous = NULL, *slot = fTable[index];
while (true) { while (slot) {
if (fTable[index] == value) { ValueType *next = _Link(slot)->fNext;
fTable[index] = (ValueType *)fDeletedToken;
if (value == slot) {
if (previous)
_Link(previous)->fNext = next;
else
fTable[index] = next;
break; break;
} }
index = _NextSlot(f, index, fTableSize); previous = slot;
slot = next;
} }
if (CheckDuplicates) { if (CheckDuplicates) {
for (size_t i = 0; i < fTableSize; i++) { for (size_t i = 0; i < fTableSize; i++) {
if (fTable[i] == value) ValueType *bucket = fTable[i];
panic("HashTable: item removed, but still in table."); while (bucket) {
if (bucket == value)
panic("Hash Table: duplicate detected.");
bucket = _Link(bucket)->fNext;
}
} }
} }
fItemCount--; fItemCount--;
fDeletedCount++;
} }
private: private:
ValueType *_Insert(ValueType **table, size_t tableSize, ValueType *value) void _Insert(ValueType **table, size_t tableSize, ValueType *value)
{ {
size_t index = Definition::Hash(fParent, value) & (tableSize - 1); size_t index = Definition::Hash(fParent, value) & (tableSize - 1);
size_t f = 0;
while (true) { _Link(value)->fNext = table[index];
if (table[index] == NULL || table[index] == fDeletedToken) { table[index] = value;
ValueType *previous = table[index];
table[index] = value;
return previous;
}
index = _NextSlot(f, index, tableSize);
}
return NULL;
}
static size_t _NextSlot(size_t &f, size_t index, size_t tableSize)
{
// quadratic probing
f++;
return (index + f) & (tableSize - 1);
}
bool _IsDeleted(ValueType *value) const
{
return value == fDeletedToken;
} }
bool _Resize(size_t newSize) bool _Resize(size_t newSize)
@@ -188,24 +175,30 @@ private:
if (fTable) { if (fTable) {
for (size_t i = 0; i < fTableSize; i++) { for (size_t i = 0; i < fTableSize; i++) {
if (fTable[i] && !_IsDeleted(fTable[i])) ValueType *bucket = fTable[i];
_Insert(newTable, newSize, fTable[i]); while (bucket) {
ValueType *next = _Link(bucket)->fNext;
_Insert(newTable, newSize, bucket);
bucket = next;
}
} }
delete [] fTable; delete [] fTable;
} }
fTableSize = newSize; fTableSize = newSize;
fDeletedCount = 0;
fTable = newTable; fTable = newTable;
return true; return true;
} }
ParentType fParent; HashTableLink<ValueType> *_Link(ValueType *bucket) const
size_t fTableSize, fItemCount, fDeletedCount; {
ValueType **fTable; return Definition::GetLink(fParent, bucket);
}
const ValueType *fDeletedToken; ParentType fParent;
size_t fTableSize, fItemCount;
ValueType **fTable;
}; };
#endif #endif
@@ -53,6 +53,14 @@ ConnectionHashDefinition::Compare(EndpointManager *manager, const KeyType &key,
} }
HashTableLink<TCPEndpoint> *
ConnectionHashDefinition::GetLink(EndpointManager *manager,
TCPEndpoint *endpoint)
{
return &endpoint->fConnectionHashLink;
}
size_t size_t
EndpointHashDefinition::HashKey(EndpointManager *manager, uint16 port) EndpointHashDefinition::HashKey(EndpointManager *manager, uint16 port)
{ {
@@ -75,6 +83,14 @@ EndpointHashDefinition::Compare(EndpointManager *manager, uint16 port,
} }
HashTableLink<TCPEndpoint> *
EndpointHashDefinition::GetLink(EndpointManager *manager,
TCPEndpoint *endpoint)
{
return &endpoint->fEndpointHashLink;
}
EndpointManager::EndpointManager(net_domain *domain) EndpointManager::EndpointManager(net_domain *domain)
: fDomain(domain), fConnectionHash(this), fEndpointHash(this) : fDomain(domain), fConnectionHash(this), fEndpointHash(this)
{ {
@@ -141,9 +157,7 @@ EndpointManager::SetConnection(TCPEndpoint *endpoint,
endpoint->LocalAddress().SetTo(*local); endpoint->LocalAddress().SetTo(*local);
endpoint->PeerAddress().SetTo(peer); endpoint->PeerAddress().SetTo(peer);
if (!fConnectionHash.Insert(endpoint)) fConnectionHash.Insert(endpoint);
return B_NO_MEMORY;
return B_OK; return B_OK;
} }
@@ -170,9 +184,7 @@ EndpointManager::SetPassive(TCPEndpoint *endpoint)
return EADDRINUSE; return EADDRINUSE;
endpoint->PeerAddress().SetTo(*passive); endpoint->PeerAddress().SetTo(*passive);
if (!fConnectionHash.Insert(endpoint)) fConnectionHash.Insert(endpoint);
return B_NO_MEMORY;
return B_OK; return B_OK;
} }
@@ -33,6 +33,8 @@ struct ConnectionHashDefinition {
static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint); static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint);
static bool Compare(EndpointManager *manager, const KeyType &key, static bool Compare(EndpointManager *manager, const KeyType &key,
TCPEndpoint *endpoint); TCPEndpoint *endpoint);
static HashTableLink<TCPEndpoint> *GetLink(EndpointManager *manager,
TCPEndpoint *endpoint);
}; };
@@ -45,6 +47,8 @@ struct EndpointHashDefinition {
static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint); static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint);
static bool Compare(EndpointManager *manager, uint16 port, static bool Compare(EndpointManager *manager, uint16 port,
TCPEndpoint *endpoint); TCPEndpoint *endpoint);
static HashTableLink<TCPEndpoint> *GetLink(EndpointManager *manager,
TCPEndpoint *endpoint);
}; };
@@ -81,8 +85,8 @@ class EndpointManager : public DoublyLinkedListLinkImpl<EndpointManager> {
net_domain *fDomain; net_domain *fDomain;
OpenHashTable<ConnectionHashDefinition> fConnectionHash; OpenHashTable<ConnectionHashDefinition, true, true> fConnectionHash;
OpenHashTable<EndpointHashDefinition> fEndpointHash; OpenHashTable<EndpointHashDefinition, true, true> fEndpointHash;
benaphore fLock; benaphore fLock;
}; };
@@ -74,7 +74,7 @@
#endif #endif
// Initial estimate for packet round trip time (RTT) // Initial estimate for packet round trip time (RTT)
#define TCP_INITIAL_RTT 4000000 #define TCP_INITIAL_RTT 2000000
// constants for the fFlags field // constants for the fFlags field
enum { enum {
@@ -18,6 +18,7 @@
#include <net_stack.h> #include <net_stack.h>
#include <util/AutoLock.h> #include <util/AutoLock.h>
#include <util/DoublyLinkedList.h> #include <util/DoublyLinkedList.h>
#include <util/OpenHashTable.h>
#include <stddef.h> #include <stddef.h>
@@ -134,8 +135,12 @@ class TCPEndpoint : public net_protocol {
EndpointManager *fManager; EndpointManager *fManager;
TCPEndpoint *fConnectionHashNext; HashTableLink<TCPEndpoint> fConnectionHashLink;
TCPEndpoint *fEndpointHashNext; HashTableLink<TCPEndpoint> fEndpointHashLink;
friend class ConnectionHashDefinition;
friend class EndpointHashDefinition;
TCPEndpoint *fEndpointNextWithSamePort; TCPEndpoint *fEndpointNextWithSamePort;
recursive_lock fLock; recursive_lock fLock;