changeset 2062:231bf6b9f5ad

Added caching for constant pool entries.
author Thomas Wuerthinger <wuerthinger@ssw.jku.at>
date Sat, 22 Jan 2011 17:31:11 +0100
parents c0b1d6a44a02
children 91fe28b03d6a
files c1x4hotspotsrc/HotSpotVM/.settings/org.eclipse.jdt.core.prefs c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotConstantPool.java c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotTypeResolved.java
diffstat 3 files changed, 94 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/c1x4hotspotsrc/HotSpotVM/.settings/org.eclipse.jdt.core.prefs	Sat Jan 22 14:37:43 2011 +0100
+++ b/c1x4hotspotsrc/HotSpotVM/.settings/org.eclipse.jdt.core.prefs	Sat Jan 22 17:31:11 2011 +0100
@@ -1,4 +1,4 @@
-#Mon Aug 23 15:50:20 PDT 2010
+#Sat Jan 22 17:02:08 CET 2011
 eclipse.preferences.version=1
 org.eclipse.jdt.core.builder.cleanOutputFolder=clean
 org.eclipse.jdt.core.builder.duplicateResourceTask=warning
@@ -83,7 +83,7 @@
 org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
 org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
 org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
-org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=error
+org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
 org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
 org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=error
 org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning
--- a/c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotConstantPool.java	Sat Jan 22 14:37:43 2011 +0100
+++ b/c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotConstantPool.java	Sat Jan 22 17:31:11 2011 +0100
@@ -20,6 +20,9 @@
  */
 package com.sun.hotspot.c1x;
 
+import java.lang.reflect.*;
+import java.util.*;
+
 import com.sun.cri.ri.*;
 
 /**
@@ -31,18 +34,77 @@
 
     private final long vmId;
 
+    private final FastLRUIntCache<RiMethod> methodCache = new FastLRUIntCache<RiMethod>();
+    private final FastLRUIntCache<RiField> fieldCache = new FastLRUIntCache<RiField>();
+    private final FastLRUIntCache<RiType> typeCache = new FastLRUIntCache<RiType>();
+
+    public static class FastLRUIntCache<T> {
+
+        private static final int InitialCapacity = 4;
+        private int lastKey;
+        private T lastObject;
+
+        private int[] keys;
+        private Object[] objects;
+        private int count;
+
+        @SuppressWarnings("unchecked")
+        private T access(int index) {
+            return (T) objects[index];
+        }
+
+        public T get(int key) {
+            if (key == lastKey) {
+                return lastObject;
+            } else if (count > 1) {
+                for (int i = 0; i < count; ++i) {
+                    if (keys[i] == key) {
+                        lastObject = access(i);
+                        lastKey = key;
+                        return lastObject;
+                    }
+                }
+            }
+            return null;
+        }
+
+        public void add(int key, T object) {
+            count++;
+            if (count == 1) {
+                lastKey = key;
+                lastObject = object;
+            } else {
+                ensureSize();
+                keys[count - 1] = key;
+                objects[count - 1] = object;
+                if (count == 2) {
+                    keys[0] = lastKey;
+                    objects[0] = lastObject;
+                }
+                lastKey = key;
+                lastObject = object;
+            }
+        }
+
+        private void ensureSize() {
+            if (keys == null) {
+                keys = new int[InitialCapacity];
+                objects = new Object[InitialCapacity];
+            } else if (count > keys.length) {
+                keys = Arrays.copyOf(keys, keys.length * 2);
+                objects = Arrays.copyOf(objects, objects.length * 2);
+            }
+        }
+    }
+
     public HotSpotConstantPool(long vmId) {
         this.vmId = vmId;
     }
 
     @Override
     public Object lookupConstant(int cpi) {
-        return Compiler.getVMEntries().RiConstantPool_lookupConstant(vmId, cpi);
-    }
-
-    @Override
-    public RiMethod lookupMethod(int cpi, int byteCode) {
-        return Compiler.getVMEntries().RiConstantPool_lookupMethod(vmId, cpi, (byte) byteCode);
+        Object constant = Compiler.getVMEntries().RiConstantPool_lookupConstant(vmId, cpi);
+        return constant;
     }
 
     @Override
@@ -51,13 +113,32 @@
     }
 
     @Override
+    public RiMethod lookupMethod(int cpi, int byteCode) {
+        RiMethod result = methodCache.get(cpi);
+        if (result == null) {
+            result = Compiler.getVMEntries().RiConstantPool_lookupMethod(vmId, cpi, (byte) byteCode);
+            methodCache.add(cpi, result);
+        }
+        return result;
+    }
+
+    @Override
     public RiType lookupType(int cpi, int opcode) {
-        return Compiler.getVMEntries().RiConstantPool_lookupType(vmId, cpi);
+        RiType result = typeCache.get(cpi);
+        if (result == null) {
+            result = Compiler.getVMEntries().RiConstantPool_lookupType(vmId, cpi);
+            typeCache.add(cpi, result);
+        }
+        return result;
     }
 
     @Override
     public RiField lookupField(int cpi, int opcode) {
-        return Compiler.getVMEntries().RiConstantPool_lookupField(vmId, cpi, (byte) opcode);
+        RiField result = fieldCache.get(cpi);
+        if (result == null) {
+            result = Compiler.getVMEntries().RiConstantPool_lookupField(vmId, cpi, (byte) opcode);
+            fieldCache.add(cpi, result);
+        }
+        return result;
     }
-
 }
--- a/c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotTypeResolved.java	Sat Jan 22 14:37:43 2011 +0100
+++ b/c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/HotSpotTypeResolved.java	Sat Jan 22 17:31:11 2011 +0100
@@ -46,6 +46,7 @@
     private int instanceSize;
     private RiType componentType;
     private HashMap<Integer, RiField> fieldCache;
+    private RiConstantPool pool;
 
     @Override
     public int accessFlags() {
@@ -172,6 +173,7 @@
     }
 
     public RiConstantPool constantPool() {
+        // TODO: Implement constant pool without the need for VmId and cache the constant pool.
         return Compiler.getVMEntries().RiType_constantPool(this);
     }