Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(tilemap): skip unchanged tile writes and empty chunk allocation

yhh2622e02fdf90
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c0685d8dc..b4b7ba82e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Unchanged tile writes preserve chunk caches.** Repainting a tile with its existing value no longer advances the chunk revision or forces geometry rebuilding. Erasing untouched empty space does not allocate empty chunks, including negative coordinates in infinite maps.
+
- **Audio sources distinguish interrupted voices from completed clips.** A refused or preempted voice lowers `playing` without setting the natural-completion latch. Only a backend end signal for a non-looping source marks it `finished`; existing backend completion callbacks remain attached and replays clear the latch.
- **Camera preview scheduling follows its current CPU budget.** A preview waiting on an old slow interval now rechecks the live budget, so cheaper recurring captures resume without waiting for the stale deadline. A slower budget also defers the next capture instead of overspending, and unmounting cancels refreshes.
diff --git a/src/esengine/tilemap/TilemapSystem.cpp b/src/esengine/tilemap/TilemapSystem.cpp
index a7afcb850..cd42a45b4 100644
--- a/src/esengine/tilemap/TilemapSystem.cpp
+++ b/src/esengine/tilemap/TilemapSystem.cpp
@@ -57,10 +57,17 @@ inline u16 readTile(const TilemapSystem::LayerData& layer, i32 x, i32 y) {
inline void writeTile(TilemapSystem::LayerData& layer, i32 x, i32 y, u16 value) {
const i32 cx = chunkOf(x), cy = chunkOf(y);
- ChunkData& chunk = layer.chunks[{cx, cy}];
const i32 lx = x - cx * static_cast<i32>(CHUNK_SIZE);
const i32 ly = y - cy * static_cast<i32>(CHUNK_SIZE);
- chunk.tiles[static_cast<usize>(ly) * CHUNK_SIZE + static_cast<usize>(lx)] = value;
+ const usize index = static_cast<usize>(ly) * CHUNK_SIZE + static_cast<usize>(lx);
+ auto it = layer.chunks.find({cx, cy});
+ if (it == layer.chunks.end()) {
+ if (value == EMPTY_TILE) return;
+ it = layer.chunks.try_emplace(ChunkCoord{cx, cy}).first;
+ }
+ ChunkData& chunk = it->second;
+ if (chunk.tiles[index] == value) return;
+ chunk.tiles[index] = value;
chunk.revision = ++layer.edit_revision;
}
diff --git a/tests/tilemap/test_tilemap.cpp b/tests/tilemap/test_tilemap.cpp
index 93165ab3b..86d0e15fe 100644
--- a/tests/tilemap/test_tilemap.cpp
+++ b/tests/tilemap/test_tilemap.cpp
@@ -13,6 +13,28 @@ using namespace esengine::tilemap;
static Entity E(u32 idx) { return Entity::make(idx, 1); }
+TEST_CASE("tilemap_unchanged_writes_preserve_sparse_storage_and_revision") {
+ TilemapSystem sys;
+ sys.initInfiniteLayer(E(0), 16.0f, 16.0f);
+ sys.fillRect(E(0), -64, -64, 128, 128, EMPTY_TILE);
+ const auto* layer = sys.getLayerData(E(0));
+ CHECK(layer->chunks.empty());
+ CHECK(layer->edit_revision == 0);
+
+ sys.setTile(E(0), -1, -1, 42);
+ const auto revision = layer->edit_revision;
+ for (int i = 0; i < 1000; ++i) sys.setTile(E(0), -1, -1, 42);
+ CHECK(layer->edit_revision == revision);
+ CHECK(layer->chunks.size() == 1);
+ CHECK(sys.getTile(E(0), -1, -1) == 42);
+
+ sys.setTile(E(0), -1, -1, EMPTY_TILE);
+ CHECK(layer->edit_revision == revision + 1);
+ CHECK(sys.getTile(E(0), -1, -1) == EMPTY_TILE);
+ sys.setTile(E(0), -1, -1, EMPTY_TILE);
+ CHECK(layer->edit_revision == revision + 1);
+}
+
TEST_CASE("tilemap_init_layer") {
TilemapSystem sys;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 81b143d4d..98a5b3c92 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'perf(tilemap): skip unchanged tile writes and empty chunk allocation':
+ { note: 'Unchanged tile writes preserve chunk caches.' },
'fix(audio): distinguish source completion from interrupted voices':
{ note: 'Audio sources distinguish interrupted voices from completed clips.' },
'fix(editor): refresh camera previews against the live CPU budget':