From 59d85f9b87fc82b52f2c7438e38768aeddc3cc87 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 Apr 2023 14:56:08 +0100 Subject: Don't fill the instances unused vector unnecessarily --- gfx/gl/instanceVertices.h | 17 ++++++++++------- test/test-instancing.cpp | 10 ++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/gfx/gl/instanceVertices.h b/gfx/gl/instanceVertices.h index edcaef4..51bf4bc 100644 --- a/gfx/gl/instanceVertices.h +++ b/gfx/gl/instanceVertices.h @@ -132,19 +132,22 @@ protected: { // Destroy p's object at(pidx).~T(); - if (next-- > index[pidx]) { - // Remember p.index is free index now - unused.push_back(pidx); + if (--next != index[pidx]) { // Move last object into p's slot new (&at(pidx)) T {std::move(data[next])}; (data[next]).~T(); - *std::find(index.begin(), index.end(), next) = index[pidx]; - // Not strictly required, but needed for uniqueness unit test assertion - index[pidx] = static_cast(-1); + *std::find_if(index.begin(), index.end(), [this](const auto & i) { + const auto n = &i - index.data(); + return i == next && std::find(unused.begin(), unused.end(), n) == unused.end(); + }) = index[pidx]; } - else { + if (pidx == index.size() - 1) { index.pop_back(); } + else { + // Remember p.index is free index now + unused.push_back(pidx); + } } void diff --git a/test/test-instancing.cpp b/test/test-instancing.cpp index 70b20fa..c743ce0 100644 --- a/test/test-instancing.cpp +++ b/test/test-instancing.cpp @@ -38,9 +38,8 @@ BOOST_AUTO_TEST_CASE(acquireRelease) BOOST_CHECK(unused.empty()); } BOOST_CHECK_EQUAL(0, next); - BOOST_CHECK_EQUAL(1, unused.size()); - BOOST_CHECK_EQUAL(0, unused.front()); - BOOST_CHECK_EQUAL(1, index.size()); + BOOST_CHECK(unused.empty()); + BOOST_CHECK(index.empty()); } BOOST_AUTO_TEST_CASE(acquireReleaseMove) @@ -55,9 +54,8 @@ BOOST_AUTO_TEST_CASE(acquireReleaseMove) BOOST_CHECK_EQUAL(data[0], 40); } BOOST_CHECK_EQUAL(0, next); - BOOST_CHECK_EQUAL(1, unused.size()); - BOOST_CHECK_EQUAL(0, unused.front()); - BOOST_CHECK_EQUAL(1, index.size()); + BOOST_CHECK(unused.empty()); + BOOST_CHECK(index.empty()); } BOOST_AUTO_TEST_CASE(autoMapUnmap) -- cgit v1.2.3