From 3ce6345d21bf4ba0f88d872bcfd3436212f365fa Mon Sep 17 00:00:00 2001 From: Neirokan Date: Thu, 4 Nov 2021 03:44:53 +0300 Subject: [PATCH] std::vector resize & move optimisations --- engine/Mesh.cpp | 10 ++++++++-- engine/Mesh.h | 2 ++ engine/animation/AScale.h | 6 ++++-- engine/physics/HitBox.cpp | 2 ++ engine/physics/RigidBody.cpp | 2 ++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index b166979..43e8f5e 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -11,10 +11,11 @@ using namespace std; Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { std::vector newTriangles; + newTriangles.reserve(_tris.size()); for (auto &t : _tris) { newTriangles.emplace_back(t * matrix4X4); } - setTriangles(newTriangles); + setTriangles(std::move(newTriangles)); return *this; } @@ -46,10 +47,11 @@ void Mesh::setColor(const sf::Color &c) { // change color of all mesh triangles: std::vector newTriangles; + newTriangles.reserve(_tris.size()); for (auto &t : _tris) { newTriangles.emplace_back(Triangle(t[0], t[1], t[2], c)); } - setTriangles(newTriangles); + setTriangles(std::move(newTriangles)); } Mesh @@ -99,6 +101,10 @@ void Mesh::setTriangles(const vector &t) { } } +void Mesh::setTriangles(vector&& t) { + _tris = std::move(t); +} + Mesh::~Mesh() { _tris.clear(); } diff --git a/engine/Mesh.h b/engine/Mesh.h index 1f37bea..e0d3c89 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -38,6 +38,8 @@ public: void setTriangles(const std::vector &t); + void setTriangles(std::vector&& t); + [[nodiscard]] size_t size() const { return _tris.size() * 3; } [[nodiscard]] sf::Color color() const { return _color; } diff --git a/engine/animation/AScale.h b/engine/animation/AScale.h index c559d8d..7fc24e5 100644 --- a/engine/animation/AScale.h +++ b/engine/animation/AScale.h @@ -21,12 +21,14 @@ private: return; } + auto object = _object.lock(); std::vector newTriangles; - for (auto &t : _object->triangles()) { + newTriangles.reserve(object->triangles().size()); + for (auto &t : object->triangles()) { newTriangles.emplace_back( t * Matrix4x4::Scale(Vec3D{1, 1, 1} + (_scalingValue - Vec3D{1, 1, 1}) * progress())); } - _object.lock()->setTriangles(newTriangles); + object.lock()->setTriangles(std::move(newTriangles)); } public: diff --git a/engine/physics/HitBox.cpp b/engine/physics/HitBox.cpp index 54eaf72..07b3f48 100644 --- a/engine/physics/HitBox.cpp +++ b/engine/physics/HitBox.cpp @@ -6,12 +6,14 @@ #include "../Consts.h" HitBox::HitBox(const Mesh &mesh) { + _hitBox.reserve(mesh.triangles().size() * 3); for(const auto& t : mesh.triangles()) { for(int i = 0; i < 3; i++) { // we dont need to add the same points in hit box _addIfUnique(Vec3D(t[i])); } } + _hitBox.shrink_to_fit(); } void HitBox::_addIfUnique(const Vec3D &point) { diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index 2150016..45b0845 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -253,6 +253,7 @@ CollisionPoint RigidBody::EPA(const Simplex &simplex, std::shared_ptr } std::vector newFaces; + newFaces.reserve(uniqueEdges.size() * 3); for (auto[edgeIndex1, edgeIndex2] : uniqueEdges) { newFaces.push_back(edgeIndex1); newFaces.push_back(edgeIndex2); @@ -280,6 +281,7 @@ CollisionPoint RigidBody::EPA(const Simplex &simplex, std::shared_ptr std::pair, size_t> RigidBody::_getFaceNormals(const std::vector &polytope, const std::vector &faces) { std::vector normals; + normals.reserve(faces.size() / 3); size_t nearestFaceIndex = 0; double minDistance = std::numeric_limits::max();