Skip to content

Commit ff3fac7

Browse files
committed
xrGame/cover_manager.cpp: optimize allocations, refactor compute_static_cover
1 parent 23e6928 commit ff3fac7

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/xrGame/cover_manager.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,42 @@ void CCoverManager::compute_static_cover()
7474
{
7575
clear();
7676
xr_delete(m_covers);
77-
m_covers = xr_new<CPointQuadTree>(
78-
ai().level_graph().header().box(), ai().level_graph().header().cell_size() * .5f, 8 * 65536, 4 * 65536);
79-
m_temp.resize(ai().level_graph().header().vertex_count());
80-
77+
8178
const CLevelGraph& graph = ai().level_graph();
82-
const u32 levelVertexCount = ai().level_graph().header().vertex_count();
79+
const LevelGraph::CHeader &levelGraphHeader = graph.header();
80+
const u32 levelVertexCount = levelGraphHeader.vertex_count();
81+
82+
m_covers = xr_new<CPointQuadTree>(levelGraphHeader.box(), levelGraphHeader.cell_size() * .5f, 8 * 65536, 4 * 65536);
8383

84+
// avoiding extra allocations with a static storage for m_covers
85+
static xr_vector<std::optional<CCoverPoint>> quadTreeStaticStorage;
86+
quadTreeStaticStorage.resize(levelVertexCount);
87+
m_temp.resize(levelVertexCount);
88+
8489
xr_parallel_for(TaskRange<u32>(0, levelVertexCount), [&](const TaskRange<u32>& range)
8590
{
8691
for (u32 i = range.begin(); i != range.end(); ++i)
8792
{
88-
const CLevelGraph::CLevelVertex& vertex = *graph.vertex(i);
89-
if (vertex.high_cover(0) + vertex.high_cover(1) + vertex.high_cover(2) + vertex.high_cover(3))
90-
{
91-
m_temp[i] = edge_vertex(i);
92-
continue;
93-
}
93+
m_temp[i] = false;
94+
quadTreeStaticStorage[i] = std::nullopt;
9495

95-
if (vertex.low_cover(0) + vertex.low_cover(1) + vertex.low_cover(2) + vertex.low_cover(3))
96+
const CLevelGraph::CLevelVertex& vertex = *graph.vertex(i);
97+
const int highCover = vertex.high_cover(0) + vertex.high_cover(1) + vertex.high_cover(2) + vertex.high_cover(3);
98+
const int lowCover = vertex.low_cover(0) + vertex.low_cover(1) + vertex.low_cover(2) + vertex.low_cover(3);
99+
100+
if (highCover || lowCover)
96101
{
97102
m_temp[i] = edge_vertex(i);
98-
continue;
103+
if (m_temp[i] && critical_cover(i))
104+
{
105+
quadTreeStaticStorage[i] = CCoverPoint(graph.vertex_position(graph.vertex(i)), i);
106+
}
99107
}
100-
101-
m_temp[i] = false;
102108
}
103109
});
104-
105-
for (u32 i = 0; i < levelVertexCount; ++i)
106-
if (m_temp[i] && critical_cover(i))
107-
m_covers->insert(xr_new<CCoverPoint>(ai().level_graph().vertex_position(ai().level_graph().vertex(i)), i));
110+
for (auto &p : quadTreeStaticStorage)
111+
if (p.has_value())
112+
m_covers->insert(&p.value());
108113

109114
VERIFY(!m_smart_covers_storage);
110115
m_smart_covers_storage = xr_new<smart_cover::storage>();

src/xrGame/cover_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class CCoverManager
4444

4545
protected:
4646
CPointQuadTree* m_covers;
47-
xr_vector<bool> m_temp;
47+
// vector<bool> is not applicable for `m_temp`
48+
// since it is filled in parallel_for (https://timsong-cpp.github.io/cppwp/container.requirements.dataraces).
49+
xr_vector<int> m_temp;
4850
mutable PointVector m_nearest;
4951

5052
private:

0 commit comments

Comments
 (0)