From d4b8a3f67c1f7184124bf95caee7e36ae8d6c82a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 16 Mar 2021 23:51:21 +0200 Subject: [PATCH] Add push_back_nocopy to NodeContainer for clarity It's not immediately obvious that pushing null and then assigning avoids cloning the node. --- source/glsl/generate.cpp | 3 +-- source/glsl/optimize.cpp | 6 ++---- source/glsl/syntax.h | 3 +++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 42b24a9c..56360991 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -579,8 +579,7 @@ void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclarati RefPtr call = new FunctionCall; call->name = type.name; call->constructor = true; - call->arguments.push_back(0); - call->arguments.back() = expr; + call->arguments.push_back_nocopy(expr); call->type = &type; expr = call; } diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index d5700255..b7a722f4 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -102,8 +102,7 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta SetForScope set_pass(pass, RENAME); var->visit(*this); - staging_block.body.push_back(0); - staging_block.body.back() = var; + staging_block.body.push_back_nocopy(var); params.push_back(var); } @@ -117,8 +116,7 @@ const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &ta SetForScope set_pass(pass, RENAME); r_inlined_statement->visit(*this); - staging_block.body.push_back(0); - staging_block.body.back() = r_inlined_statement; + staging_block.body.push_back_nocopy(r_inlined_statement); } /* Now collect names from the staging block. Local variables that would diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 61294211..9bc3c5a8 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -96,6 +96,9 @@ class NodeContainer: public C public: NodeContainer() { } NodeContainer(const NodeContainer &); + + void push_back_nocopy(const typename C::value_type &v) + { C::push_back(0); C::back() = v; } }; template -- 2.45.2