mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add RelativeProgramSize metric
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
@@ -33,3 +35,18 @@ size_t ProgramSize::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
return optimisedProgram(_chromosome).codeSize();
|
||||
}
|
||||
|
||||
size_t RelativeProgramSize::evaluate(Chromosome const& _chromosome)
|
||||
{
|
||||
size_t const scalingFactor = pow(10, m_fixedPointPrecision);
|
||||
|
||||
size_t unoptimisedSize = optimisedProgram(Chromosome("")).codeSize();
|
||||
if (unoptimisedSize == 0)
|
||||
return scalingFactor;
|
||||
|
||||
size_t optimisedSize = optimisedProgram(_chromosome).codeSize();
|
||||
|
||||
return static_cast<size_t>(round(
|
||||
static_cast<double>(optimisedSize) / unoptimisedSize * scalingFactor
|
||||
));
|
||||
}
|
||||
|
||||
@@ -87,4 +87,30 @@ public:
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fitness metric based on the size of a specific program after applying the optimisations from the
|
||||
* chromosome to it in relation to the original, unoptimised program.
|
||||
*
|
||||
* Since metric values are integers, the class multiplies the ratio by 10^@a _fixedPointPrecision
|
||||
* before rounding it.
|
||||
*/
|
||||
class RelativeProgramSize: public ProgramBasedMetric
|
||||
{
|
||||
public:
|
||||
explicit RelativeProgramSize(
|
||||
Program _program,
|
||||
size_t _fixedPointPrecision,
|
||||
size_t _repetitionCount = 1
|
||||
):
|
||||
ProgramBasedMetric(std::move(_program), _repetitionCount),
|
||||
m_fixedPointPrecision(_fixedPointPrecision) {}
|
||||
|
||||
size_t fixedPointPrecision() const { return m_fixedPointPrecision; }
|
||||
|
||||
size_t evaluate(Chromosome const& _chromosome) override;
|
||||
|
||||
private:
|
||||
size_t m_fixedPointPrecision;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user