Browse Source

Introduced function cache in order to improve statement interpreter performance..

terfloth@itemis.de 12 years ago
parent
commit
0abf69cd70

+ 2 - 1
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/impl/ExecutionFlowInterpreter.xtend

@@ -208,7 +208,7 @@ class ExecutionFlowInterpreter extends AbstractExecutionFacade implements IExecu
 		} 
 	}
 	
-	override exit(){
+	override exit() {
 		for(step : flow.exitSequence.steps){
 			step.execute
 		} 
@@ -318,6 +318,7 @@ class ExecutionFlowInterpreter extends AbstractExecutionFacade implements IExecu
 		null
 	}
 	
+	
 	override getExecutionContext() {
 		return executionContext;
 	}

+ 12 - 1
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/AbstractStatementInterpreter.java

@@ -21,6 +21,7 @@ import org.yakindu.sct.model.stext.stext.OperationDefinition;
 /**
  * 
  * @author andreas muelder - Initial contribution and API
+ * @author axel terfloth - added caching of function lookups
  * 
  */
 public abstract class AbstractStatementInterpreter extends CoreFunction
@@ -43,6 +44,9 @@ public abstract class AbstractStatementInterpreter extends CoreFunction
 		assignFunctionMap.put("orAssign", "|");
 	}
 
+	
+	protected Map<Signature, Function> functionMap = new HashMap<Signature, Function>();
+	
 	public Object evaluate(String name, Object... params) {
 		Function lookup = lookup(name, params);
 		return lookup.execute(params);
@@ -50,7 +54,14 @@ public abstract class AbstractStatementInterpreter extends CoreFunction
 	}
 
 	public Function lookup(String name, Object... params) {
-		return super.lookup(getClass(), name, params);
+		
+		Signature sig = new Signature(name, toParamTypes(params));
+		Function f = functionMap.get(sig);
+		if ( f == null ) {
+			f =  super.lookup(getClass(), name, sig.getParamTypes());
+			functionMap.put(sig, f);
+		}
+		return f;
 	}
 
 	public void setOperationCallbacks(List<Object> callbacks) {

+ 6 - 1
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/Function.java

@@ -138,13 +138,18 @@ public class Function {
 
 	public Function lookup(Class<?> functionClass, String name,
 			Object... params) {
+		Class<?>[] paramTypes = toParamTypes(params);
+		return lookup(functionClass, name, paramTypes);
+	}
+
+	protected Class<?>[] toParamTypes(Object... params) {
 		Class<?>[] paramTypes = new Class<?>[params.length];
 		for (int i = 0; i < params.length; i++) {
 			if (params[i] == null)
 				continue;
 			paramTypes[i] = params[i].getClass();
 		}
-		return lookup(functionClass, name, paramTypes);
+		return paramTypes;
 	}
 
 	protected static Function createFunction(Class<?> functionClass,

+ 73 - 0
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/Signature.java

@@ -0,0 +1,73 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU and others.
+ * 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.model.sexec.interpreter.stext;
+
+import java.util.Arrays;
+
+
+/**
+ * This class represents a function signature consisting of the function name and the parameter types.
+ * Instances are used as keys for function mappings.
+ * 
+ * @author axel terfloth 
+ */
+public class Signature {
+	
+	protected String name;
+	protected Class<?>[] paramTypes;
+
+	public Signature(String name, Class<?>[] paramTypes) {
+		super();
+		this.name = name;
+		this.paramTypes = paramTypes;
+	}
+
+	
+	public String getName() {
+		return name;
+	}
+
+	public Class<?>[] getParamTypes() {
+		return paramTypes;
+	}
+
+	
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((name == null) ? 0 : name.hashCode());
+		result = prime * result + Arrays.hashCode(paramTypes);
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		Signature other = (Signature) obj;
+		if (name == null) {
+			if (other.name != null)
+				return false;
+		} else if (!name.equals(other.name))
+			return false;
+		if (!Arrays.equals(paramTypes, other.paramTypes))
+			return false;
+		return true;
+	}
+	
+	
+
+}

+ 10 - 2
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/StextStatementInterpreter.xtend

@@ -223,16 +223,24 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 	def dispatch execute(FeatureCall call){
 		if (call.operationCall) context.call(call.feature.name)
 		else {
-			var variableRef = context.getVariable(call.feature.fullyQualifiedName.toString)
+			var fqn = call.feature.fqn
+			var variableRef = context.getVariable(fqn)
 			if(variableRef != null){
 				return variableRef.getValue
 			}
-			return context.isEventRaised(call.feature.fullyQualifiedName.toString)
+			return context.isEventRaised(fqn)
 		}
 		
 		null
 	}
 
+//    def String create qn : obj.fullyQualifiedName.toString fqn(EObject obj) {
+//    }
+
+    def String fqn(EObject obj) {
+    	obj.fullyQualifiedName.toString
+    } 
+
 	def dispatch execute(ParenthesizedExpression e){
 		e.expression.execute()
 	}