Skip to content

Commit e5e0370

Browse files
committed
Use inline cache for handle cache.
1 parent 7454a6b commit e5e0370

File tree

1 file changed

+36
-9
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext

1 file changed

+36
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/HandleCache.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.oracle.graal.python.builtins.objects.cext.HandleCacheFactory.HandleCacheMRFactory.GetOrInsertNodeGen;
44
import com.oracle.truffle.api.CompilerDirectives;
55
import com.oracle.truffle.api.dsl.Cached;
6+
import com.oracle.truffle.api.dsl.ImportStatic;
67
import com.oracle.truffle.api.dsl.Specialization;
78
import com.oracle.truffle.api.interop.ArityException;
89
import com.oracle.truffle.api.interop.ForeignAccess;
@@ -12,12 +13,13 @@
1213
import com.oracle.truffle.api.interop.TruffleObject;
1314
import com.oracle.truffle.api.interop.UnsupportedMessageException;
1415
import com.oracle.truffle.api.interop.UnsupportedTypeException;
16+
import com.oracle.truffle.api.nodes.ControlFlowException;
1517
import com.oracle.truffle.api.nodes.ExplodeLoop;
1618
import com.oracle.truffle.api.nodes.Node;
1719
import com.oracle.truffle.api.profiles.BranchProfile;
1820

1921
public final class HandleCache implements TruffleObject {
20-
private static final int CACHE_SIZE = 10;
22+
public static final int CACHE_SIZE = 10;
2123

2224
final long[] keys;
2325
final Object[] values;
@@ -66,32 +68,57 @@ Object access(HandleCache receiver, Object[] args) {
6668

6769
}
6870

71+
static class InvalidCacheEntryException extends ControlFlowException {
72+
private static final long serialVersionUID = 1L;
73+
public static final InvalidCacheEntryException INSTANCE = new InvalidCacheEntryException();
74+
}
75+
76+
@ImportStatic(HandleCache.class)
6977
abstract static class GetOrInsertNode extends Node {
7078
@Child private Node executeNode;
7179

7280
private final BranchProfile errorProfile = BranchProfile.create();
7381

7482
public abstract Object execute(HandleCache cache, long handle);
7583

76-
@ExplodeLoop
77-
@Specialization(guards = {"cache.len() == cachedLen", "cache.getPtrToResolveHandle() == ptrToResolveHandle"})
78-
Object doIt(HandleCache cache, long handle,
84+
@Specialization(limit = "CACHE_SIZE", guards = {"cache.len() == cachedLen",
85+
"handle == cachedHandle"}, rewriteOn = InvalidCacheEntryException.class)
86+
Object doCached(HandleCache cache, @SuppressWarnings("unused") long handle,
87+
@Cached("handle") long cachedHandle,
88+
@Cached("cache.len()") @SuppressWarnings("unused") int cachedLen,
89+
@Cached("cache.getPtrToResolveHandle()") @SuppressWarnings("unused") TruffleObject ptrToResolveHandle,
90+
@Cached("lookupPosition(cache, handle, cachedLen, ptrToResolveHandle)") int cachedPosition) throws InvalidCacheEntryException {
91+
if (cache.keys[cachedPosition] == cachedHandle) {
92+
return cache.values[cachedPosition];
93+
}
94+
throw InvalidCacheEntryException.INSTANCE;
95+
}
96+
97+
@Specialization(guards = {"cache.len() == cachedLen"}, replaces = "doCached")
98+
Object doFullLookup(HandleCache cache, long handle,
7999
@Cached("cache.len()") int cachedLen,
80100
@Cached("cache.getPtrToResolveHandle()") TruffleObject ptrToResolveHandle) {
101+
int pos = lookupPosition(cache, handle, cachedLen, ptrToResolveHandle);
102+
return cache.values[pos];
103+
}
104+
105+
@ExplodeLoop
106+
protected int lookupPosition(HandleCache cache, long handle, int cachedLen, TruffleObject ptrToResolveHandle) {
81107
for (int i = 0; i < cachedLen; i++) {
82108
if (cache.keys[i] == handle) {
83-
return cache.values[i];
109+
return i;
84110
}
85111
}
86112

87113
try {
88114
Object resolved = ForeignAccess.sendExecute(getExecuteNode(), ptrToResolveHandle, handle);
89115

90-
cache.keys[cache.pos] = handle;
91-
cache.values[cache.pos] = resolved;
92-
cache.pos = (cache.pos + 1) % cache.len();
116+
int insertPos = cache.pos;
117+
cache.keys[insertPos] = handle;
118+
cache.values[insertPos] = resolved;
119+
cache.pos = (insertPos + 1) % cache.len();
93120

94-
return resolved;
121+
return insertPos;
95122
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
96123
errorProfile.enter();
97124
throw e.raise();

0 commit comments

Comments
 (0)