Skip to content

Commit e045dea

Browse files
Use pool in std::unordered_map
1 parent ac05d36 commit e045dea

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

include/util/pool_allocator.hpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
namespace osrm::util
1515
{
1616

17-
template <typename T>
18-
class PoolAllocator;
19-
20-
template <typename T>
2117
class MemoryManager
2218
{
2319
private:
2420
constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024;
21+
2522
public:
2623
static std::shared_ptr<MemoryManager> instance()
2724
{
@@ -33,42 +30,45 @@ class MemoryManager
3330
return instance;
3431
}
3532

33+
template <typename T>
3634
T *allocate(std::size_t n)
3735
{
38-
size_t free_list_index = get_next_power_of_two_exponent(n);
36+
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
3937
auto &free_list = free_lists_[free_list_index];
4038
const auto items_in_block = 1u << free_list_index;
4139
if (free_list.empty())
4240
{
4341
// Check if there is space in current block
4442
if (current_block_left_items_ < items_in_block)
4543
{
46-
allocate_block(items_in_block);
44+
allocate_block<T>(items_in_block);
4745
}
4846

4947
free_list.push_back(current_block_ptr_);
5048
current_block_left_items_ -= items_in_block;
51-
current_block_ptr_ += items_in_block;
49+
current_block_ptr_ += items_in_block * sizeof(T);
5250
}
53-
auto ptr = free_list.back();
51+
auto ptr = static_cast<T*>(free_list.back());
5452
free_list.pop_back();
5553
return ptr;
5654
}
5755

56+
template <typename T>
5857
void deallocate(T *p, std::size_t n) noexcept
5958
{
60-
size_t free_list_index = get_next_power_of_two_exponent(n);
59+
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
6160
free_lists_[free_list_index].push_back(p);
6261
}
6362

6463
~MemoryManager()
6564
{
66-
// std::cerr << "~MemoryManager()" << std::endl;
65+
std::cerr << "~MemoryManager()" << std::endl;
6766
for (auto block : blocks_)
6867
{
6968
std::free(block);
7069
}
7170
}
71+
7272
private:
7373
MemoryManager() = default;
7474
MemoryManager(const MemoryManager &) = delete;
@@ -80,25 +80,26 @@ class MemoryManager
8080
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
8181
}
8282

83+
template <typename T>
8384
void allocate_block(size_t items_in_block)
8485
{
8586
items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK);
8687

8788
size_t block_size = items_in_block * sizeof(T);
88-
T *block = static_cast<T *>(std::malloc(block_size));
89+
void *block = std::malloc(block_size);
8990
if (!block)
9091
{
9192
throw std::bad_alloc();
9293
}
9394
total_allocated_ += block_size;
9495
blocks_.push_back(block);
95-
current_block_ptr_ = block;
96+
current_block_ptr_ = static_cast<uint8_t*>(block);
9697
current_block_left_items_ = items_in_block;
9798
}
9899

99-
std::array<std::vector<T *>, 32> free_lists_;
100-
std::vector<T *> blocks_;
101-
T *current_block_ptr_ = nullptr;
100+
std::array<std::vector<void *>, 32> free_lists_;
101+
std::vector<void *> blocks_;
102+
uint8_t *current_block_ptr_ = nullptr;
102103
size_t current_block_left_items_ = 0;
103104

104105
size_t total_allocated_ = 0;
@@ -110,10 +111,10 @@ class PoolAllocator
110111
public:
111112
using value_type = T;
112113

113-
PoolAllocator() noexcept : pool(MemoryManager<T>::instance()) {};
114+
PoolAllocator() noexcept : pool(MemoryManager::instance()) {};
114115

115116
template <typename U>
116-
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager<T>::instance()) {}
117+
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager::instance()) {}
117118

118119
template <typename U>
119120
struct rebind
@@ -123,25 +124,26 @@ class PoolAllocator
123124

124125
T *allocate(std::size_t n)
125126
{
126-
return pool->allocate(n);
127+
return pool->allocate<T>(n);
127128
}
128129

129130
void deallocate(T *p, std::size_t n) noexcept
130131
{
131-
pool->deallocate(p, n);
132+
pool->deallocate<T>(p, n);
132133
}
133134

134-
~PoolAllocator() = default;
135+
~PoolAllocator() {
136+
std::cerr << "~PoolAllocator()" << std::endl;
137+
}
135138

136139
PoolAllocator(const PoolAllocator &) = default;
137140
PoolAllocator &operator=(const PoolAllocator &) = default;
138141
PoolAllocator(PoolAllocator &&) noexcept = default;
139142
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
140143

141144
private:
142-
std::shared_ptr<MemoryManager<T>> pool;
145+
std::shared_ptr<MemoryManager> pool;
143146
};
144-
145147
template <typename T, typename U>
146148
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
147149
{

include/util/query_heap.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
101101
public:
102102
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }
103103

104+
~UnorderedMapStorage() {
105+
std::cerr << "~UnorderedMapStorage()" << std::endl;
106+
}
107+
104108
Key &operator[](const NodeID node) { return nodes[node]; }
105109

106110
Key peek_index(const NodeID node) const
@@ -124,7 +128,7 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
124128
private:
125129
template <typename K, typename V>
126130
using UnorderedMap = std::
127-
unordered_map<K, V/*, std::hash<K>, std::equal_to<K>, PoolAllocator<std::pair<const K, V>>*/>;
131+
unordered_map<K, V, std::hash<K>, std::equal_to<K>, PoolAllocator<std::pair<const K, V>>>;
128132

129133
UnorderedMap<NodeID, Key> nodes;
130134
};

0 commit comments

Comments
 (0)