mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Variables are free with regards to code size.
This commit is contained in:
parent
63319cfdcd
commit
edda79eec5
@ -25,6 +25,9 @@
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <libdevcore/Visitor.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
@ -45,7 +48,7 @@ size_t CodeSize::codeSize(Expression const& _expression)
|
||||
size_t CodeSize::codeSize(Block const& _block)
|
||||
{
|
||||
CodeSize cs;
|
||||
cs(_block);
|
||||
cs.visit(_block);
|
||||
return cs.m_size;
|
||||
}
|
||||
|
||||
@ -53,13 +56,20 @@ void CodeSize::visit(Statement const& _statement)
|
||||
{
|
||||
if (_statement.type() == typeid(FunctionDefinition))
|
||||
return;
|
||||
|
||||
else if (!(
|
||||
_statement.type() == typeid(Block) ||
|
||||
_statement.type() == typeid(ExpressionStatement) ||
|
||||
_statement.type() == typeid(Assignment) ||
|
||||
_statement.type() == typeid(VariableDeclaration)
|
||||
))
|
||||
++m_size;
|
||||
|
||||
ASTWalker::visit(_statement);
|
||||
}
|
||||
|
||||
void CodeSize::visit(Expression const& _expression)
|
||||
{
|
||||
if (_expression.type() != typeid(Identifier))
|
||||
++m_size;
|
||||
ASTWalker::visit(_expression);
|
||||
}
|
||||
|
@ -30,6 +30,13 @@ namespace yul
|
||||
* More specifically, the number of AST nodes.
|
||||
* Ignores function definitions while traversing the AST.
|
||||
* If you want to know the size of a function, you have to invoke this on its body.
|
||||
*
|
||||
* As an exception, the following AST elements have a cost of zero:
|
||||
* - 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)
|
||||
*/
|
||||
class CodeSize: public ASTWalker
|
||||
{
|
||||
@ -39,6 +46,8 @@ public:
|
||||
static size_t codeSize(Block const& _block);
|
||||
|
||||
private:
|
||||
CodeSize() {}
|
||||
|
||||
void visit(Statement const& _statement) override;
|
||||
void visit(Expression const& _expression) override;
|
||||
|
||||
|
116
test/libyul/Metrics.cpp
Normal file
116
test/libyul/Metrics.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Unit tests for the code metrics.
|
||||
*/
|
||||
|
||||
#include <test/Options.h>
|
||||
|
||||
#include <test/libyul/Common.h>
|
||||
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace langutil;
|
||||
|
||||
namespace yul
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
size_t codeSize(string const& _source)
|
||||
{
|
||||
shared_ptr<Block> ast = parse(_source, false).first;
|
||||
BOOST_REQUIRE(ast);
|
||||
return CodeSize::codeSize(*ast);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(YulCodeSize)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(empty_code)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{}"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(nested_blocks)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ {} {} {{ }} }"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(instruction)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ pop(calldatasize()) }"), 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(variables_are_free)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ let x let y let a, b, c }"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(constants_cost_one)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ let x := 3 }"), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(functions_are_skipped)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ function f(x) -> r { r := mload(x) } }"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_with_arguments)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } f(2) }"), 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_with_variables_as_arguments)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } let y f(y) }"), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_with_variables_and_constants_as_arguments)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize(
|
||||
"{ function f(x, r) -> z { sstore(x, r) z := r } let y let t := f(y, 2) }"
|
||||
), 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(assignment)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ let a a := 3 }"), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(assignments_between_vars_are_free)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ let a let b := a a := b }"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(assignment_complex)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(codeSize("{ let a let x := mload(a) a := sload(x) }"), 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user