# HG changeset patch # User Thomas Wuerthinger # Date 1392914538 -3600 # Node ID 25b86e46536514345e0d008ea3bb88ee1339723d # Parent b167b1838029a15b3aff97db60656f8d5deae40d Turn Truffle cache into least recently used cache with maximum size. diff -r b167b1838029 -r 25b86e465365 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCache.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCache.java Thu Feb 20 11:14:46 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCache.java Thu Feb 20 17:42:18 2014 +0100 @@ -26,6 +26,7 @@ import java.lang.reflect.*; import java.util.*; +import java.util.Map.Entry; import com.oracle.graal.api.code.*; import com.oracle.graal.api.meta.*; @@ -55,13 +56,16 @@ */ public final class TruffleCache { + private static final int MAX_CACHE_SIZE = 512; private final Providers providers; private final GraphBuilderConfiguration config; private final OptimisticOptimizations optimisticOptimizations; private final HashMap, StructuredGraph> cache = new HashMap<>(); + private final HashMap, Long> lastUsed = new HashMap<>(); private final StructuredGraph markerGraph = new StructuredGraph(); private final ResolvedJavaType stringBuilderClass; + private long counter; public TruffleCache(Providers providers, GraphBuilderConfiguration config, OptimisticOptimizations optimisticOptimizations) { this.providers = providers; @@ -82,6 +86,7 @@ } StructuredGraph resultGraph = cache.get(key); if (resultGraph != null) { + lastUsed.put(key, counter++); return resultGraph; } @@ -90,6 +95,28 @@ return null; } + if (lastUsed.values().size() >= TruffleCompilerOptions.TruffleMaxCompilationCacheSize.getValue()) { + List lastUsedList = new ArrayList<>(); + for (long l : lastUsed.values()) { + lastUsedList.add(l); + } + Collections.sort(lastUsedList); + long mid = lastUsedList.get(lastUsedList.size() / 2); + + List> toRemoveList = new ArrayList<>(); + for (Entry, Long> entry : lastUsed.entrySet()) { + if (entry.getValue() < mid) { + toRemoveList.add(entry.getKey()); + } + } + + for (List entry : toRemoveList) { + cache.remove(entry); + lastUsed.remove(entry); + } + } + + lastUsed.put(key, counter++); cache.put(key, markerGraph); try (Scope s = Debug.scope("TruffleCache", new Object[]{providers.getMetaAccess(), method})) { diff -r b167b1838029 -r 25b86e465365 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerOptions.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerOptions.java Thu Feb 20 11:14:46 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerOptions.java Thu Feb 20 17:42:18 2014 +0100 @@ -72,6 +72,8 @@ @Option(help = "") public static final OptionValue TruffleCompilationDecisionTime = new OptionValue<>(100); @Option(help = "") + public static final OptionValue TruffleMaxCompilationCacheSize = new OptionValue<>(512); + @Option(help = "") public static final OptionValue TruffleCompilationDecisionTimePrintFail = new OptionValue<>(false); @Option(help = "") public static final OptionValue TruffleBackgroundCompilation = new OptionValue<>(true);