Skip to content

Commit

Permalink
[yul-phaser] ProgramCache::calculateTotalCachedCodeSize(): Replace de…
Browse files Browse the repository at this point in the history
…fault weights with ones that better correlate with memory usage
  • Loading branch information
cameel committed May 20, 2020
1 parent bff012c commit 1fa689e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
10 changes: 5 additions & 5 deletions test/yulPhaser/ProgramCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion tools/yulPhaser/ProgramCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions tools/yulPhaser/ProgramCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <tools/yulPhaser/Program.h>

#include <libyul/optimiser/Metrics.h>

#include <map>
#include <string>

Expand All @@ -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;
Expand Down

0 comments on commit 1fa689e

Please sign in to comment.