From a6f1242a11ac5d52a56248c62032f6ced2cf500b Mon Sep 17 00:00:00 2001 From: Desour Date: Wed, 14 Aug 2024 21:34:15 +0200 Subject: [PATCH] Fix CMeshBuffer::append reallocating too eagerly (#14969) --- irr/include/CMeshBuffer.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/irr/include/CMeshBuffer.h b/irr/include/CMeshBuffer.h index 8f8158ff1..fb03d1396 100644 --- a/irr/include/CMeshBuffer.h +++ b/irr/include/CMeshBuffer.h @@ -170,16 +170,21 @@ public: return; const u32 vertexCount = getVertexCount(); - u32 i; - Vertices.reallocate(vertexCount + numVertices); - for (i = 0; i < numVertices; ++i) { + // Only reallocate if there are enough new vertices. Otherwise append() + // will be slow when called in a loop. + if (vertexCount + numVertices > Vertices.allocated_size() * 3 / 2) + Vertices.reallocate(vertexCount + numVertices); + + for (u32 i = 0; i < numVertices; ++i) { Vertices.push_back(static_cast(vertices)[i]); BoundingBox.addInternalPoint(static_cast(vertices)[i].Pos); } - Indices.reallocate(getIndexCount() + numIndices); - for (i = 0; i < numIndices; ++i) { + if (getIndexCount() + numIndices > Indices.allocated_size() * 3 / 2) + Indices.reallocate(getIndexCount() + numIndices); + + for (u32 i = 0; i < numIndices; ++i) { Indices.push_back(indices[i] + vertexCount); } }