Skip to content

Commit

Permalink
Cache literal to number conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Nov 8, 2022
1 parent ce18ddd commit c42d577
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions libyul/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<YulString, u256> 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)
Expand Down

0 comments on commit c42d577

Please sign in to comment.