Skip to content

Commit 979c722

Browse files
committed
Core/Maps: Tiny optimization for TerrainInfo::CleanUpGrids
1 parent a0c8e02 commit 979c722

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

src/server/game/Maps/Map.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,9 @@ i_mapEntry(sMapStore.LookupEntry(id)), i_spawnMode(SpawnMode), i_InstanceId(Inst
138138
m_unloadTimer(0), m_VisibleDistance(DEFAULT_VISIBILITY_DISTANCE), m_mapRefIter(m_mapRefManager.end()),
139139
m_VisibilityNotifyPeriod(DEFAULT_VISIBILITY_NOTIFY_PERIOD),
140140
m_activeNonPlayersIter(m_activeNonPlayers.end()), _transportsUpdateIter(_transports.end()),
141-
i_gridExpiry(expiry), m_terrain(sTerrainMgr.LoadTerrain(id)), m_forceEnabledNavMeshFilterFlags(0), m_forceDisabledNavMeshFilterFlags(0),
141+
i_gridExpiry(expiry), m_terrain(sTerrainMgr.LoadTerrain(id)), m_forceEnabledNavMeshFilterFlags(0), m_forceDisabledNavMeshFilterFlags(0), i_grids(),
142142
i_scriptLock(false), _respawnTimes(std::make_unique<RespawnListContainer>()), _respawnCheckTimer(0), _vignetteUpdateTimer(5200, 5200)
143143
{
144-
for (uint32 x = 0; x < MAX_NUMBER_OF_GRIDS; ++x)
145-
{
146-
for (uint32 y = 0; y < MAX_NUMBER_OF_GRIDS; ++y)
147-
{
148-
//z code
149-
setNGrid(nullptr, x, y);
150-
}
151-
}
152-
153-
_zonePlayerCountMap.clear();
154-
155144
//lets initialize visibility distance for map
156145
Map::InitVisibilityDistance();
157146

src/server/game/Maps/TerrainMgr.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "World.h"
3333
#include <G3D/g3dmath.h>
3434

35-
TerrainInfo::TerrainInfo(uint32 mapId) : _mapId(mapId), _parentTerrain(nullptr), _cleanupTimer(randtime(CleanupInterval / 2, CleanupInterval))
35+
TerrainInfo::TerrainInfo(uint32 mapId) : _mapId(mapId), _parentTerrain(nullptr), _loadedGrids(), _cleanupTimer(randtime(CleanupInterval / 2, CleanupInterval))
3636
{
3737
}
3838

@@ -180,7 +180,7 @@ void TerrainInfo::LoadMapAndVMapImpl(int32 gx, int32 gy)
180180
for (std::shared_ptr<TerrainInfo> const& childTerrain : _childTerrain)
181181
childTerrain->LoadMapAndVMapImpl(gx, gy);
182182

183-
_loadedGrids[GetBitsetIndex(gx, gy)] = true;
183+
_loadedGrids[gx] |= UI64LIT(1) << gy;
184184
}
185185

186186
void TerrainInfo::LoadMMapInstanceImpl(uint32 mapId, uint32 instanceId)
@@ -279,7 +279,7 @@ void TerrainInfo::UnloadMapImpl(int32 gx, int32 gy)
279279
for (std::shared_ptr<TerrainInfo> const& childTerrain : _childTerrain)
280280
childTerrain->UnloadMapImpl(gx, gy);
281281

282-
_loadedGrids[GetBitsetIndex(gx, gy)] = false;
282+
_loadedGrids[gx] &= ~(UI64LIT(1) << gy);
283283
}
284284

285285
void TerrainInfo::UnloadMMapInstanceImpl(uint32 mapId, uint32 instanceId)
@@ -294,7 +294,7 @@ GridMap* TerrainInfo::GetGrid(uint32 mapId, float x, float y, bool loadIfMissing
294294
int32 gy = (int)(CENTER_GRID_ID - y / SIZE_OF_GRIDS); //grid y
295295

296296
// ensure GridMap is loaded
297-
if (!_loadedGrids[GetBitsetIndex(gx, gy)] && loadIfMissing)
297+
if (!(_loadedGrids[gx] & (UI64LIT(1) << gy)) && loadIfMissing)
298298
{
299299
std::lock_guard<std::mutex> lock(_loadMutex);
300300
LoadMapAndVMapImpl(gx, gy);
@@ -303,7 +303,7 @@ GridMap* TerrainInfo::GetGrid(uint32 mapId, float x, float y, bool loadIfMissing
303303
GridMap* grid = _gridMap[gx][gy].get();
304304
if (mapId != GetId())
305305
{
306-
auto childMapItr = std::find_if(_childTerrain.begin(), _childTerrain.end(), [mapId](std::shared_ptr<TerrainInfo> const& childTerrain) { return childTerrain->GetId() == mapId; });
306+
auto childMapItr = std::ranges::find(_childTerrain, mapId, [](std::shared_ptr<TerrainInfo> const& childTerrain) { return childTerrain->GetId(); });
307307
if (childMapItr != _childTerrain.end() && (*childMapItr)->_gridMap[gx][gy])
308308
grid = (*childMapItr)->GetGrid(mapId, x, y, false);
309309
}
@@ -319,9 +319,10 @@ void TerrainInfo::CleanUpGrids(uint32 diff)
319319

320320
// delete those GridMap objects which have refcount = 0
321321
for (int32 x = 0; x < MAX_NUMBER_OF_GRIDS; ++x)
322-
for (int32 y = 0; y < MAX_NUMBER_OF_GRIDS; ++y)
323-
if (_loadedGrids[GetBitsetIndex(x, y)] && !_referenceCountFromMap[x][y])
324-
UnloadMapImpl(x, y);
322+
if (_loadedGrids[x])
323+
for (int32 y = 0; y < MAX_NUMBER_OF_GRIDS; ++y)
324+
if ((_loadedGrids[x] & (UI64LIT(1) << y)) && !_referenceCountFromMap[x][y])
325+
UnloadMapImpl(x, y);
325326

326327
_cleanupTimer.Reset(CleanupInterval);
327328
}

src/server/game/Maps/TerrainMgr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "MapDefines.h"
2424
#include "Position.h"
2525
#include "Timer.h"
26+
#include <array>
2627
#include <atomic>
2728
#include <bitset>
2829
#include <memory>
@@ -111,8 +112,8 @@ class TC_GAME_API TerrainInfo
111112
std::mutex _loadMutex;
112113
std::unique_ptr<GridMap> _gridMap[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
113114
std::atomic<uint16> _referenceCountFromMap[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
114-
std::bitset<MAX_NUMBER_OF_GRIDS* MAX_NUMBER_OF_GRIDS> _loadedGrids;
115-
std::bitset<MAX_NUMBER_OF_GRIDS* MAX_NUMBER_OF_GRIDS> _gridFileExists; // cache what grids are available for this map (not including parent/child maps)
115+
std::array<uint64, (MAX_NUMBER_OF_GRIDS * MAX_NUMBER_OF_GRIDS + 63) / 64> _loadedGrids;
116+
std::bitset<MAX_NUMBER_OF_GRIDS * MAX_NUMBER_OF_GRIDS> _gridFileExists; // cache what grids are available for this map (not including parent/child maps)
116117

117118
static constexpr Milliseconds CleanupInterval = 1min;
118119

0 commit comments

Comments
 (0)