[Jython-checkins] jython: Add a WeakReference from the generated class back to the JITHandle, so the same

wayne.meissner jython-checkins at python.org
Sun Sep 4 03:55:25 CEST 2011


http://hg.python.org/jython/rev/5d45171d9824
changeset:   6240:5d45171d9824
user:        Wayne Meissner <wmeissner at gmail.com>
date:        Fri Jun 17 23:16:31 2011 +1000
summary:
  Add a WeakReference from the generated class back to the JITHandle, so the same JITHandle is re-used as long as the generated class is alive.

files:
  src/org/python/modules/jffi/JITCompiler.java |  13 ++++++++-
  src/org/python/modules/jffi/JITHandle.java   |  12 ++++++--
  2 files changed, 20 insertions(+), 5 deletions(-)


diff --git a/src/org/python/modules/jffi/JITCompiler.java b/src/org/python/modules/jffi/JITCompiler.java
--- a/src/org/python/modules/jffi/JITCompiler.java
+++ b/src/org/python/modules/jffi/JITCompiler.java
@@ -8,8 +8,10 @@
 
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.WeakHashMap;
 
 /**
  *
@@ -18,9 +20,12 @@
     
     private final Map<JITSignature, HandleRef> 
             handles = new HashMap<JITSignature, HandleRef>();
+
+    private final Map<Class, JITHandle> classes = Collections.synchronizedMap(new WeakHashMap<Class, JITHandle>());
+
     private final ReferenceQueue referenceQueue = new ReferenceQueue();
     
-    private final JITHandle failedHandle = new JITHandle(
+    private final JITHandle failedHandle = new JITHandle(this,
             new JITSignature(NativeType.VOID, new NativeType[0], false, new boolean[0], CallingConvention.DEFAULT, false),
             true);
 
@@ -90,11 +95,15 @@
             HandleRef ref = handles.get(jitSignature);
             JITHandle handle = ref != null ? ref.get() : null;
             if (handle == null) {
-                handle = new JITHandle(jitSignature, false);
+                handle = new JITHandle(this, jitSignature, false);
                 handles.put(jitSignature, new HandleRef(handle, jitSignature, referenceQueue));
             }
             
             return handle;
         }
     }
+
+    void registerClass(JITHandle handle, Class<? extends Invoker> klass) {
+        classes.put(klass, handle);
+    }
 }
diff --git a/src/org/python/modules/jffi/JITHandle.java b/src/org/python/modules/jffi/JITHandle.java
--- a/src/org/python/modules/jffi/JITHandle.java
+++ b/src/org/python/modules/jffi/JITHandle.java
@@ -3,6 +3,7 @@
  */
 package org.python.modules.jffi;
 
+import java.lang.ref.WeakReference;
 import java.lang.reflect.Constructor;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -15,9 +16,11 @@
     private final JITSignature jitSignature;
     private volatile boolean compilationFailed = false;
     private final AtomicInteger counter = new AtomicInteger(0);
-    private volatile Class<? extends Invoker> compiledClass = null;
+    private final JITCompiler compiler;
+    private WeakReference<Class<? extends Invoker>> compiledClassRef = null;
 
-    JITHandle(JITSignature signature, boolean compilationFailed) {
+    JITHandle(JITCompiler compiler, JITSignature signature, boolean compilationFailed) {
+        this.compiler = compiler;
         this.jitSignature = signature;
         this.compilationFailed = compilationFailed;
     }
@@ -31,13 +34,16 @@
             return null;
         }
 
+        Class<? extends Invoker> compiledClass;
         synchronized (this) {
-            if (compiledClass == null) {
+            if (compiledClassRef == null || (compiledClass = compiledClassRef.get()) == null) {
                 compiledClass = newInvokerClass(jitSignature);
                 if (compiledClass == null) {
                     compilationFailed = true;
                     return null;
                 }
+                compiler.registerClass(this, compiledClass);
+                compiledClassRef = new WeakReference<Class<? extends Invoker>>(compiledClass);
             }
         }
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list