Merge pull request #10341 from ethereum/optimizeAllocation

Do not allocate memory objects if they will be assigned directly.
This commit is contained in:
chriseth
2020-11-24 16:05:46 +01:00
committed by GitHub
9 changed files with 128 additions and 5 deletions
+13 -4
View File
@@ -612,7 +612,7 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
}
for (ASTPointer<VariableDeclaration> const& variable: _function.returnParameters())
appendStackVariableInitialisation(*variable);
appendStackVariableInitialisation(*variable, /* _provideDefaultValue = */ true);
if (_function.isConstructor())
if (auto c = dynamic_cast<ContractDefinition const&>(*_function.scope()).nextConstructor(
@@ -1230,7 +1230,7 @@ bool ContractCompiler::visit(VariableDeclarationStatement const& _variableDeclar
// and freed in the end of their scope.
for (auto decl: _variableDeclarationStatement.declarations())
if (decl)
appendStackVariableInitialisation(*decl);
appendStackVariableInitialisation(*decl, !_variableDeclarationStatement.initialValue());
StackHeightChecker checker(m_context);
if (Expression const* expression = _variableDeclarationStatement.initialValue())
@@ -1376,11 +1376,20 @@ void ContractCompiler::appendModifierOrFunctionCode()
m_context.setModifierDepth(m_modifierDepth);
}
void ContractCompiler::appendStackVariableInitialisation(VariableDeclaration const& _variable)
void ContractCompiler::appendStackVariableInitialisation(
VariableDeclaration const& _variable,
bool _provideDefaultValue
)
{
CompilerContext::LocationSetter location(m_context, _variable);
m_context.addVariable(_variable);
CompilerUtils(m_context).pushZeroValue(*_variable.annotation().type);
if (!_provideDefaultValue && _variable.type()->dataStoredIn(DataLocation::Memory))
{
solAssert(_variable.type()->sizeOnStack() == 1, "");
m_context << u256(0);
}
else
CompilerUtils(m_context).pushZeroValue(*_variable.annotation().type);
}
void ContractCompiler::compileExpression(Expression const& _expression, TypePointer const& _targetType)
+4 -1
View File
@@ -130,7 +130,10 @@ private:
/// body itself if the last modifier was reached.
void appendModifierOrFunctionCode();
void appendStackVariableInitialisation(VariableDeclaration const& _variable);
/// Creates a stack slot for the given variable and assigns a default value.
/// If the default value is complex (needs memory allocation) and @a _provideDefaultValue
/// is false, this might be skipped.
void appendStackVariableInitialisation(VariableDeclaration const& _variable, bool _provideDefaultValue);
void compileExpression(Expression const& _expression, TypePointer const& _targetType = TypePointer());
/// Frees the variables of a certain scope (to be used when leaving).