astutils.h: reserve `std::vector` space in `visitAstNodes()` to avoid excess allocations (#4158)

This commit is contained in:
Oliver Stöneberg 2022-06-03 19:21:17 +02:00 committed by GitHub
parent 00abf21d40
commit cc08a661e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 1 deletions

View File

@ -53,7 +53,11 @@ void visitAstNodes(T *ast, const TFunc &visitor)
if (!ast) if (!ast)
return; return;
std::stack<T *, std::vector<T *>> tokens; std::vector<T *> 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<T *, std::vector<T *>> tokens(std::move(tokensContainer));
T *tok = ast; T *tok = ast;
do { do {
ChildrenToVisit c = visitor(tok); ChildrenToVisit c = visitor(tok);