tibia-rme/source/map_region.cpp
Eduardo Dantas 1fbc5b9113
perf: otbm load save and preview/map renderization (#188)
Improve OTBM load/save and map view performance

This change improves Remere's Map Editor performance in OTBM load/save paths,
object allocation, binary serialization, tile lookup, and idle map rendering.

Measured impact:

Object pool allocation:
- Slab refill events reduced from 17,830 to 2,230, about 87.5% fewer refills.
- 48-byte class refills reduced from 4,276 to 535, about 87.5% fewer refills.
- 64-byte class refills reduced from 5 to 1, about 80.0% fewer refills.
- 128-byte class refills reduced from 8,776 to 1,097, about 87.5% fewer refills.
- 1024-byte class refills reduced from 4,773 to 597, about 87.5% fewer refills.
- rme::allocatePooledObject sampled CPU share reduced from 20.01% to 14.62%,
  about 26.9% lower sampled share.
- Total pooled allocation calls stayed at 42,559,971 for the measured workload.
- Heap fallback allocations stayed at 0, confirming the hot load path remains pooled.

Latest mixed load/save profile:
- GUI::LoadMap remained the dominant sampled cost at 67.02%.
- IOMapOTBM::loadMap accounted for 60.23% total sampled CPU, with 56.34% in
  the inner load body.
- GUI::SaveMap and Editor::saveMap accounted for 28.16%.
- IOMapOTBM::saveMap accounted for 27.63%.
- BaseMap::forEachTileLocation during save accounted for 23.01%.
- Tile::Tile accounted for 11.53%.
- Tile::addLoadedItem accounted for 8.15%.
- Item::Create accounted for 6.99%.
- QTreeNode::createFloor accounted for 6.71%.
- BinaryNode::advance and BinaryNode::load accounted for 5.37% and 3.81%.

Map view idle and preview rendering:
- Static map-view sampled CPU dropped from 139,767 sampled units to 126 sampled
  units after overlay-only refresh reuse.
- MapCanvas::OnPaint dropped from 46,229 sampled units and 32.20% to 19 sampled
  units and 15.08%.
- GLRenderer::flushCommands dropped from 24,321 sampled units and 16.94% to
  4 sampled units and 3.17%.
- Performance stats refresh changed from a 16 ms scene-dirty timer to a 500 ms
  overlay-only timer.
- Show Preview changed from a 16 ms scene-dirty timer to a 250 ms scene-dirty
  timer.
- Position indicator keeps the 16 ms scene-dirty timer because it is expected
  to animate smoothly while active.

Main changes:
- Added cached floor and tile lookup while loading OTBM map data and spawn files.
- Added direct TileLocation assignment for parser paths that already resolved
  the destination location.
- Added BaseMap::forEachTileLocation for direct save traversal of existing tile
  locations.
- Added a small-object slab allocator for hot Item, Tile, and Floor allocations.
- Added pool owner-thread binding and diagnostics for allocation validation.
- Increased slab sizing to reduce refill pressure in large-map loads.
- Improved binary node writing by batching raw bytes and avoiding redundant cache
  renewal checks.
- Avoided rewriting XML sidecar files when serialized content only differs by
  line endings.
- Fixed invalid ground serialization so placeholder ground id 0 no longer drops
  the rest of the tile contents during save.
- Split map canvas refresh into scene-dirty and overlay-only paths.
- Adjusted animation timer behavior for position indicator, Show Preview, and
  performance stats.
- Scaled tooltip rendering with map zoom, clamped to 55% minimum.
- Kept review and Sonar cleanups away from hot path regressions with targeted
  NOSONAR annotations or FORCEINLINE where needed.
- Added AGENTS.md guidance for future Git, build, and PCH discipline.

Notes:
- The Visual Studio captures are sampling profiles, so percentages represent CPU
  sample share, not direct wall-clock speedup.
- The latest profile is a mixed interaction profile, not a strict load-only or
  save-only benchmark.
- The allocator counters are the strongest before/after measurement in this
  change.
- No OTBM format or map semantics are intended to change.
2026-05-22 15:14:34 -03:00

284 lines
6 KiB
C++

//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "map_region.h"
#include "basemap.h"
#include "position.h"
#include "tile.h"
#include "object_pool.h"
//**************** Tile Location **********************
TileLocation::TileLocation() :
tile(nullptr),
position(0, 0, 0),
spawn_monster_count(0),
spawn_npc_count(0),
waypoint_count(0),
house_exits(nullptr) {
////
}
TileLocation::~TileLocation() {
delete tile;
delete house_exits;
}
int TileLocation::size() const {
if (tile) {
return tile->size();
}
return spawn_monster_count + spawn_npc_count + waypoint_count + (house_exits ? 1 : 0);
}
bool TileLocation::empty() const {
return size() == 0;
}
HouseExitList* TileLocation::createHouseExits() {
if (!house_exits) {
house_exits = new HouseExitList();
}
return house_exits;
}
//**************** Floor **********************
void* Floor::operator new(size_t size) {
return rme::allocatePooledObject(size);
}
void Floor::operator delete(void* ptr) noexcept {
rme::deallocatePooledObject(ptr);
}
#ifdef DEBUG_MEM
void* Floor::operator new(size_t size, const char*, int) {
return rme::allocatePooledObject(size);
}
void Floor::operator delete(void* ptr, const char*, int) noexcept {
rme::deallocatePooledObject(ptr);
}
#endif
Floor::Floor(int sx, int sy, int z) {
sx = sx & ~3;
sy = sy & ~3;
for (int i = 0; i < rme::MapLayers; ++i) {
locs[i].position.x = sx + (i >> 2);
locs[i].position.y = sy + (i & 3);
locs[i].position.z = z;
}
}
//**************** QTreeNode **********************
QTreeNode::QTreeNode(BaseMap &map) :
map(map),
visible(0),
isLeaf(false) {
// Doesn't matter if we're leaf or node
for (int i = 0; i < rme::MapLayers; ++i) {
child[i] = nullptr;
}
}
QTreeNode::~QTreeNode() {
if (isLeaf) {
for (int i = 0; i < rme::MapLayers; ++i) {
delete array[i];
}
} else {
for (int i = 0; i < rme::MapLayers; ++i) {
delete child[i];
}
}
}
QTreeNode* QTreeNode::getLeaf(int x, int y) {
QTreeNode* node = this;
uint32_t cx = x, cy = y;
while (node) {
if (node->isLeaf) {
return node;
} else {
uint32_t index = ((cx & 0xC000) >> 14) | ((cy & 0xC000) >> 12);
if (node->child[index]) {
node = node->child[index];
cx <<= 2;
cy <<= 2;
} else {
return nullptr;
}
}
}
return nullptr;
}
QTreeNode* QTreeNode::getLeafForce(int x, int y) {
QTreeNode* node = this;
uint32_t cx = x, cy = y;
int level = 6;
while (node) {
uint32_t index = ((cx & 0xC000) >> 14) | ((cy & 0xC000) >> 12);
QTreeNode*&qt = node->child[index];
if (qt) {
if (qt->isLeaf) {
return qt;
}
} else {
if (level == 0) {
qt = newd QTreeNode(map);
qt->isLeaf = true;
return qt;
} else {
qt = newd QTreeNode(map);
}
}
node = node->child[index];
cx <<= 2;
cy <<= 2;
level -= 1;
}
return nullptr;
}
Floor* QTreeNode::createFloor(int x, int y, int z) {
ASSERT(isLeaf);
if (!array[z]) {
array[z] = newd Floor(x, y, z);
}
return array[z];
}
bool QTreeNode::isVisible(bool underground) {
return testFlags(visible, underground + 1);
}
bool QTreeNode::isRequested(bool underground) {
if (underground) {
return testFlags(visible, 4);
} else {
return testFlags(visible, 8);
}
}
void QTreeNode::clearVisible(uint32_t u) {
if (isLeaf) {
visible &= u;
} else {
for (int i = 0; i < rme::MapLayers; ++i) {
if (child[i]) {
child[i]->clearVisible(u);
}
}
}
}
bool QTreeNode::isVisible(uint32_t client, bool underground) {
if (underground) {
return testFlags(visible >> rme::MapLayers, static_cast<uint64_t>(1) << client);
} else {
return testFlags(visible, static_cast<uint64_t>(1) << client);
}
}
void QTreeNode::setVisible(bool underground, bool value) {
if (underground) {
if (value) {
visible |= 2;
} else {
visible &= ~2;
}
} else { // overground
if (value) {
visible |= 1;
} else {
visible &= 1;
}
}
}
void QTreeNode::setRequested(bool underground, bool r) {
if (r) {
visible |= (underground ? 4 : 8);
} else {
visible &= ~(underground ? 4 : 8);
}
}
void QTreeNode::setVisible(uint32_t client, bool underground, bool value) {
if (value) {
visible |= (1 << client << (underground ? rme::MapLayers : 0));
} else {
visible &= ~(1 << client << (underground ? rme::MapLayers : 0));
}
}
TileLocation* QTreeNode::getTile(int x, int y, int z) {
ASSERT(isLeaf);
Floor* f = array[z];
if (!f) {
return nullptr;
}
return &f->locs[(x & 3) * 4 + (y & 3)];
}
TileLocation* QTreeNode::createTile(int x, int y, int z) {
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
return &f->locs[(x & 3) * 4 + (y & 3)];
}
Tile* QTreeNode::setTile(int x, int y, int z, Tile* newtile) {
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
int offset_x = x & 3;
int offset_y = y & 3;
TileLocation* tmp = &f->locs[offset_x * 4 + offset_y];
Tile* oldtile = tmp->tile;
tmp->tile = newtile;
if (newtile && !oldtile) {
++map.tilecount;
} else if (oldtile && !newtile) {
--map.tilecount;
}
return oldtile;
}
void QTreeNode::clearTile(int x, int y, int z) {
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
int offset_x = x & 3;
int offset_y = y & 3;
TileLocation* tmp = &f->locs[offset_x * 4 + offset_y];
delete tmp->tile;
tmp->tile = map.allocator(tmp);
}