4545import java .util .Iterator ;
4646import java .util .LinkedHashMap ;
4747import java .util .List ;
48- import java .util .Map ;
4948import java .util .concurrent .atomic .AtomicReference ;
5049import java .util .function .Predicate ;
5150
@@ -799,7 +798,7 @@ public ValueNode findLength(FindLengthMode mode, ConstantReflectionProvider cons
799798 }
800799 }
801800
802- static class CacheKey {
801+ public static class CacheKey {
803802
804803 private final ResolvedJavaMethod method ;
805804 private final Object [] values ;
@@ -857,9 +856,6 @@ public int hashCode() {
857856 public static class Options {
858857 @ Option (help = "Use a LRU cache for snippet templates." )//
859858 public static final OptionKey <Boolean > UseSnippetTemplateCache = new OptionKey <>(true );
860-
861- @ Option (help = "" )//
862- public static final OptionKey <Integer > MaxTemplatesPerSnippet = new OptionKey <>(50 );
863859 }
864860
865861 /**
@@ -869,20 +865,13 @@ public abstract static class AbstractTemplates implements SnippetTemplateCache {
869865
870866 protected final OptionValues options ;
871867 protected final SnippetReflectionProvider snippetReflection ;
872- private final Map <CacheKey , SnippetTemplate > templates ;
873868
874869 private final boolean shouldTrackNodeSourcePosition ;
875870
876871 protected AbstractTemplates (OptionValues options , Providers providers ) {
877872 this .options = options ;
878873 this .snippetReflection = providers .getSnippetReflection ();
879874 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- }
886875 }
887876
888877 public static ResolvedJavaMethod findMethod (MetaAccessProvider metaAccess , Class <?> declaringClass , String methodName ) {
@@ -981,7 +970,7 @@ protected SnippetInfo snippet(Providers providers,
981970 public SnippetTemplate template (CoreProviders context , ValueNode replacee , final Arguments args ) {
982971 StructuredGraph graph = replacee .graph ();
983972 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 ;
985974 if (template == null || (graph .trackNodeSourcePosition () && !template .snippet .trackNodeSourcePosition ())) {
986975 try (DebugContext debug = context .getReplacements ().openSnippetDebugContext ("SnippetTemplate_" , args .cacheKey .method , outer , options )) {
987976 try (DebugCloseable a = SnippetTemplateCreationTime .start (outer );
@@ -1002,7 +991,7 @@ public SnippetTemplate template(CoreProviders context, ValueNode replacee, final
1002991 createMidTierPreLoweringPhases (),
1003992 createMidTierPostLoweringPhases ());
1004993 if (Options .UseSnippetTemplateCache .getValue (snippetOptions ) && args .cacheable ) {
1005- templates .put (args .cacheKey , template );
994+ context . getReplacements (). getTemplatesCache () .put (args .cacheKey , template );
1006995 }
1007996 if (outer .areMetricsEnabled ()) {
1008997 DebugContext .counter ("SnippetTemplateNodeCount[%#s]" , args ).add (outer , template .nodes .size ());
@@ -1037,11 +1026,21 @@ protected PhaseSuite<CoreProviders> createMidTierPostLoweringPhases() {
10371026
10381027 public static final class LRUCache <K , V > extends LinkedHashMap <K , V > {
10391028 private static final long serialVersionUID = 1L ;
1029+
1030+ /**
1031+ * Maximum capacity of the least-recently used snippet template cache. A higher number
1032+ * increases the total amount of memory used for snippet templates and a lower number
1033+ * increases the cache misses and thus decreases compilation speed. At a value of 64, the
1034+ * estimated misses are at 2% of lookups, at 80, they are at 1% of lookups, and at 100, they
1035+ * are at 0.5% of lookups.
1036+ */
1037+ public static final int SNIPPET_CACHE_CAPACITY = 64 ;
1038+
10401039 private final int maxCacheSize ;
10411040
1042- public LRUCache (int initialCapacity , int maxCacheSize ) {
1043- super (initialCapacity , 0.75F , true );
1044- this .maxCacheSize = maxCacheSize ;
1041+ public LRUCache () {
1042+ super (SNIPPET_CACHE_CAPACITY , 0.75F , true );
1043+ this .maxCacheSize = SNIPPET_CACHE_CAPACITY ;
10451044 }
10461045
10471046 @ Override
0 commit comments