From 0d59a1d0292d2550fc6f7a2c55f20c688b9f3655 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 20 Sep 2019 18:51:31 +0200 Subject: [PATCH] Speed up lexer by allocating more space for its tokens initially. --- src/Parser/Lexer.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Parser/Lexer.cpp b/src/Parser/Lexer.cpp index cb1022c..c8f5b49 100644 --- a/src/Parser/Lexer.cpp +++ b/src/Parser/Lexer.cpp @@ -14,17 +14,23 @@ namespace Porygon::Parser { vector Lexer::Lex() { - vector tokens; + // This is a very greedy allocation. Look into potentially making this less greedy. + vector tokens(this->_scriptSize+1); + size_t current = 0; while (true) { Token *next = this->LexNext(this->Next()); auto nextKind = next->GetKind(); if (nextKind != TokenKind::WhiteSpace && nextKind != TokenKind::Comment) - tokens.push_back(next); + { + tokens[current] = next; + current++; + } else delete next; if (nextKind == TokenKind::EndOfFile) break; } + tokens.resize(current); return tokens; }