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 10:32:39 UTC

[groovy] branch danielsun/tweak-ccs updated (7803ebaeb4 -> dc858c360c)

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

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


 discard 7803ebaeb4 GROOVY-10772: Hold `MethodHandleWrapper` with `SoftReference` in cache
     new dc858c360c GROOVY-10772: Hold `MethodHandleWrapper` with `SoftReference` in cache

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (7803ebaeb4)
            \
             N -- N -- N   refs/heads/danielsun/tweak-ccs (dc858c360c)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/vmplugin/v8/CacheableCallSite.java         | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)


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

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit dc858c360c71ac491e3e126e5b5dce6ba563e492
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Dec 4 18:05:01 2022 +0800

    GROOVY-10772: Hold `MethodHandleWrapper` with `SoftReference` in cache
---
 .../groovy/vmplugin/v8/CacheableCallSite.java      | 37 +++++++++++++++++++---
 1 file changed, 32 insertions(+), 5 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 aaa7138dbe..a15a7bb0d6 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
@@ -24,6 +24,7 @@ import org.codehaus.groovy.runtime.memoize.MemoizeCache;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodType;
 import java.lang.invoke.MutableCallSite;
+import java.lang.ref.SoftReference;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
@@ -41,8 +42,8 @@ public class CacheableCallSite extends MutableCallSite {
     private final AtomicLong fallbackCount = new AtomicLong();
     private MethodHandle defaultTarget;
     private MethodHandle fallbackTarget;
-    private final Map<String, MethodHandleWrapper> lruCache =
-            new LinkedHashMap<String, MethodHandleWrapper>(INITIAL_CAPACITY, LOAD_FACTOR, true) {
+    private final Map<String, SoftReference<MethodHandleWrapper>> lruCache =
+            new LinkedHashMap<>(INITIAL_CAPACITY, LOAD_FACTOR, true) {
                 private static final long serialVersionUID = 7785958879964294463L;
 
                 @Override
@@ -56,9 +57,22 @@ public class CacheableCallSite extends MutableCallSite {
     }
 
     public MethodHandleWrapper getAndPut(String className, MemoizeCache.ValueProvider<? super String, ? extends MethodHandleWrapper> valueProvider) {
-        final MethodHandleWrapper result;
+        MethodHandleWrapper result = null;
         synchronized (lruCache) {
-            result = lruCache.computeIfAbsent(className, valueProvider::provide);
+            final SoftReference<MethodHandleWrapper> methodHandleWrapperSoftReference = lruCache.get(className);
+            if (null != methodHandleWrapperSoftReference) {
+                result = methodHandleWrapperSoftReference.get();
+
+                if (null == result) {
+                    // remove all stale entries
+                    removeAllStaleEntriesOfLruCache();
+                }
+            }
+
+            if (null == result) {
+                result = valueProvider.provide(className);
+                lruCache.put(className, new SoftReference<>(result));
+            }
         }
         final MethodHandleWrapper lhmh = latestHitMethodHandleWrapper;
 
@@ -76,10 +90,23 @@ public class CacheableCallSite extends MutableCallSite {
 
     public MethodHandleWrapper put(String name, MethodHandleWrapper mhw) {
         synchronized (lruCache) {
-            return lruCache.put(name, mhw);
+            final SoftReference<MethodHandleWrapper> methodHandleWrapperSoftReference;
+            methodHandleWrapperSoftReference = lruCache.put(name, new SoftReference<>(mhw));
+            if (null == methodHandleWrapperSoftReference) {
+                return null;
+            }
+            final MethodHandleWrapper methodHandleWrapper = methodHandleWrapperSoftReference.get();
+            if (null == methodHandleWrapper) {
+                removeAllStaleEntriesOfLruCache();
+            }
+            return methodHandleWrapper;
         }
     }
 
+    private void removeAllStaleEntriesOfLruCache() {
+        lruCache.values().removeIf(v -> null == v.get());
+    }
+
     public long incrementFallbackCount() {
         return fallbackCount.incrementAndGet();
     }