Skip to content

Commit

Permalink
Avoid sorting FunctionDefinitions by AST ID during codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Sep 12, 2023
1 parent 34c86d9 commit f840539
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
6 changes: 3 additions & 3 deletions libsolidity/codegen/ir/IRGenerationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::string IRGenerationContext::enqueueFunctionForCodeGeneration(FunctionDefini
std::string name = IRNames::function(_function);

if (!m_functions.contains(name))
m_functionGenerationQueue.insert(&_function);
m_functionGenerationQueue.push(&_function);

return name;
}
Expand All @@ -50,8 +50,8 @@ FunctionDefinition const* IRGenerationContext::dequeueFunctionForCodeGeneration(
{
solAssert(!m_functionGenerationQueue.empty(), "");

FunctionDefinition const* result = *m_functionGenerationQueue.begin();
m_functionGenerationQueue.erase(m_functionGenerationQueue.begin());
FunctionDefinition const* result = m_functionGenerationQueue.front();
m_functionGenerationQueue.pop();
return result;
}

Expand Down
24 changes: 6 additions & 18 deletions libsolidity/codegen/ir/IRGenerationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,16 @@
#include <set>
#include <string>
#include <memory>
#include <vector>
#include <queue>

namespace solidity::frontend
{

class YulUtilFunctions;
class ABIFunctions;

struct AscendingFunctionIDCompare
{
bool operator()(FunctionDefinition const* _f1, FunctionDefinition const* _f2) const
{
// NULLs always first.
if (_f1 != nullptr && _f2 != nullptr)
return _f1->id() < _f2->id();
else
return _f1 == nullptr;
}
};

using DispatchSet = std::set<FunctionDefinition const*, AscendingFunctionIDCompare>;
using DispatchQueue = std::queue<FunctionDefinition const*>;
using DispatchSet = std::set<FunctionDefinition const*>;
using InternalDispatchMap = std::map<YulArity, DispatchSet>;

/**
Expand Down Expand Up @@ -197,10 +186,9 @@ class IRGenerationContext
/// were discovered by the IR generator during AST traversal.
/// Note that the queue gets filled in a lazy way - new definitions can be added while the
/// collected ones get removed and traversed.
/// The order and duplicates are irrelevant here (hence std::set rather than std::queue) as
/// long as the order of Yul functions in the generated code is deterministic and the same on
/// all platforms - which is a property guaranteed by MultiUseYulFunctionCollector.
DispatchSet m_functionGenerationQueue;
/// The order and duplicates are relevant here
/// (see: IRGenerationContext::[enqueue|dequeue]FunctionForCodeGeneration)
DispatchQueue m_functionGenerationQueue;

/// Collection of functions that need to be callable via internal dispatch.
/// Note that having a key with an empty set of functions is a valid situation. It means that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ contract C {
// gas legacy: 411269
// gas legacyOptimized: 317754
// test_uint256() ->
// gas irOptimized: 509677
// gas irOptimized: 509479
// gas legacy: 577469
// gas legacyOptimized: 441003
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ contract C {
// gas legacy: 411269
// gas legacyOptimized: 317754
// test_uint256() ->
// gas irOptimized: 509677
// gas irOptimized: 509479
// gas legacy: 577469
// gas legacyOptimized: 441003

0 comments on commit f840539

Please sign in to comment.