From a320d1a5b4b7ce3b90372697fbe50242b78d082e Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sun, 28 Dec 2014 00:56:32 -0200 Subject: Clipper: Avoid dynamic allocations The triangle clipper was allocating its temporary input, output and work buffers using a std::vector. Since this is a hot path, it's desirable to use stack allocation instead. --- src/video_core/clipper.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'src/video_core') diff --git a/src/video_core/clipper.cpp b/src/video_core/clipper.cpp index 0bcd0b895..e89b7a0c0 100644 --- a/src/video_core/clipper.cpp +++ b/src/video_core/clipper.cpp @@ -2,7 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include +#include #include "clipper.h" #include "pica.h" @@ -98,18 +98,15 @@ static void InitScreenCoordinates(OutputVertex& vtx) } void ProcessTriangle(OutputVertex &v0, OutputVertex &v1, OutputVertex &v2) { + using boost::container::static_vector; // TODO (neobrain): // The list of output vertices has some fixed maximum size, // however I haven't taken the time to figure out what it is exactly. - // For now, we hence just assume a maximal size of 1000 vertices. - const size_t max_vertices = 1000; - std::vector buffer_vertices; - std::vector output_list{ &v0, &v1, &v2 }; - - // Make sure to reserve space for all vertices. - // Without this, buffer reallocation would invalidate references. - buffer_vertices.reserve(max_vertices); + // For now, we hence just assume a maximal size of 256 vertices. + static const size_t MAX_VERTICES = 256; + static_vector buffer_vertices; + static_vector output_list = { &v0, &v1, &v2 }; // Simple implementation of the Sutherland-Hodgman clipping algorithm. // TODO: Make this less inefficient (currently lots of useless buffering overhead happens here) @@ -120,7 +117,7 @@ void ProcessTriangle(OutputVertex &v0, OutputVertex &v1, OutputVertex &v2) { ClippingEdge(ClippingEdge::POS_Z, float24::FromFloat32(+1.0)), ClippingEdge(ClippingEdge::NEG_Z, float24::FromFloat32(-1.0)) }) { - const std::vector input_list = output_list; + const static_vector input_list = output_list; output_list.clear(); const OutputVertex* reference_vertex = input_list.back(); -- cgit v1.2.3