Skip to content

Commit c2d26d8

Browse files
committed
Fix bug in dispatch chain element removal (adopt/insert node at with right parent)
Also extract code to reduce duplication. Signed-off-by: Stefan Marr <[email protected]>
1 parent e4983d5 commit c2d26d8

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/trufflesom/interpreter/nodes/MessageSendNode.java

+36-30
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,8 @@ public Object doPreEvaluated(final VirtualFrame frame,
149149
return cache.doPreEvaluated(frame, arguments);
150150
}
151151
} catch (InvalidAssumptionException e) {
152-
// Remove invalid node from dispatch chain
153152
CompilerDirectives.transferToInterpreterAndInvalidate();
154-
if (cache.getParent() == this) {
155-
if (cache.next == null) {
156-
dispatchCache = null;
157-
cache = null;
158-
} else {
159-
dispatchCache = insert(cache.next);
160-
cache = cache.next;
161-
}
162-
} else {
163-
GuardedDispatchNode parent = (GuardedDispatchNode) cache.getParent();
164-
if (cache.next == null) {
165-
parent.next = null;
166-
cache = null;
167-
} else {
168-
parent.next = insert(cache.next);
169-
cache = cache.next;
170-
}
171-
}
153+
cache = removeInvalidEntryAndReturnNext(cache);
172154
continue;
173155
}
174156
cache = cache.next;
@@ -179,11 +161,43 @@ public Object doPreEvaluated(final VirtualFrame frame,
179161
return specialize(arguments).doPreEvaluated(frame, arguments);
180162
}
181163

164+
private GuardedDispatchNode removeInvalidEntryAndReturnNext(
165+
final GuardedDispatchNode cache) {
166+
if (cache.getParent() == this) {
167+
if (cache.next == null) {
168+
dispatchCache = null;
169+
return null;
170+
} else {
171+
dispatchCache = insert(cache.next);
172+
return cache.next;
173+
}
174+
} else {
175+
GuardedDispatchNode parent = (GuardedDispatchNode) cache.getParent();
176+
if (cache.next == null) {
177+
parent.next = null;
178+
return null;
179+
} else {
180+
parent.next = parent.insertHere(cache.next);
181+
return cache.next;
182+
}
183+
}
184+
}
185+
182186
@Override
183187
public String toString() {
184188
return "GMsgSend(" + selector.getString() + ")";
185189
}
186190

191+
private int getCacheSize(GuardedDispatchNode cache) {
192+
int cacheSize = 0;
193+
while (cache != null) {
194+
cache = cache.next;
195+
cacheSize += 1;
196+
}
197+
198+
return cacheSize;
199+
}
200+
187201
@Override
188202
public NodeCost getCost() {
189203
if (!triedEager) {
@@ -196,11 +210,7 @@ public NodeCost getCost() {
196210
return NodeCost.MEGAMORPHIC;
197211
}
198212

199-
int cacheSize = 0;
200-
while (cache != null) {
201-
cache = cache.next;
202-
cacheSize += 1;
203-
}
213+
int cacheSize = getCacheSize(cache);
204214

205215
if (cacheSize == 0) {
206216
return NodeCost.UNINITIALIZED;
@@ -223,12 +233,8 @@ private PreevaluatedExpression specialize(final Object[] arguments) {
223233
}
224234

225235
final GuardedDispatchNode first = dispatchCache;
226-
GuardedDispatchNode cache = first;
227-
int cacheSize = 0;
228-
while (cache != null) {
229-
cache = cache.next;
230-
cacheSize += 1;
231-
}
236+
237+
int cacheSize = getCacheSize(first);
232238

233239
Object rcvr = arguments[0];
234240
assert rcvr != null;

0 commit comments

Comments
 (0)