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

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0bd13c3664 Minor tweak: clean PIC in another thread
0bd13c3664 is described below

commit 0bd13c36649874f7bf4b2c7e98eeef4b2ae55e8d
Author: Daniel Sun <[email protected]>
AuthorDate: Sun Jan 12 01:07:18 2025 +0900

    Minor tweak: clean PIC in another thread
---
 .../groovy/vmplugin/v8/CacheableCallSite.java      | 29 +++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

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 9e00ccfa8e..58a6dfac5f 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/CacheableCallSite.java
@@ -19,6 +19,7 @@
 package org.codehaus.groovy.vmplugin.v8;
 
 import org.apache.groovy.util.SystemUtil;
+import org.codehaus.groovy.runtime.DefaultGroovyMethods;
 import org.codehaus.groovy.runtime.memoize.MemoizeCache;
 
 import java.lang.invoke.MethodHandle;
@@ -28,7 +29,11 @@ import java.lang.invoke.MutableCallSite;
 import java.lang.ref.SoftReference;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Represents a cacheable call site, which can reduce the cost of resolving 
methods
@@ -101,7 +106,11 @@ public class CacheableCallSite extends MutableCallSite {
     }
 
     private void removeAllStaleEntriesOfLruCache() {
-        lruCache.values().removeIf(v -> null == v.get());
+        CACHE_CLEANER_QUEUE.offer(() -> {
+            synchronized (lruCache) {
+                lruCache.values().removeIf(v -> null == v.get());
+            }
+        });
     }
 
     public long incrementFallbackCount() {
@@ -131,4 +140,22 @@ public class CacheableCallSite extends MutableCallSite {
     public MethodHandles.Lookup getLookup() {
         return lookup;
     }
+
+    private static final BlockingQueue<Runnable> CACHE_CLEANER_QUEUE = new 
LinkedBlockingQueue<>();
+    static {
+        Thread cacheCleaner = new Thread(() -> {
+            while (true) {
+                try {
+                    CACHE_CLEANER_QUEUE.take().run();
+                } catch (Throwable ignore) {
+                    Logger logger = 
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
+                    if (logger.isLoggable(Level.FINEST)) {
+                        logger.finest(DefaultGroovyMethods.asString(ignore));
+                    }
+                }
+            }
+        }, "PIC-Cleaner");
+        cacheCleaner.setDaemon(true);
+        cacheCleaner.start();
+    }
 }

Reply via email to