summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorahaase2008-02-18 06:57:13 (EST)
committerahaase2008-02-18 06:57:13 (EST)
commitaeacc5a21edf02c3eabdcb596bbb235d86026fdd (patch)
tree2f77c8b9c15466b4980e8f1133e9171d41f40b46
parent3c81ed8385dc7769bef753e9bfed174af7ed772c (diff)
downloadorg.eclipse.xpand-aeacc5a21edf02c3eabdcb596bbb235d86026fdd.zip
org.eclipse.xpand-aeacc5a21edf02c3eabdcb596bbb235d86026fdd.tar.gz
org.eclipse.xpand-aeacc5a21edf02c3eabdcb596bbb235d86026fdd.tar.bz2
fine tuning of the type system implementations with regard to caching
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/CompositeTypesystem.java61
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/util/IdentityCache.java38
2 files changed, 84 insertions, 15 deletions
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/CompositeTypesystem.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/CompositeTypesystem.java
index 50ee206..39f0263 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/CompositeTypesystem.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/CompositeTypesystem.java
@@ -37,6 +37,7 @@ import org.eclipse.xtend.backend.types.builtin.TypeType;
import org.eclipse.xtend.backend.types.builtin.VoidType;
import org.eclipse.xtend.backend.types.java.internal.GlobalJavaBeansTypesystem;
import org.eclipse.xtend.backend.util.Cache;
+import org.eclipse.xtend.backend.util.IdentityCache;
/**
@@ -54,21 +55,40 @@ public final class CompositeTypesystem implements BackendTypesystem {
_javaBeansTypesystem.setRootTypesystem (_rootTypesystem);
}
+ private final IdentityCache<Object, BackendType> _typeByInstanceCache = new IdentityCache<Object, BackendType>() {
+
+ @Override
+ protected BackendType create (Object o) {
+ if (o == null)
+ return VoidType.INSTANCE;
+
+ BackendType bestMatch = findSimpleBuiltinType (o.getClass());
+ for (BackendTypesystem ts: _inner)
+ bestMatch = bestMatch (bestMatch, ts.findType (o));
+
+ if (bestMatch != null)
+ return bestMatch;
+
+ bestMatch = _javaBeansTypesystem.findType (o);
+ if (bestMatch != null)
+ return bestMatch;
+
+ return ObjectType.INSTANCE;
+ }
+ };
- private final Cache<Class<?>, BackendType> _cache = new Cache<Class<?>, BackendType>() {
+ private final Cache<Class<?>, BackendType> _typeByClassCache = new Cache<Class<?>, BackendType>() {
@Override
protected BackendType create (Class<?> key) {
- final BackendType builtin = findSimpleBuiltinType (key);
- if (builtin != null)
- return builtin;
+ BackendType bestMatch = findSimpleBuiltinType (key);
- for (BackendTypesystem ts : _inner) {
- final BackendType result = ts.findType(key);
- if (result != null)
- return result;
- }
+ for (BackendTypesystem ts : _inner)
+ bestMatch = bestMatch (bestMatch, ts.findType(key));
+
+ if (bestMatch != null)
+ return bestMatch;
- final BackendType jbResult = _javaBeansTypesystem.findType(key);
+ final BackendType jbResult = _javaBeansTypesystem.findType (key);
if (jbResult != null)
return jbResult;
@@ -76,6 +96,20 @@ public final class CompositeTypesystem implements BackendTypesystem {
}
};
+ private BackendType bestMatch (BackendType t1, BackendType t2) {
+ if (t1 == null)
+ return t2;
+ if (t2 == null)
+ return t1;
+
+ if (t1.isAssignableFrom(t2))
+ return t2;
+ if (t2.isAssignableFrom(t1))
+ return t1;
+
+ throw new IllegalArgumentException ("no unique best match for types " + t1 + " and " + t2);
+ }
+
//TODO remove this - add "asBackendType" to frontend type instead
public Collection<BackendTypesystem> getInner () {
final Collection<BackendTypesystem> result = new ArrayList<BackendTypesystem> (_inner);
@@ -89,14 +123,11 @@ public final class CompositeTypesystem implements BackendTypesystem {
}
public BackendType findType (Object o) {
- if (o == null)
- return VoidType.INSTANCE;
-
- return findType (o.getClass());
+ return _typeByInstanceCache.get (o);
}
public BackendType findType (Class<?> cls) {
- return _cache.get (cls);
+ return _typeByClassCache.get (cls);
}
public BackendTypesystem getRootTypesystem () {
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/util/IdentityCache.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/util/IdentityCache.java
new file mode 100644
index 0000000..fab4cb4
--- /dev/null
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/util/IdentityCache.java
@@ -0,0 +1,38 @@
+/*
+Copyright (c) 2008 Arno Haase.
+All rights reserved. This program and the accompanying materials
+are made available under the terms of the Eclipse Public License v1.0
+which accompanies this distribution, and is available at
+http://www.eclipse.org/legal/epl-v10.html
+
+Contributors:
+ Arno Haase - initial API and implementation
+ */
+package org.eclipse.xtend.backend.util;
+
+import java.util.IdentityHashMap;
+import java.util.Map;
+
+
+/**
+ *
+ * @author Arno Haase (http://www.haase-consulting.com)
+ */
+public abstract class IdentityCache<K,V> {
+ private final Map<K, V> _cache = new IdentityHashMap<K, V>();
+
+ public V get (K key) {
+ if (_cache.containsKey(key))
+ return _cache.get (key);
+
+ final V result = create (key);
+ _cache.put (key, result);
+ return result;
+ }
+
+ protected abstract V create (K key);
+
+ public Map<K, V> getMap () {
+ return _cache;
+ }
+}