From 8179226b18e7881a6b6134b01e7363064ccbfe9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 24 Jan 2022 21:44:09 +0100 Subject: [PATCH] astutils.cpp: optimized visitAstNodesGeneric() a bit more by avoiding unnecessary checks and std::stack usage (#3732) --- lib/astutils.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index cb2876eef..08c080baf 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -42,14 +42,12 @@ template )> void visitAstNodesGeneric(T *ast, std::function visitor) { - std::stack> tokens; - tokens.push(ast); - while (!tokens.empty()) { - T *tok = tokens.top(); - tokens.pop(); - if (!tok) - continue; + if (!ast) + return; + std::stack> tokens; + T *tok = ast; + do { ChildrenToVisit c = visitor(tok); if (c == ChildrenToVisit::done) @@ -64,7 +62,13 @@ void visitAstNodesGeneric(T *ast, std::function visitor) if (t1) tokens.push(t1); } - } + + if (tokens.empty()) + break; + + tok = tokens.top(); + tokens.pop(); + } while (true); } void visitAstNodes(const Token *ast, std::function visitor)