From c42d577fc0dc6103af3639013361927b106da5c5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 8 Nov 2022 12:17:04 +0100 Subject: [PATCH] Cache literal to number conversion. --- libyul/Utilities.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libyul/Utilities.cpp b/libyul/Utilities.cpp index 8db0e44ff..b45821066 100644 --- a/libyul/Utilities.cpp +++ b/libyul/Utilities.cpp @@ -91,9 +91,17 @@ u256 solidity::yul::valueOfNumberLiteral(Literal const& _literal) { yulAssert(_literal.kind == LiteralKind::Number, "Expected number literal!"); - std::string const& literalString = _literal.value.str(); - yulAssert(isValidDecimal(literalString) || isValidHex(literalString), "Invalid number literal!"); - return u256(literalString); + static map numberCache; + static YulStringRepository::ResetCallback callback{[&] { numberCache.clear(); }}; + + auto&& [it, isNew] = numberCache.try_emplace(_literal.value, 0); + if (isNew) + { + std::string const& literalString = _literal.value.str(); + yulAssert(isValidDecimal(literalString) || isValidHex(literalString), "Invalid number literal!"); + it->second = u256(literalString); + } + return it->second; } u256 solidity::yul::valueOfStringLiteral(Literal const& _literal)