The hashmap uses the memorypool for allocating/deallocating its Nodes
(It is faster and it saves approximately 70kB the DS or other small devices will appreciate having) svn-id: r31321
This commit is contained in:
parent
411a588850
commit
a84d1ea78b
2 changed files with 38 additions and 6 deletions
|
@ -58,6 +58,12 @@
|
||||||
#include "common/str.h"
|
#include "common/str.h"
|
||||||
#include "common/util.h"
|
#include "common/util.h"
|
||||||
|
|
||||||
|
#define USE_HASHMAP_MEMORY_POOL
|
||||||
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
||||||
|
#include "common/memorypool.h"
|
||||||
|
#include <new>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
// The table sizes ideally are primes. We use a helper function to find
|
// The table sizes ideally are primes. We use a helper function to find
|
||||||
|
@ -70,6 +76,7 @@ uint nextTableSize(uint x);
|
||||||
// hash table that is too small).
|
// hash table that is too small).
|
||||||
//#define DEBUG_HASH_COLLISIONS
|
//#define DEBUG_HASH_COLLISIONS
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HashMap<Key,Val> maps objects of type Key to objects of type Val.
|
* HashMap<Key,Val> maps objects of type Key to objects of type Val.
|
||||||
* For each used Key type, we need an "uint hashit(Key,uint)" function
|
* For each used Key type, we need an "uint hashit(Key,uint)" function
|
||||||
|
@ -98,13 +105,28 @@ public:
|
||||||
Node(const Key &key) : _key(key), _value() {}
|
Node(const Key &key) : _key(key), _value() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
||||||
|
MemoryPool _nodePool;
|
||||||
|
|
||||||
Node *allocNode(const Key& key) {
|
Node *allocNode(const Key& key) {
|
||||||
|
void* mem = _nodePool.malloc();
|
||||||
|
return new (mem) Node(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeNode(Node* node) {
|
||||||
|
node->~Node();
|
||||||
|
_nodePool.free(node);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Node* allocNode(const Key& key) {
|
||||||
return new Node(key);
|
return new Node(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void freeNode(Node *node) {
|
void freeNode(Node *node) {
|
||||||
delete node;
|
delete node;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Node **_arr; // hashtable of size arrsize.
|
Node **_arr; // hashtable of size arrsize.
|
||||||
uint _arrsize, _nele;
|
uint _arrsize, _nele;
|
||||||
|
@ -328,8 +350,11 @@ public:
|
||||||
* Base constructor, creates an empty hashmap.
|
* Base constructor, creates an empty hashmap.
|
||||||
*/
|
*/
|
||||||
template <class Key, class Val, class HashFunc, class EqualFunc>
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
||||||
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap() :
|
||||||
: _defaultVal() {
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
||||||
|
_nodePool(sizeof(Node)),
|
||||||
|
#endif
|
||||||
|
_defaultVal() {
|
||||||
_arrsize = nextTableSize(0);
|
_arrsize = nextTableSize(0);
|
||||||
_arr = new Node *[_arrsize];
|
_arr = new Node *[_arrsize];
|
||||||
assert(_arr != NULL);
|
assert(_arr != NULL);
|
||||||
|
@ -349,8 +374,11 @@ HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
|
||||||
* to heap buffers for the internal storage.
|
* to heap buffers for the internal storage.
|
||||||
*/
|
*/
|
||||||
template <class Key, class Val, class HashFunc, class EqualFunc>
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
||||||
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map)
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map) :
|
||||||
: _defaultVal() {
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
||||||
|
_nodePool(sizeof(Node)),
|
||||||
|
#endif
|
||||||
|
_defaultVal() {
|
||||||
assign(map);
|
assign(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ MemoryPool::MemoryPool(size_t chunkSize) {
|
||||||
|
|
||||||
MemoryPool::~MemoryPool() {
|
MemoryPool::~MemoryPool() {
|
||||||
for(size_t i=0; i<_pages.size(); ++i)
|
for(size_t i=0; i<_pages.size(); ++i)
|
||||||
free(_pages[i]);
|
::free(_pages[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* MemoryPool::malloc() {
|
void* MemoryPool::malloc() {
|
||||||
|
@ -83,12 +83,16 @@ void MemoryPool::freeUnusedPages() {
|
||||||
iterator = *(void**)iterator;
|
iterator = *(void**)iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t freedPagesCount = 0;
|
||||||
for(size_t i=0; i<_pages.size(); ++i) {
|
for(size_t i=0; i<_pages.size(); ++i) {
|
||||||
if(numberOfFreeChunksPerPage[i] == CHUNK_PAGE_SIZE) {
|
if(numberOfFreeChunksPerPage[i] == CHUNK_PAGE_SIZE) {
|
||||||
free(_pages[i]);
|
::free(_pages[i]);
|
||||||
_pages[i] = NULL; // TODO : Remove NULL values
|
_pages[i] = NULL; // TODO : Remove NULL values
|
||||||
|
++freedPagesCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("%d freed pages\n", freedPagesCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue