From f053ffeb1595c8186116425cfbe963c9d0a37d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 18 Dec 2021 20:38:33 +0100 Subject: [PATCH] SceneTools: remove one more allocation in orderClusterParents(). --- src/Magnum/SceneTools/OrderClusterParents.cpp | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Magnum/SceneTools/OrderClusterParents.cpp b/src/Magnum/SceneTools/OrderClusterParents.cpp index 60025f560..ff94eb610 100644 --- a/src/Magnum/SceneTools/OrderClusterParents.cpp +++ b/src/Magnum/SceneTools/OrderClusterParents.cpp @@ -60,15 +60,16 @@ void orderClusterParentsInto(const Trade::SceneData& scene, const Containers::St const Containers::Array> parents = scene.parentsAsArray(); /* Children offset for each node including root. First calculate the count - of children for each ... */ - Containers::Array childrenOffsets{DirectInit, scene.mappingBound() + 2, 0u}; + of children for each, skipping the first element (parent.second() can be + -1, accounting for that as well)... */ + Containers::Array childrenOffsets{DirectInit, scene.mappingBound() + 3, 0u}; for(const Containers::Pair& parent: parents) { CORRADE_INTERNAL_ASSERT(parent.first() < scene.mappingBound() && (parent.second() == -1 || UnsignedInt(parent.second()) < scene.mappingBound())); - ++childrenOffsets[parent.second() + 1]; + ++childrenOffsets[parent.second() + 2]; } /* ... then convert the counts to a running offset. Now - `[childrenOffsets[i + 1], childrenOffsets[i + 2])` contains a range in + `[childrenOffsets[i + 2], childrenOffsets[i + 3])` contains a range in which the `children` array below contains a list of children for `i`. */ UnsignedInt offset = 0; for(UnsignedInt& i: childrenOffsets) { @@ -78,16 +79,14 @@ void orderClusterParentsInto(const Trade::SceneData& scene, const Containers::St } CORRADE_INTERNAL_ASSERT(offset == parents.size()); - /* Go through the parent list again, convert that to child ranges */ + /* Go through the parent list again, convert that to child ranges. The + childrenOffsets array gets shifted by one element by the process, thus + now `[childrenOffsets[i + 1], childrenOffsets[i + 2])` contains a range + in which the `children` array below contains a list of children for + `i`. */ Containers::Array children{NoInit, parents.size()}; - { - Containers::Array currentChildrenOffsets{DirectInit, scene.mappingBound() + 1, 0u}; - for(const Containers::Pair& parent: parents) { - UnsignedInt& currentChildrenOffset = currentChildrenOffsets[parent.second() + 1]; - children[childrenOffsets[parent.second() + 1] + currentChildrenOffset] = parent.first(); - ++currentChildrenOffset; - } - } + for(const Containers::Pair& parent: parents) + children[childrenOffsets[parent.second() + 2]++] = parent.first(); /* Go breadth-first (so we have nodes sharing the same parent next to each other) and build a list of (id, parent id) where a parent is always