Commit c7632b6f authored by Lukáš Marek's avatar Lukáš Marek
Browse files

Reintroduced static context data caching

parent 4ab77ace
......@@ -4,9 +4,11 @@ import java.util.HashMap;
import java.util.Map;
import ch.usi.dag.disl.snippet.Shadow;
import ch.usi.dag.disl.staticcontext.cache.CacheableStaticContext;
import ch.usi.dag.disl.staticcontext.cache.StaticContextCache;
public abstract class AbstractStaticContext implements StaticContext {
public abstract class AbstractStaticContext implements StaticContext,
CacheableStaticContext {
protected Shadow staticContextData;
......@@ -19,6 +21,10 @@ public abstract class AbstractStaticContext implements StaticContext {
retValCache.put(methodName, new StaticContextCache(keyCacheClass));
}
public StaticContextCache getRetValCache(String method) {
return retValCache.get(method);
}
public void staticContextData(Shadow sa) {
staticContextData = sa;
......
package ch.usi.dag.disl.staticcontext.cache;
public interface CacheableStaticContext {
public StaticContextCache getRetValCache(String method);
}
......@@ -15,6 +15,8 @@ import ch.usi.dag.disl.snippet.ProcInvocation;
import ch.usi.dag.disl.snippet.Shadow;
import ch.usi.dag.disl.snippet.Snippet;
import ch.usi.dag.disl.staticcontext.StaticContext;
import ch.usi.dag.disl.staticcontext.cache.CacheableStaticContext;
import ch.usi.dag.disl.staticcontext.cache.StaticContextCache;
import ch.usi.dag.disl.util.Constants;
public class SCGenerator {
......@@ -153,18 +155,14 @@ public class SCGenerator {
for (StaticContextMethod stConMth : stConMethods) {
// get static context instance
StaticContext scInst =
StConResolver.getInstance().getStaticContextInstance(
StaticContext scInst = StConResolver.getInstance()
.getStaticContextInstance(
stConMth.getReferencedClass());
// populate static context instance with data
scInst.staticContextData(shadow);
// resolve static context data
Object result = getStaticContextData(stConMth.getMethod(),
scInst, shadow);
// get static data by invoking static context method
Object result = invokeStaticContextMethod(
stConMth.getMethod(), scInst);
// store the result
setSI(shadow, stConMth.getId(), result);
}
......@@ -172,63 +170,59 @@ public class SCGenerator {
}
}
private Object invokeStaticContextMethod(Method staticContextMethod,
Object methodObject) throws StaticContextGenException {
// resolves static context data - uses static context data caching
private Object getStaticContextData(Method method, StaticContext scInst,
Shadow shadow) throws StaticContextGenException,
ReflectionException {
// NOTE: default cache
// some default cache is not needed because for each marked region,
// the computation is called only once
try {
// invoke static context method and return result
return staticContextMethod.invoke(methodObject);
} catch (Exception e) {
throw new StaticContextGenException(
"Invocation of static context method "
+ staticContextMethod.getName() + " failed", e);
StaticContextCache cache = null;
// cacheable static context data - try to query cache first
if(scInst instanceof CacheableStaticContext) {
CacheableStaticContext cacheableSC =
(CacheableStaticContext) scInst;
cache = cacheableSC.getRetValCache(method.getName());
// static context has cache for this method
if(cache != null) {
Object result = cache.getCachedResult(shadow);
// cache is valid - return cache hit
if(result != null) {
return result;
}
}
}
}
// TODO ! static context - introduce caching
/*
// NOTE: default cache
// some default cache is not needed because for each marked region,
// the computation is called only once
// if cache wasn't hit...
try {
// resolve specific method cache
StaticContextCache cache = retValCache.get(usingMethod.getName());
// populate static context instance with data
scInst.staticContextData(shadow);
// resolve cached data
if (cache != null) {
// get static data by invoking static context method
Object result = method.invoke(scInst);
Object result = cache.getCachedResult(sa);
// cache result if applicable
if (cache != null) {
cache.cacheResult(shadow, result);
}
// return cache hit
if (result != null) {
return result;
}
}
// if cache wasn't hit...
try {
// ... invoke static context method
Object result = usingMethod.invoke(this);
if (cache != null) {
// ... cache result
cache.cacheResult(sa, result);
} catch (Exception e) {
throw new StaticContextGenException(
"Invocation of static context method " + method.getName()
+ " failed", e);
}
// ... return result
return result;
} catch (Exception e) {
throw new StaticContextGenException(
"Invocation of static context method "
+ usingMethod.getName() + " failed", e);
}
*/
private void setSI(Shadow shadow, String methodID, Object value) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment