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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf: amortize per-frame allocations in the render hot path
esengine96557a5c637a
diff --git a/src/esengine/renderer/DrawList.cpp b/src/esengine/renderer/DrawList.cppindex 00c429f56..8dede258d 100644--- a/src/esengine/renderer/DrawList.cpp+++ b/src/esengine/renderer/DrawList.cpp@@ -32,12 +32,13 @@ void DrawList::finalize() {return a.key < b.key;});- std::vector<DrawCommand> sorted;- sorted.reserve(count);+ // Gather into a reused scratch buffer (not a fresh per-frame vector) and swap+ // it with commands_, so the allocation is amortized across frames.+ sorted_scratch_.resize(count);for (u32 i = 0; i < count; ++i) {- sorted.push_back(commands_[sort_entries_[i].index]);+ sorted_scratch_[i] = commands_[sort_entries_[i].index];}- commands_ = std::move(sorted);+ commands_.swap(sorted_scratch_);merged_draw_calls_ = 0;u32 writeIdx = 0;diff --git a/src/esengine/renderer/DrawList.hpp b/src/esengine/renderer/DrawList.hppindex eb7dac076..498054dce 100644--- a/src/esengine/renderer/DrawList.hpp+++ b/src/esengine/renderer/DrawList.hpp@@ -44,6 +44,8 @@ private:std::vector<DrawCommand> commands_;std::vector<SortEntry> sort_entries_;+ std::vector<DrawCommand> sorted_scratch_; // reused across frames to avoid a+ // per-frame heap alloc in finalize()u32 merged_draw_calls_ = 0;};diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cppindex aea73a07f..03e640cd3 100644--- a/src/esengine/renderer/plugins/SpritePlugin.cpp+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp@@ -124,6 +124,7 @@ void SpritePlugin::emitQuad(BatchVertex verts[4];f32 ox = 0.5f - pivot.x;f32 oy = 0.5f - pivot.y;+ const u32 packedColor = packColor(color); // color is constant across the 4 vertsif (std::abs(angle) > 0.001f) {f32 cosA = std::cos(angle);@@ -135,7 +136,7 @@ void SpritePlugin::emitQuad(position.x + lx * cosA - ly * sinA,position.y + lx * sinA + ly * cosA);- verts[i].color = packColor(color);+ verts[i].color = packedColor;verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;}} else {@@ -144,7 +145,7 @@ void SpritePlugin::emitQuad(position.x + (QUAD_POSITIONS[i].x + ox) * size.x,position.y + (QUAD_POSITIONS[i].y + oy) * size.y);- verts[i].color = packColor(color);+ verts[i].color = packedColor;verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;}}diff --git a/src/esengine/renderer/plugins/UIElementPlugin.cpp b/src/esengine/renderer/plugins/UIElementPlugin.cppindex 59594cc46..6f4ded5ba 100644--- a/src/esengine/renderer/plugins/UIElementPlugin.cpp+++ b/src/esengine/renderer/plugins/UIElementPlugin.cpp@@ -126,6 +126,7 @@ void UIElementPlugin::emitQuad(const ClipState& clips) {BatchVertex verts[4];+ const u32 packedColor = packColor(color); // color is constant across the 4 vertsif (std::abs(angle) > 0.001f) {f32 cosA = std::cos(angle);@@ -137,7 +138,7 @@ void UIElementPlugin::emitQuad(position.x + lx * cosA - ly * sinA,position.y + lx * sinA + ly * cosA);- verts[i].color = packColor(color);+ verts[i].color = packedColor;verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;}} else {@@ -146,7 +147,7 @@ void UIElementPlugin::emitQuad(position.x + QUAD_POSITIONS[i].x * size.x,position.y + QUAD_POSITIONS[i].y * size.y);- verts[i].color = packColor(color);+ verts[i].color = packedColor;verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;}}