Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yul: Avoid expensive operations in functions called very often #15574

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion libyul/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ void Parser::fetchDebugDataFromComment()
{
solAssert(m_sourceNames.has_value(), "");

std::string_view commentLiteral = m_scanner->currentCommentLiteral();
if (commentLiteral.empty())
{
m_astIDFromComment = std::nullopt;
return;
}
static std::regex const tagRegex = std::regex(
R"~~((?:^|\s+)(@[a-zA-Z0-9\-_]+)(?:\s+|$))~~", // tag, e.g: @src
std::regex_constants::ECMAScript | std::regex_constants::optimize
);

std::string_view commentLiteral = m_scanner->currentCommentLiteral();
std::match_results<std::string_view::const_iterator> match;

langutil::SourceLocation originLocation = m_locationFromComment;
Expand Down
5 changes: 2 additions & 3 deletions libyul/optimiser/SyntacticalEquality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ bool SyntacticallyEqual::expressionEqual(Identifier const& _lhs, Identifier cons
}
bool SyntacticallyEqual::expressionEqual(Literal const& _lhs, Literal const& _rhs)
{
yulAssert(validLiteral(_lhs), "Invalid lhs literal during syntactical equality check");
yulAssert(validLiteral(_rhs), "Invalid rhs literal during syntactical equality check");

assert(validLiteral(_lhs));
assert(validLiteral(_rhs));
Comment on lines +67 to +68
Copy link
Collaborator

@matheusaaguiar matheusaaguiar Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really significant? validLiteral is still being called twice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd myself just have removed these entirely. Any assertion that we deem ok to just do non-release we might as well not do at all in our context. But whatever, no need to keep this waiting.

Copy link
Member

@cameel cameel Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any assertion that we deem ok to just do non-release we might as well not do at all in our context.

I don't agree. The standard assert macro is perfectly fine as a baseline for sanity checks and even when disabled still adds value by documenting the assumptions. Having asserts checked in release builds is definitely nice to have and saved us many times, but when they're too costly I'd still rather fall back to using assert than remove them completely.

The method validLiteral is nontrivial and should not be called (twice!) on every call to the equality comparison.
It might be desirable to re-introduce the check in a more suitable place.

We're asserting validity here because == does not compare the whole LiteralValue. This is fine for valid literals but not in general and I don't think that's obvious.

A better place for these checks would be the implementation of ==, but that would introduce a circular dependency. And would not make it less costly anyway.

return _lhs.value == _rhs.value;
}

Expand Down