mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Eliminates dead code around GasEstimator
- structuralEstimation() - breakToStatementLevel()
This commit is contained in:
@@ -42,93 +42,6 @@ using namespace solidity::evmasm;
|
||||
using namespace solidity::frontend;
|
||||
using namespace solidity::langutil;
|
||||
|
||||
GasEstimator::ASTGasConsumptionSelfAccumulated GasEstimator::structuralEstimation(
|
||||
AssemblyItems const& _items,
|
||||
vector<ASTNode const*> const& _ast
|
||||
) const
|
||||
{
|
||||
solAssert(std::count(_ast.begin(), _ast.end(), nullptr) == 0, "");
|
||||
map<SourceLocation, GasConsumption> particularCosts;
|
||||
|
||||
ControlFlowGraph cfg(_items);
|
||||
for (BasicBlock const& block: cfg.optimisedBlocks())
|
||||
{
|
||||
solAssert(!!block.startState, "");
|
||||
GasMeter meter(block.startState->copy(), m_evmVersion);
|
||||
auto const end = _items.begin() + static_cast<ptrdiff_t>(block.end);
|
||||
for (auto iter = _items.begin() + static_cast<ptrdiff_t>(block.begin); iter != end; ++iter)
|
||||
particularCosts[iter->location()] += meter.estimateMax(*iter);
|
||||
}
|
||||
|
||||
set<ASTNode const*> finestNodes = finestNodesAtLocation(_ast);
|
||||
ASTGasConsumptionSelfAccumulated gasCosts;
|
||||
auto onNode = [&](ASTNode const& _node)
|
||||
{
|
||||
if (!finestNodes.count(&_node))
|
||||
return true;
|
||||
gasCosts[&_node][0] = gasCosts[&_node][1] = particularCosts[_node.location()];
|
||||
return true;
|
||||
};
|
||||
auto onEdge = [&](ASTNode const& _parent, ASTNode const& _child)
|
||||
{
|
||||
gasCosts[&_parent][1] += gasCosts[&_child][1];
|
||||
};
|
||||
ASTReduce folder(onNode, onEdge);
|
||||
for (ASTNode const* ast: _ast)
|
||||
ast->accept(folder);
|
||||
|
||||
return gasCosts;
|
||||
}
|
||||
|
||||
map<ASTNode const*, GasMeter::GasConsumption> GasEstimator::breakToStatementLevel(
|
||||
ASTGasConsumptionSelfAccumulated const& _gasCosts,
|
||||
vector<ASTNode const*> const& _roots
|
||||
)
|
||||
{
|
||||
solAssert(std::count(_roots.begin(), _roots.end(), nullptr) == 0, "");
|
||||
// first pass: statementDepth[node] is the distance from the deepend statement to node
|
||||
// in direction of the tree root (or undefined if not possible)
|
||||
map<ASTNode const*, int> statementDepth;
|
||||
auto onNodeFirstPass = [&](ASTNode const& _node)
|
||||
{
|
||||
if (dynamic_cast<Statement const*>(&_node))
|
||||
statementDepth[&_node] = 0;
|
||||
return true;
|
||||
};
|
||||
auto onEdgeFirstPass = [&](ASTNode const& _parent, ASTNode const& _child)
|
||||
{
|
||||
if (statementDepth.count(&_child))
|
||||
statementDepth[&_parent] = max(statementDepth[&_parent], statementDepth[&_child] + 1);
|
||||
};
|
||||
ASTReduce firstPass(onNodeFirstPass, onEdgeFirstPass);
|
||||
for (ASTNode const* node: _roots)
|
||||
node->accept(firstPass);
|
||||
|
||||
// we use the location of a node if
|
||||
// - its statement depth is 0 or
|
||||
// - its statement depth is undefined but the parent's statement depth is at least 1
|
||||
map<ASTNode const*, GasConsumption> gasCosts;
|
||||
auto onNodeSecondPass = [&](ASTNode const& _node)
|
||||
{
|
||||
return statementDepth.count(&_node);
|
||||
};
|
||||
auto onEdgeSecondPass = [&](ASTNode const& _parent, ASTNode const& _child)
|
||||
{
|
||||
bool useNode = false;
|
||||
if (statementDepth.count(&_child))
|
||||
useNode = statementDepth[&_child] == 0;
|
||||
else
|
||||
useNode = statementDepth.count(&_parent) && statementDepth.at(&_parent) > 0;
|
||||
if (useNode)
|
||||
gasCosts[&_child] = _gasCosts.at(&_child)[1];
|
||||
};
|
||||
ASTReduce secondPass(onNodeSecondPass, onEdgeSecondPass);
|
||||
for (ASTNode const* node: _roots)
|
||||
node->accept(secondPass);
|
||||
// gasCosts should only contain non-overlapping locations
|
||||
return gasCosts;
|
||||
}
|
||||
|
||||
GasEstimator::GasConsumption GasEstimator::functionalEstimation(
|
||||
AssemblyItems const& _items,
|
||||
string const& _signature
|
||||
|
||||
@@ -48,22 +48,6 @@ public:
|
||||
|
||||
explicit GasEstimator(langutil::EVMVersion _evmVersion): m_evmVersion(_evmVersion) {}
|
||||
|
||||
/// Estimates the gas consumption for every assembly item in the given assembly and stores
|
||||
/// it by source location.
|
||||
/// @returns a mapping from each AST node to a pair of its particular and syntactically accumulated gas costs.
|
||||
ASTGasConsumptionSelfAccumulated structuralEstimation(
|
||||
evmasm::AssemblyItems const& _items,
|
||||
std::vector<ASTNode const*> const& _ast
|
||||
) const;
|
||||
/// @returns a mapping from nodes with non-overlapping source locations to gas consumptions such that
|
||||
/// the following source locations are part of the mapping:
|
||||
/// 1. source locations of statements that do not contain other statements
|
||||
/// 2. maximal source locations that do not overlap locations coming from the first rule
|
||||
static ASTGasConsumption breakToStatementLevel(
|
||||
ASTGasConsumptionSelfAccumulated const& _gasCosts,
|
||||
std::vector<ASTNode const*> const& _roots
|
||||
);
|
||||
|
||||
/// @returns the estimated gas consumption by the (public or external) function with the
|
||||
/// given signature. If no signature is given, estimates the maximum gas usage.
|
||||
GasConsumption functionalEstimation(
|
||||
|
||||
Reference in New Issue
Block a user