You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/12/04 16:16:39 UTC

[groovy] 01/01: GROOVY-10772: Hold `MethodHandleWrapper` with `SoftReference`

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch danielsun/tweak-ccs-further
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 448622e495d91f226f6c84c59e8c79e205c4249f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Dec 5 00:16:30 2022 +0800

    GROOVY-10772: Hold `MethodHandleWrapper` with `SoftReference`
---
 .../groovy/vmplugin/v8/CacheableCallSite.java      | 23 +++++++++-------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
index f998dd781e..0de1ac8d6a 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
@@ -38,12 +38,12 @@ public class CacheableCallSite extends MutableCallSite {
     private static final int CACHE_SIZE = SystemUtil.getIntegerSafe("groovy.indy.callsite.cache.size", 4);
     private static final float LOAD_FACTOR = 0.75f;
     private static final int INITIAL_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) + 1;
-    private volatile MethodHandleWrapper latestHitMethodHandleWrapper = null;
+    private volatile SoftReference<MethodHandleWrapper> methodHandleWrapperSoftReference = null;
     private final AtomicLong fallbackCount = new AtomicLong();
     private MethodHandle defaultTarget;
     private MethodHandle fallbackTarget;
     private final Map<String, SoftReference<MethodHandleWrapper>> lruCache =
-            new LinkedHashMap<>(INITIAL_CAPACITY, LOAD_FACTOR, true) {
+            new LinkedHashMap<String, SoftReference<MethodHandleWrapper>>(INITIAL_CAPACITY, LOAD_FACTOR, true) {
                 private static final long serialVersionUID = 7785958879964294463L;
 
                 @Override
@@ -62,10 +62,7 @@ public class CacheableCallSite extends MutableCallSite {
             final SoftReference<MethodHandleWrapper> methodHandleWrapperSoftReference = lruCache.get(className);
             if (null != methodHandleWrapperSoftReference) {
                 result = methodHandleWrapperSoftReference.get();
-
-                if (null == result) {
-                    removeAllStaleEntriesOfLruCache();
-                }
+                if (null == result) removeAllStaleEntriesOfLruCache();
             }
 
             if (null == result) {
@@ -73,15 +70,15 @@ public class CacheableCallSite extends MutableCallSite {
                 lruCache.put(className, new SoftReference<>(result));
             }
         }
-        final MethodHandleWrapper lhmh = latestHitMethodHandleWrapper;
+        final SoftReference<MethodHandleWrapper> mhwsr = methodHandleWrapperSoftReference;
+        final MethodHandleWrapper methodHandleWrapper = null == mhwsr ? null : mhwsr.get();
 
-        if (lhmh == result) {
+        if (methodHandleWrapper == result) {
             result.incrementLatestHitCount();
         } else {
             result.resetLatestHitCount();
-            if (null != lhmh) lhmh.resetLatestHitCount();
-
-            latestHitMethodHandleWrapper = result;
+            if (null != methodHandleWrapper) methodHandleWrapper.resetLatestHitCount();
+            methodHandleWrapperSoftReference = new SoftReference<>(result);
         }
 
         return result;
@@ -95,9 +92,7 @@ public class CacheableCallSite extends MutableCallSite {
                 return null;
             }
             final MethodHandleWrapper methodHandleWrapper = methodHandleWrapperSoftReference.get();
-            if (null == methodHandleWrapper) {
-                removeAllStaleEntriesOfLruCache();
-            }
+            if (null == methodHandleWrapper) removeAllStaleEntriesOfLruCache();
             return methodHandleWrapper;
         }
     }