Skip to content

Commit 04fe223

Browse files
authored
Merge pull request #14 from TECHNICANGEL/perf-optimize-entity-manager-369316685402116316
⚡ Avoid redundant vector allocation in EntityManager
2 parents 0b43f2e + 955f408 commit 04fe223

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

src/EntityManager.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class EntityManager {
99
private:
1010
std::vector<std::unique_ptr<Entity>> entities;
1111
std::unordered_map<std::string, Entity*> entityMap;
12+
mutable std::vector<Transform> cachedTransforms;
1213

1314
public:
1415
// Add entity to the scene
@@ -38,13 +39,13 @@ class EntityManager {
3839
}
3940

4041
// Get all transforms for acceleration structure
41-
std::vector<Transform> getTransforms() const {
42-
std::vector<Transform> transforms;
43-
transforms.reserve(entities.size());
42+
const std::vector<Transform>& getTransforms() const {
43+
cachedTransforms.clear();
44+
cachedTransforms.reserve(entities.size());
4445
for (const auto& entity : entities) {
45-
transforms.push_back(entity->transform);
46+
cachedTransforms.push_back(entity->transform);
4647
}
47-
return transforms;
48+
return cachedTransforms;
4849
}
4950

5051
// Get entity by name

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ class RacingEngine {
835835

836836
// Prepare TLAS instance data (CPU-side, before command buffer recording)
837837
if (entityManager.countDynamic() > 0) {
838-
auto transforms = entityManager.getTransforms();
838+
const auto& transforms = entityManager.getTransforms();
839839
acceleration.prepareInstanceData(device, transforms);
840840
}
841841

0 commit comments

Comments
 (0)