diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-06 23:58:17 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-06 23:58:17 +0000 |
commit | 2fa2269bd4a993217302183736e6dd0888656869 (patch) | |
tree | 0ca250451c89b441319ed9ae7862ed2696cc05e9 | |
parent | ce411305c2393b081bb9b7fa0ce507ce31e81ff7 (diff) |
Improve performance by indexing invocation chains by operation and caching Java instanceFactory (merge from 1.x)
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@833609 13f79535-47bb-0310-9956-ffa450edef68
4 files changed, 45 insertions, 16 deletions
diff --git a/branches/sca-java-1.5.2/itest/services/src/test/java/org/apache/tuscany/sca/itest/services/ServicesTestCase.java b/branches/sca-java-1.5.2/itest/services/src/test/java/org/apache/tuscany/sca/itest/services/ServicesTestCase.java index ba6cac279c..4857b751b9 100644 --- a/branches/sca-java-1.5.2/itest/services/src/test/java/org/apache/tuscany/sca/itest/services/ServicesTestCase.java +++ b/branches/sca-java-1.5.2/itest/services/src/test/java/org/apache/tuscany/sca/itest/services/ServicesTestCase.java @@ -47,6 +47,12 @@ public class ServicesTestCase { public void testAService() { AComponent a1 = domain.getService(AComponent.class, "AComponent1"); assertEquals("AComponent", a1.foo()); + + // Call twice + assertEquals("AComponent", a1.foo()); + + a1 = domain.getService(AComponent.class, "AComponent1"); + assertEquals("AComponent", a1.foo()); AComponent a2 = domain.getService(AComponent.class, "AComponent2/AComponent"); assertEquals("AComponent", a2.foo()); diff --git a/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java b/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java index ad38906e9d..cc71374296 100644 --- a/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java +++ b/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/RuntimeWireImpl.java @@ -21,7 +21,9 @@ package org.apache.tuscany.sca.core.assembly; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.IdentityHashMap; import java.util.List; +import java.util.Map; import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.Component; @@ -79,6 +81,10 @@ public class RuntimeWireImpl implements RuntimeWire { private List<InvocationChain> chains; private InvocationChain bindingInvocationChain; + // Cache + private transient final Map<Operation, InvocationChain> invocationChainMap = + new IdentityHashMap<Operation, InvocationChain>(); + /** * @param source @@ -129,20 +135,30 @@ public class RuntimeWireImpl implements RuntimeWire { } public InvocationChain getInvocationChain(Operation operation) { - for (InvocationChain chain : getInvocationChains()) { - Operation op = null; - if (wireSource.getContract() != null) { - // Reference chain - op = chain.getSourceOperation(); + synchronized (invocationChainMap) { + InvocationChain cached = invocationChainMap.get(operation); + if (cached == null) { + for (InvocationChain chain : getInvocationChains()) { + Operation op = null; + if (wireSource.getContract() != null) { + // Reference chain + op = chain.getSourceOperation(); + } else { + // Service chain + op = chain.getTargetOperation(); + } + if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + invocationChainMap.put(operation, chain); + return chain; + } + } + invocationChainMap.put(operation, null); + return null; + } else { - // Service chain - op = chain.getTargetOperation(); - } - if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) { - return chain; + return cached; } } - return null; } public Object invoke(Message msg) throws InvocationTargetException { diff --git a/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java b/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java index 80650b2804..4c6abc726c 100644 --- a/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java +++ b/branches/sca-java-1.5.2/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java @@ -23,7 +23,7 @@ import java.io.Serializable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -82,7 +82,7 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { protected boolean fixedWire = true; - protected transient Map<Method, InvocationChain> chains = new HashMap<Method, InvocationChain>(); + protected transient Map<Method, InvocationChain> chains = new IdentityHashMap<Method, InvocationChain>(); public JDKInvocationHandler(MessageFactory messageFactory, Class<?> businessInterface, RuntimeWire wire) { this.messageFactory = messageFactory; @@ -288,8 +288,11 @@ public class JDKInvocationHandler implements InvocationHandler, Serializable { } protected synchronized InvocationChain getInvocationChain(Method method, RuntimeWire wire) { - if (fixedWire && chains.containsKey(method)) { - return chains.get(method); + if (fixedWire) { + InvocationChain chain = chains.get(method); + if (chain != null) { + return chain; + } } InvocationChain found = null; for (InvocationChain chain : wire.getInvocationChains()) { diff --git a/branches/sca-java-1.5.2/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java b/branches/sca-java-1.5.2/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java index 29e02da648..65bff6efdb 100644 --- a/branches/sca-java-1.5.2/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java +++ b/branches/sca-java-1.5.2/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java @@ -51,6 +51,7 @@ import org.apache.tuscany.sca.core.invocation.WireObjectFactory; import org.apache.tuscany.sca.core.scope.ScopeContainer; import org.apache.tuscany.sca.core.scope.TargetResolutionException; import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactory; import org.apache.tuscany.sca.implementation.java.impl.JavaConstructorImpl; import org.apache.tuscany.sca.implementation.java.impl.JavaElementImpl; import org.apache.tuscany.sca.implementation.java.impl.JavaResourceImpl; @@ -78,6 +79,7 @@ public class JavaComponentContextProvider { private RuntimeComponent component; private JavaInstanceFactoryProvider<?> instanceFactoryProvider; private ProxyFactory proxyFactory; + private InstanceFactory instanceFactory; public JavaComponentContextProvider(RuntimeComponent component, JavaInstanceFactoryProvider configuration, @@ -99,7 +101,7 @@ public class JavaComponentContextProvider { } InstanceWrapper<?> createInstanceWrapper() throws ObjectCreationException { - return instanceFactoryProvider.createFactory().newInstance(); + return instanceFactory.newInstance(); } void configureProperties(List<ComponentProperty> definedProperties) { @@ -272,6 +274,8 @@ public class JavaComponentContextProvider { ccImpl.setPropertyValueFactory(propertyValueFactory); //setUpPolicyHandlers(); + this.instanceFactory = instanceFactoryProvider.createFactory(); + } void addResourceFactory(String name, ObjectFactory<?> factory) { |