Skip to content

Commit 67f8e5b

Browse files
committed
Keep track of capacity of an ordered index
So we don't have to recalculate. If we want to ask how much memory the index currently needs to keep it under some limit, we will call capacity() quite often.
1 parent 7773457 commit 67f8e5b

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/ordered-index.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
#include "ordered-index.hpp"
1111

1212
#include <algorithm>
13-
#include <numeric>
14-
15-
std::size_t ordered_index_t::capacity() const noexcept
16-
{
17-
return std::accumulate(m_ranges.cbegin(), m_ranges.cend(), 0ULL,
18-
[](std::size_t sum, range_entry const &range) {
19-
return sum + range.index.capacity();
20-
});
21-
}
2213

2314
void ordered_index_t::add(osmid_t id, std::size_t offset)
2415
{
@@ -32,6 +23,7 @@ void ordered_index_t::add(osmid_t id, std::size_t offset)
3223
m_ranges.back().to = id - 1;
3324
}
3425
m_ranges.emplace_back(id, offset, m_block_size);
26+
m_capacity += m_block_size;
3527
if (m_block_size < max_block_size) {
3628
m_block_size <<= 1U;
3729
}

src/ordered-index.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ordered_index_t
8282
* is accurate for normal operations, but if there are huge gaps between
8383
* consecutive ids (> 2^32), less entries than this will fit.
8484
*/
85-
std::size_t capacity() const noexcept;
85+
std::size_t capacity() const noexcept { return m_capacity; }
8686

8787
/// The number of entries in the index.
8888
std::size_t size() const noexcept { return m_size; }
@@ -135,7 +135,7 @@ class ordered_index_t
135135
std::size_t used_memory() const noexcept
136136
{
137137
return m_ranges.capacity() * sizeof(range_entry) +
138-
capacity() * sizeof(second_level_index_entry);
138+
m_capacity * sizeof(second_level_index_entry);
139139
}
140140

141141
/**
@@ -146,6 +146,7 @@ class ordered_index_t
146146
{
147147
m_ranges.clear();
148148
m_ranges.shrink_to_fit();
149+
m_capacity = 0;
149150
m_size = 0;
150151
}
151152

@@ -185,6 +186,7 @@ class ordered_index_t
185186

186187
std::vector<range_entry> m_ranges;
187188
std::size_t m_block_size;
189+
std::size_t m_capacity = 0;
188190
std::size_t m_size = 0;
189191
}; // class ordered_index_t
190192

0 commit comments

Comments
 (0)