mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Adjust cost of literal zero.
This commit is contained in:
@@ -70,8 +70,14 @@ size_t CodeWeights::costOf(Expression const& _expression) const
|
||||
return functionCallCost;
|
||||
else if (holds_alternative<Identifier>(_expression))
|
||||
return identifierCost;
|
||||
else if (holds_alternative<Literal>(_expression))
|
||||
return literalCost;
|
||||
else if (Literal const* literal = get_if<Literal>(&_expression))
|
||||
{
|
||||
// Avoid strings because they could be longer than 32 bytes.
|
||||
if (literal->kind != LiteralKind::String && valueOfLiteral(*literal) == 0)
|
||||
return literalZeroCost;
|
||||
else
|
||||
return literalCost;
|
||||
}
|
||||
else
|
||||
yulAssert(false, "If you add a new expression type, you must update CodeWeights.");
|
||||
}
|
||||
|
||||
@@ -36,12 +36,13 @@ struct EVMDialect;
|
||||
* The default values are meant to reflect specifically the number of AST nodes.
|
||||
*
|
||||
* The following AST elements have a default cost of zero (because the cleanup phase would
|
||||
* remove them anyway or they are just wrappers around something else will be counted instead):
|
||||
* remove them anyway or they are just wrappers around something else that will be counted instead):
|
||||
* - expression statement (only the expression inside has a cost)
|
||||
* - block (only the statements inside have a cost)
|
||||
* - variable references
|
||||
* - variable declarations (only the right hand side has a cost)
|
||||
* - assignments (only the value has a cost)
|
||||
* - literal zeros (we optimistically assume they can be copied from somewhere else)
|
||||
*
|
||||
* Each statement incurs and additional cost of one
|
||||
* per jump/branch. This means if, break and continue statements have a cost of 2,
|
||||
@@ -68,6 +69,7 @@ struct CodeWeights
|
||||
size_t functionCallCost = 1;
|
||||
size_t identifierCost = 0;
|
||||
size_t literalCost = 1;
|
||||
size_t literalZeroCost = 0;
|
||||
|
||||
size_t costOf(Statement const& _statement) const;
|
||||
size_t costOf(Expression const& _expression) const;
|
||||
|
||||
Reference in New Issue
Block a user