From cc08a661e64b0512105efccc97e389305b7d5c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 3 Jun 2022 19:21:17 +0200 Subject: [PATCH] astutils.h: reserve `std::vector` space in `visitAstNodes()` to avoid excess allocations (#4158) --- lib/astutils.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/astutils.h b/lib/astutils.h index 6bdde92b1..0dafe305d 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);