Skip to content

Commit 196bc04

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 0186ef5 commit 196bc04

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

Diff for: src/trufflesom/interpreter/nodes/MessageSendNode.java

+36-30
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,8 @@ public Object doPreEvaluated(final VirtualFrame frame,
162162
return cache.doPreEvaluated(frame, arguments);
163163
}
164164
} catch (InvalidAssumptionException e) {
165-
// Remove invalid node from dispatch chain
166165
CompilerDirectives.transferToInterpreterAndInvalidate();
167-
if (cache.getParent() == this) {
168-
if (cache.next == null) {
169-
dispatchCache = null;
170-
cache = null;
171-
} else {
172-
dispatchCache = insert(cache.next);
173-
cache = cache.next;
174-
}
175-
} else {
176-
GuardedDispatchNode parent = (GuardedDispatchNode) cache.getParent();
177-
if (cache.next == null) {
178-
parent.next = null;
179-
cache = null;
180-
} else {
181-
parent.next = insert(cache.next);
182-
cache = cache.next;
183-
}
184-
}
166+
cache = removeInvalidEntryAndReturnNext(cache);
185167
continue;
186168
}
187169
cache = cache.next;
@@ -192,11 +174,43 @@ public Object doPreEvaluated(final VirtualFrame frame,
192174
return specialize(arguments).doPreEvaluated(frame, arguments);
193175
}
194176

177+
private GuardedDispatchNode removeInvalidEntryAndReturnNext(
178+
final GuardedDispatchNode cache) {
179+
if (cache.getParent() == this) {
180+
if (cache.next == null) {
181+
dispatchCache = null;
182+
return null;
183+
} else {
184+
dispatchCache = insert(cache.next);
185+
return cache.next;
186+
}
187+
} else {
188+
GuardedDispatchNode parent = (GuardedDispatchNode) cache.getParent();
189+
if (cache.next == null) {
190+
parent.next = null;
191+
return null;
192+
} else {
193+
parent.next = parent.insertHere(cache.next);
194+
return cache.next;
195+
}
196+
}
197+
}
198+
195199
@Override
196200
public String toString() {
197201
return "GMsgSend(" + selector.getString() + ")";
198202
}
199203

204+
private int getCacheSize(GuardedDispatchNode cache) {
205+
int cacheSize = 0;
206+
while (cache != null) {
207+
cache = cache.next;
208+
cacheSize += 1;
209+
}
210+
211+
return cacheSize;
212+
}
213+
200214
@Override
201215
public NodeCost getCost() {
202216
if (!triedEager) {
@@ -209,11 +223,7 @@ public NodeCost getCost() {
209223
return NodeCost.MEGAMORPHIC;
210224
}
211225

212-
int cacheSize = 0;
213-
while (cache != null) {
214-
cache = cache.next;
215-
cacheSize += 1;
216-
}
226+
int cacheSize = getCacheSize(cache);
217227

218228
if (cacheSize == 0) {
219229
return NodeCost.UNINITIALIZED;
@@ -236,12 +246,8 @@ private PreevaluatedExpression specialize(final Object[] arguments) {
236246
}
237247

238248
final GuardedDispatchNode first = dispatchCache;
239-
GuardedDispatchNode cache = first;
240-
int cacheSize = 0;
241-
while (cache != null) {
242-
cache = cache.next;
243-
cacheSize += 1;
244-
}
249+
250+
int cacheSize = getCacheSize(first);
245251

246252
Object rcvr = arguments[0];
247253
assert rcvr != null;

0 commit comments

Comments
 (0)