From 249a99dda70a8caf4c70b76155a16d05a3410206 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 31 May 2022 22:01:47 +0200 Subject: [PATCH] astutils.h: reserve `std::vector` space in `visitAstNodes()` to avoid excess allocations --- lib/astutils.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/astutils.h b/lib/astutils.h index 6bdde92b1b5..0dafe305d6c 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -53,7 +53,11 @@ void visitAstNodes(T *ast, const TFunc &visitor) if (!ast) return; - std::stack> tokens; + std::vector tokensContainer; + // the size of 8 was determined in tests to be sufficient to avoid excess allocations. also add 1 as a buffer. + // we might need to increase that value in the future. + tokensContainer.reserve(8 + 1); + std::stack> tokens(std::move(tokensContainer)); T *tok = ast; do { ChildrenToVisit c = visitor(tok);