Skip to content
Snippets Groups Projects
Commit 829b8c6e authored by Florian Angerer's avatar Florian Angerer
Browse files

Synchronizing FD cache.

parent 2b05e288
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,8 @@ package com.oracle.truffle.r.runtime.env;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
......@@ -114,6 +116,8 @@ import com.oracle.truffle.r.runtime.env.frame.REnvTruffleFrameAccess;
public abstract class REnvironment extends RAttributeStorage {
public static final class ContextStateImpl implements RContext.ContextState {
private static Map<RStringVector, WeakReference<FrameDescriptor>> frameDescriptorCache;
private final MaterializedFrame globalFrame;
@CompilationFinal private Base baseEnv;
@CompilationFinal private REnvironment namespaceRegistry;
......@@ -179,6 +183,33 @@ public abstract class REnvironment extends RAttributeStorage {
this.namespaceRegistry = newNamespaceRegistry;
this.searchPath = newSearchPath;
}
public static FrameDescriptor getFrameDescriptorFromList(RList list) {
CompilerAsserts.neverPartOfCompilation();
// create lazily
if (frameDescriptorCache == null) {
frameDescriptorCache = Collections.synchronizedMap(new WeakHashMap<>());
}
RStringVector names = list.getNames();
WeakReference<FrameDescriptor> weakReference = frameDescriptorCache.get(names);
FrameDescriptor fd = weakReference != null ? weakReference.get() : null;
if (fd == null) {
// ensure that string vector is not modified anymore
names.makeSharedPermanent();
fd = RRuntime.createFrameDescriptorWithMetaData("<new-cachedfd-env>");
for (int i = 0; i < list.getLength(); i++) {
FrameSlotKind valueSlotKind = RRuntime.getSlotKind(list.getDataAt(i));
FrameSlotChangeMonitor.findOrAddFrameSlot(fd, names.getDataAt(i), valueSlotKind);
}
frameDescriptorCache.put(names, new WeakReference<>(fd));
}
return fd;
}
}
public static class PutException extends RErrorException {
......@@ -670,7 +701,7 @@ public abstract class REnvironment extends RAttributeStorage {
REnvironment result;
RStringVector names = list.getNames();
if (list.getLength() >= LARGE_LIST_THRESHOLD) {
FrameDescriptor cachedFd = getFrameDescriptorFromList(list);
FrameDescriptor cachedFd = ContextStateImpl.getFrameDescriptorFromList(list);
result = RDataFactory.createNewEnv(cachedFd, null);
} else {
result = RDataFactory.createNewEnv(null);
......@@ -686,33 +717,6 @@ public abstract class REnvironment extends RAttributeStorage {
return result;
}
private static WeakHashMap<RStringVector, WeakReference<FrameDescriptor>> frameDescriptorCache;
public static FrameDescriptor getFrameDescriptorFromList(RList list) {
// create lazily
if (frameDescriptorCache == null) {
frameDescriptorCache = new WeakHashMap<>();
}
RStringVector names = list.getNames();
WeakReference<FrameDescriptor> weakReference = frameDescriptorCache.get(names);
FrameDescriptor fd = weakReference != null ? weakReference.get() : null;
if (fd == null) {
// ensure that string vector is not modified anymore
names.makeSharedPermanent();
fd = RRuntime.createFrameDescriptorWithMetaData("<new-cachedfd-env>");
for (int i = 0; i < list.getLength(); i++) {
FrameSlotKind valueSlotKind = RRuntime.getSlotKind(list.getDataAt(i));
FrameSlotChangeMonitor.findOrAddFrameSlot(fd, names.getDataAt(i), valueSlotKind);
}
frameDescriptorCache.put(names, new WeakReference<>(fd));
}
return fd;
}
// END of static methods
public static final String NAMESPACE_KEY = ".__NAMESPACE__.";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment