45
45
import java .util .Iterator ;
46
46
import java .util .LinkedHashMap ;
47
47
import java .util .List ;
48
- import java .util .Map ;
49
48
import java .util .concurrent .atomic .AtomicReference ;
50
49
import java .util .function .Predicate ;
51
50
@@ -799,7 +798,7 @@ public ValueNode findLength(FindLengthMode mode, ConstantReflectionProvider cons
799
798
}
800
799
}
801
800
802
- static class CacheKey {
801
+ public static class CacheKey {
803
802
804
803
private final ResolvedJavaMethod method ;
805
804
private final Object [] values ;
@@ -857,9 +856,6 @@ public int hashCode() {
857
856
public static class Options {
858
857
@ Option (help = "Use a LRU cache for snippet templates." )//
859
858
public static final OptionKey <Boolean > UseSnippetTemplateCache = new OptionKey <>(true );
860
-
861
- @ Option (help = "" )//
862
- public static final OptionKey <Integer > MaxTemplatesPerSnippet = new OptionKey <>(50 );
863
859
}
864
860
865
861
/**
@@ -869,20 +865,13 @@ public abstract static class AbstractTemplates implements SnippetTemplateCache {
869
865
870
866
protected final OptionValues options ;
871
867
protected final SnippetReflectionProvider snippetReflection ;
872
- private final Map <CacheKey , SnippetTemplate > templates ;
873
868
874
869
private final boolean shouldTrackNodeSourcePosition ;
875
870
876
871
protected AbstractTemplates (OptionValues options , Providers providers ) {
877
872
this .options = options ;
878
873
this .snippetReflection = providers .getSnippetReflection ();
879
874
this .shouldTrackNodeSourcePosition = providers .getCodeCache () != null && providers .getCodeCache ().shouldDebugNonSafepoints ();
880
- if (Options .UseSnippetTemplateCache .getValue (options )) {
881
- int size = Options .MaxTemplatesPerSnippet .getValue (options );
882
- this .templates = Collections .synchronizedMap (new LRUCache <>(size , size ));
883
- } else {
884
- this .templates = null ;
885
- }
886
875
}
887
876
888
877
public static ResolvedJavaMethod findMethod (MetaAccessProvider metaAccess , Class <?> declaringClass , String methodName ) {
@@ -981,7 +970,7 @@ protected SnippetInfo snippet(Providers providers,
981
970
public SnippetTemplate template (CoreProviders context , ValueNode replacee , final Arguments args ) {
982
971
StructuredGraph graph = replacee .graph ();
983
972
DebugContext outer = graph .getDebug ();
984
- SnippetTemplate template = Options .UseSnippetTemplateCache .getValue (options ) && args .cacheable ? templates .get (args .cacheKey ) : null ;
973
+ SnippetTemplate template = Options .UseSnippetTemplateCache .getValue (options ) && args .cacheable ? context . getReplacements (). getTemplatesCache () .get (args .cacheKey ) : null ;
985
974
if (template == null || (graph .trackNodeSourcePosition () && !template .snippet .trackNodeSourcePosition ())) {
986
975
try (DebugContext debug = context .getReplacements ().openSnippetDebugContext ("SnippetTemplate_" , args .cacheKey .method , outer , options )) {
987
976
try (DebugCloseable a = SnippetTemplateCreationTime .start (outer );
@@ -1002,7 +991,7 @@ public SnippetTemplate template(CoreProviders context, ValueNode replacee, final
1002
991
createMidTierPreLoweringPhases (),
1003
992
createMidTierPostLoweringPhases ());
1004
993
if (Options .UseSnippetTemplateCache .getValue (snippetOptions ) && args .cacheable ) {
1005
- templates .put (args .cacheKey , template );
994
+ context . getReplacements (). getTemplatesCache () .put (args .cacheKey , template );
1006
995
}
1007
996
if (outer .areMetricsEnabled ()) {
1008
997
DebugContext .counter ("SnippetTemplateNodeCount[%#s]" , args ).add (outer , template .nodes .size ());
@@ -1037,11 +1026,20 @@ protected PhaseSuite<CoreProviders> createMidTierPostLoweringPhases() {
1037
1026
1038
1027
public static final class LRUCache <K , V > extends LinkedHashMap <K , V > {
1039
1028
private static final long serialVersionUID = 1L ;
1029
+
1030
+ /**
1031
+ * Maximum capacity of the least-recently used snippet template cache. A higher number increases the total amount
1032
+ * of memory used for snippet templates and a lower number increases the cache misses and thus decreases compilation
1033
+ * speed. At a value of 64, the estimated misses are at 2% of lookups, at 80, they are at 1% of lookups, and
1034
+ * at 100, they are at 0.5% of lookups.
1035
+ */
1036
+ public static final int SNIPPET_CACHE_CAPACITY = 64 ;
1037
+
1040
1038
private final int maxCacheSize ;
1041
1039
1042
- public LRUCache (int initialCapacity , int maxCacheSize ) {
1043
- super (initialCapacity , 0.75F , true );
1044
- this .maxCacheSize = maxCacheSize ;
1040
+ public LRUCache () {
1041
+ super (SNIPPET_CACHE_CAPACITY , 0.75F , true );
1042
+ this .maxCacheSize = SNIPPET_CACHE_CAPACITY ;
1045
1043
}
1046
1044
1047
1045
@ Override
0 commit comments