diff --git a/test/yulPhaser/ProgramCache.cpp b/test/yulPhaser/ProgramCache.cpp index 5e2a67c2a..3b652e3fb 100644 --- a/test/yulPhaser/ProgramCache.cpp +++ b/test/yulPhaser/ProgramCache.cpp @@ -214,11 +214,11 @@ BOOST_FIXTURE_TEST_CASE(startRound_should_remove_entries_older_than_two_rounds, BOOST_FIXTURE_TEST_CASE(gatherStats_should_return_cache_statistics, ProgramCacheFixture) { - size_t sizeI = optimisedProgram(m_program, "I").codeSize(CodeWeights{}); - size_t sizeIu = optimisedProgram(m_program, "Iu").codeSize(CodeWeights{}); - size_t sizeIuO = optimisedProgram(m_program, "IuO").codeSize(CodeWeights{}); - size_t sizeL = optimisedProgram(m_program, "L").codeSize(CodeWeights{}); - size_t sizeLT = optimisedProgram(m_program, "LT").codeSize(CodeWeights{}); + size_t sizeI = optimisedProgram(m_program, "I").codeSize(CacheStats::StorageWeights); + size_t sizeIu = optimisedProgram(m_program, "Iu").codeSize(CacheStats::StorageWeights); + size_t sizeIuO = optimisedProgram(m_program, "IuO").codeSize(CacheStats::StorageWeights); + size_t sizeL = optimisedProgram(m_program, "L").codeSize(CacheStats::StorageWeights); + size_t sizeLT = optimisedProgram(m_program, "LT").codeSize(CacheStats::StorageWeights); m_programCache.optimiseProgram("L"); m_programCache.optimiseProgram("Iu"); diff --git a/tools/yulPhaser/ProgramCache.cpp b/tools/yulPhaser/ProgramCache.cpp index 2556ea7c3..b174ad07f 100644 --- a/tools/yulPhaser/ProgramCache.cpp +++ b/tools/yulPhaser/ProgramCache.cpp @@ -135,7 +135,7 @@ size_t ProgramCache::calculateTotalCachedCodeSize() const { size_t size = 0; for (auto const& pair: m_entries) - size += pair.second.program.codeSize(CodeWeights{}); + size += pair.second.program.codeSize(CacheStats::StorageWeights); return size; } diff --git a/tools/yulPhaser/ProgramCache.h b/tools/yulPhaser/ProgramCache.h index 8cc830098..25c796a3d 100644 --- a/tools/yulPhaser/ProgramCache.h +++ b/tools/yulPhaser/ProgramCache.h @@ -19,6 +19,8 @@ #include +#include + #include #include @@ -44,6 +46,29 @@ struct CacheEntry */ struct CacheStats { + /// Weights used to compute totalCodeSize. + /// The goal here is to get a result proportional to the amount of memory taken by the AST. + /// Each statement/expression gets 1 just for existing. We add more if it contains any extra + /// data that won't be visited separately by ASTWalker. + static yul::CodeWeights constexpr StorageWeights = { + /* expressionStatementCost = */ 1, + /* assignmentCost = */ 1, + /* variableDeclarationCost = */ 1, + /* functionDefinitionCost = */ 1, + /* ifCost = */ 1, + /* switchCost = */ 1, + /* caseCost = */ 1, + /* forLoopCost = */ 1, + /* breakCost = */ 1, + /* continueCost = */ 1, + /* leaveCost = */ 1, + /* blockCost = */ 1, + + /* functionCallCost = */ 1, + /* identifierCost = */ 1, + /* literalCost = */ 1, + }; + size_t hits; size_t misses; size_t totalCodeSize;