From 61d50e2c7953102cc04c6efc3ac2e1d2beb07edd Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 18 Oct 2025 17:17:41 +0200 Subject: [PATCH] optimizations --- src/core/shuntingYard.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/core/shuntingYard.cpp b/src/core/shuntingYard.cpp index 5cb262f50..12ef69d41 100644 --- a/src/core/shuntingYard.cpp +++ b/src/core/shuntingYard.cpp @@ -22,7 +22,7 @@ #include "shuntingYard.h" -// find tokens +// find tokens - optimized to reduce string allocations std::deque exprToTokens(const std::string & expr) { std::deque tokens; @@ -40,13 +40,14 @@ std::deque exprToTokens(const std::string & expr) { if (*p) { ++p; } - auto s = std::string(b, p); - auto n = s.find("\"\""); - while (n != std::string::npos) { - s.erase(n, 2); + // Use string_view to avoid unnecessary string copies + std::string_view s(b, p - b); + auto n = s.find("\"\""); + while (n != std::string_view::npos) { + s.remove_prefix(n + 2); n = s.find("\"\""); } - tokens.emplace_back(Token::Type::String, s, -3); + tokens.emplace_back(Token::Type::String, std::string(s), -3); if (*p == '\0') { --p; } @@ -225,11 +226,14 @@ std::deque exprToTokens(const std::string & expr) { return tokens; } -// sort tokens to RPN form +// sort tokens to RPN form - optimized for memory usage std::deque shuntingYard(const std::deque & tokens) { std::deque queue; std::vector stack; + // Reserve space for vector to reduce reallocations + stack.reserve(tokens.size() / 2); + // While there are tokens to be read: for (auto const & token : tokens) { // Read a token