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(render): collect paths allocate nothing in the steady state

esengineee81a76bda0e
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 21eb8084d..ee0342eee 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -582,7 +582,8 @@ void RenderFrame::collectLights(ecs::Registry& registry) {
// Gather non-ambient lights, then (if over the UBO's cap) keep the most intense — the
// brightest contribute most, and an explicit importance cull beats silently dropping
// whichever happened to come last in iteration order. Ambient lights sum without a cap.
- std::vector<GpuLight2D> collected;
+ std::vector<GpuLight2D>& collected = light_scratch_;
+ collected.clear();
// A light's Transform need is type-dependent: Ambient (a flat scene-wide term) and
// Directional (parallel rays; direction is intrinsic) have no spatial anchor and work
diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hpp
index d08e0d42c..09bc29cf7 100644
--- a/src/esengine/renderer/RenderFrame.hpp
+++ b/src/esengine/renderer/RenderFrame.hpp
@@ -6,6 +6,7 @@
#include "RenderItem.hpp"
#include "RenderTarget.hpp"
#include "RenderContext.hpp"
+#include "LightConstants.hpp"
#include "RenderTypePlugin.hpp"
#ifdef ES_ENABLE_POSTPROCESS
#include "PostProcessPipeline.hpp"
@@ -23,6 +24,7 @@
#include <string>
#include <memory>
#include <unordered_map>
+#include <unordered_set>
namespace esengine {
@@ -276,6 +278,15 @@ private:
void collectLights(ecs::Registry& registry);
u32 initBatchShader();
u32 compileBatchVariant(const std::vector<std::string>& features);
+
+ std::vector<GpuLight2D> light_scratch_; // reused across frames; collectLights only
+
+ // processMasks scratch, reused across cameras/frames.
+ std::vector<Entity> mask_scissor_scratch_;
+ std::vector<Entity> mask_stencil_scratch_;
+ std::vector<Entity> mask_root_scratch_;
+ std::unordered_set<u32> mask_set_scratch_;
+ std::unordered_set<u32> stencil_set_scratch_;
};
} // namespace esengine
diff --git a/src/esengine/renderer/RenderFrameMask.cpp b/src/esengine/renderer/RenderFrameMask.cpp
index aa6cfeb56..58983a819 100644
--- a/src/esengine/renderer/RenderFrameMask.cpp
+++ b/src/esengine/renderer/RenderFrameMask.cpp
@@ -195,10 +195,14 @@ void RenderFrame::processMasks(ecs::Registry& registry, i32 vpX, i32 vpY, i32 vp
clearAllStencilMasks();
auto maskView = registry.view<ecs::UIMask>();
- std::vector<Entity> scissorMasks;
- std::vector<Entity> stencilMasks;
- std::unordered_set<u32> maskSet;
- std::unordered_set<u32> stencilSet;
+ std::vector<Entity>& scissorMasks = mask_scissor_scratch_;
+ std::vector<Entity>& stencilMasks = mask_stencil_scratch_;
+ std::unordered_set<u32>& maskSet = mask_set_scratch_;
+ std::unordered_set<u32>& stencilSet = stencil_set_scratch_;
+ scissorMasks.clear();
+ stencilMasks.clear();
+ maskSet.clear();
+ stencilSet.clear();
for (auto entity : maskView) {
const auto& mask = registry.get<ecs::UIMask>(entity);
@@ -215,7 +219,8 @@ void RenderFrame::processMasks(ecs::Registry& registry, i32 vpX, i32 vpY, i32 vp
if (scissorMasks.empty() && stencilMasks.empty()) return;
if (!scissorMasks.empty()) {
- std::vector<Entity> rootScissors;
+ std::vector<Entity>& rootScissors = mask_root_scratch_;
+ rootScissors.clear();
for (auto entity : scissorMasks) {
if (!hasAncestorScissorMask(registry, entity, maskSet)) {
rootScissors.push_back(entity);
@@ -233,7 +238,8 @@ void RenderFrame::processMasks(ecs::Registry& registry, i32 vpX, i32 vpY, i32 vp
device_.clearStencil(0);
#endif
- std::vector<Entity> rootStencils;
+ std::vector<Entity>& rootStencils = mask_root_scratch_;
+ rootStencils.clear();
for (auto entity : stencilMasks) {
if (!hasAncestorStencilMask(registry, entity, stencilSet)) {
rootStencils.push_back(entity);
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index 97e2c4bf1..c9cf12d2a 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -192,17 +192,21 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
// The layer's tileset slot list: the multi-tileset table when present
// (Tiled imports), else a single slot from the painted/synthetic tileset.
- std::vector<tilemap::TilesetSlot> slots;
+ const std::vector<tilemap::TilesetSlot>* slotsPtr;
if (!layer.tilesets.empty()) {
- slots = layer.tilesets;
+ slotsPtr = &layer.tilesets;
} else {
if (!singleTileset.isValid() || singleColumns == 0) continue;
- slots.push_back(tilemap::TilesetSlot{ 1, singleTileset.id(), singleColumns });
+ single_slot_scratch_.clear();
+ single_slot_scratch_.push_back(tilemap::TilesetSlot{ 1, singleTileset.id(), singleColumns });
+ slotsPtr = &single_slot_scratch_;
}
+ const std::vector<tilemap::TilesetSlot>& slots = *slotsPtr;
// Resolve each slot's texture + UV scale (tile size / texture size). Kept
// parallel to `slots`; a slot whose texture is missing renders nothing.
- std::vector<ResolvedSlot> resolved(slots.size());
+ resolved_scratch_.assign(slots.size(), ResolvedSlot{});
+ std::vector<ResolvedSlot>& resolved = resolved_scratch_;
bool anySlotValid = false;
for (usize i = 0; i < slots.size(); ++i) {
auto* tex = ctx.resources.getTexture(resource::TextureHandle(slots[i].texture_handle));
@@ -259,8 +263,16 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
auto& chunkCaches = layer_caches_[entity];
// Per-slot merged geometry for this layer (one draw call per slot/texture).
- std::vector<std::vector<BatchVertex>> slotVertices(slots.size());
- std::vector<std::vector<u32>> slotIndices(slots.size());
+ if (slot_vertices_scratch_.size() < slots.size()) {
+ slot_vertices_scratch_.resize(slots.size());
+ slot_indices_scratch_.resize(slots.size());
+ }
+ for (usize si = 0; si < slots.size(); ++si) {
+ slot_vertices_scratch_[si].clear();
+ slot_indices_scratch_[si].clear();
+ }
+ std::vector<std::vector<BatchVertex>>& slotVertices = slot_vertices_scratch_;
+ std::vector<std::vector<u32>>& slotIndices = slot_indices_scratch_;
for (i32 cy = minCY; cy < maxCY; ++cy) {
for (i32 cx = minCX; cx < maxCX; ++cx) {
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
index a1e25e8b7..2743d2fb9 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
@@ -71,6 +71,13 @@ private:
tilemap::TilemapSystem* tilemap_system_ = nullptr;
u32 batch_shader_id_ = 0;
LayerChunkMap layer_caches_;
+
+ // Per-frame scratch reused across layers/frames so collect allocates nothing
+ // in the steady state.
+ std::vector<tilemap::TilesetSlot> single_slot_scratch_;
+ std::vector<ResolvedSlot> resolved_scratch_;
+ std::vector<std::vector<BatchVertex>> slot_vertices_scratch_;
+ std::vector<std::vector<u32>> slot_indices_scratch_;
};
} // namespace esengine