[yul-phaser] ProgramSize: Add an option to repeat the optimisation sequence several times

This commit is contained in:
Kamil Śliwak 2020-02-05 19:24:46 +01:00
parent 751caf0ed3
commit 930a9918a6
3 changed files with 40 additions and 2 deletions

View File

@ -72,6 +72,39 @@ BOOST_FIXTURE_TEST_CASE(evaluate_should_compute_size_of_the_optimised_program, F
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) == optimisedProgram.codeSize());
}
BOOST_FIXTURE_TEST_CASE(evaluate_should_repeat_the_optimisation_specified_number_of_times, FitnessMetricFixture)
{
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
Program programOptimisedOnce = m_program;
programOptimisedOnce.optimise(chromosome.optimisationSteps());
Program programOptimisedTwice = programOptimisedOnce;
programOptimisedTwice.optimise(chromosome.optimisationSteps());
assert(m_program.codeSize() != programOptimisedOnce.codeSize());
assert(m_program.codeSize() != programOptimisedTwice.codeSize());
assert(programOptimisedOnce.codeSize() != programOptimisedTwice.codeSize());
ProgramSize metric(m_program, 2);
BOOST_TEST(metric.evaluate(chromosome) != m_program.codeSize());
BOOST_TEST(metric.evaluate(chromosome) != programOptimisedOnce.codeSize());
BOOST_TEST(metric.evaluate(chromosome) == programOptimisedTwice.codeSize());
}
BOOST_FIXTURE_TEST_CASE(evaluate_should_not_optimise_if_number_of_repetitions_is_zero, FitnessMetricFixture)
{
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
Program optimisedProgram = m_program;
optimisedProgram.optimise(chromosome.optimisationSteps());
assert(m_program.codeSize() != optimisedProgram.codeSize());
ProgramSize metric(m_program, 0);
BOOST_TEST(metric.evaluate(chromosome) == m_program.codeSize());
BOOST_TEST(metric.evaluate(chromosome) != optimisedProgram.codeSize());
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

View File

@ -23,6 +23,8 @@ using namespace solidity::phaser;
size_t ProgramSize::evaluate(Chromosome const& _chromosome) const
{
Program programCopy = m_program;
programCopy.optimise(_chromosome.optimisationSteps());
for (size_t i = 0; i < m_repetitionCount; ++i)
programCopy.optimise(_chromosome.optimisationSteps());
return programCopy.codeSize();
}

View File

@ -53,12 +53,15 @@ public:
class ProgramSize: public FitnessMetric
{
public:
ProgramSize(Program _program): m_program(std::move(_program)) {}
explicit ProgramSize(Program _program, size_t _repetitionCount = 1):
m_program(std::move(_program)),
m_repetitionCount(_repetitionCount) {}
size_t evaluate(Chromosome const& _chromosome) const override;
private:
Program m_program;
size_t m_repetitionCount;
};
}