optimizations

This commit is contained in:
proddy
2025-10-18 17:17:41 +02:00
parent a5af36e15b
commit 61d50e2c79

View File

@@ -22,7 +22,7 @@
#include "shuntingYard.h" #include "shuntingYard.h"
// find tokens // find tokens - optimized to reduce string allocations
std::deque<Token> exprToTokens(const std::string & expr) { std::deque<Token> exprToTokens(const std::string & expr) {
std::deque<Token> tokens; std::deque<Token> tokens;
@@ -40,13 +40,14 @@ std::deque<Token> exprToTokens(const std::string & expr) {
if (*p) { if (*p) {
++p; ++p;
} }
auto s = std::string(b, p); // Use string_view to avoid unnecessary string copies
std::string_view s(b, p - b);
auto n = s.find("\"\""); auto n = s.find("\"\"");
while (n != std::string::npos) { while (n != std::string_view::npos) {
s.erase(n, 2); s.remove_prefix(n + 2);
n = s.find("\"\""); n = s.find("\"\"");
} }
tokens.emplace_back(Token::Type::String, s, -3); tokens.emplace_back(Token::Type::String, std::string(s), -3);
if (*p == '\0') { if (*p == '\0') {
--p; --p;
} }
@@ -225,11 +226,14 @@ std::deque<Token> exprToTokens(const std::string & expr) {
return tokens; return tokens;
} }
// sort tokens to RPN form // sort tokens to RPN form - optimized for memory usage
std::deque<Token> shuntingYard(const std::deque<Token> & tokens) { std::deque<Token> shuntingYard(const std::deque<Token> & tokens) {
std::deque<Token> queue; std::deque<Token> queue;
std::vector<Token> stack; std::vector<Token> stack;
// Reserve space for vector to reduce reallocations
stack.reserve(tokens.size() / 2);
// While there are tokens to be read: // While there are tokens to be read:
for (auto const & token : tokens) { for (auto const & token : tokens) {
// Read a token // Read a token