Skip to content

Commit 4b1d0ec

Browse files
committed
Avoid uncached POL lookup in deque.index loop
1 parent e0303f3 commit 4b1d0ec

File tree

1 file changed

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

1 file changed

+21
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/deque/DequeBuiltins.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ void appendOperation(PDeque self, Object item) {
361361
@GenerateNodeFactory
362362
public abstract static class DequeIndexNode extends PythonQuaternaryBuiltinNode {
363363

364+
@Child private PythonObjectLibrary itemLib;
365+
364366
@Specialization(guards = {"isNoValue(start)", "isNoValue(stop)"})
365-
@TruffleBoundary
366367
int doWithoutSlice(PDeque self, Object value, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone stop) {
367368
return doWithIntSlice(self, value, 0, self.getSize());
368369
}
369370

370371
@Specialization
371-
@TruffleBoundary
372372
int doWithIntSlice(PDeque self, Object value, int start, int stop) {
373373
int size = self.getSize();
374374
int normStart = normalize(start, size);
@@ -380,24 +380,23 @@ int doWithIntSlice(PDeque self, Object value, int start, int stop) {
380380
if (normStart > normStop) {
381381
normStart = normStop;
382382
}
383-
PythonObjectLibrary valueLib = PythonObjectLibrary.getFactory().getUncached(value);
384-
Iterator<Object> iterator = self.data.iterator();
385-
for (int idx = 0; iterator.hasNext() && idx < normStop; idx++) {
383+
Iterator<Object> iterator = self.iterator();
384+
for (int idx = 0; idx < normStop; idx++) {
386385
/*
387386
* Note: A 'ConcurrentModificationException' should not be possible because we
388387
* manually check for modifications during iteration.
389388
*/
390-
Object item = iterator.next();
389+
Object item = next(iterator);
391390
if (normStart <= idx) {
392-
if (PythonObjectLibrary.getUncached().equals(item, value, valueLib)) {
391+
if (ensureItemLib().equals(item, value, ensureItemLib())) {
393392
return idx;
394393
}
395394
if (startState != self.getState()) {
396-
throw PRaiseNode.raiseUncached(this, RuntimeError, ErrorMessages.DEQUE_MUTATED_DURING_ITERATION);
395+
throw raise(RuntimeError, ErrorMessages.DEQUE_MUTATED_DURING_ITERATION);
397396
}
398397
}
399398
}
400-
throw PRaiseNode.raiseUncached(this, ValueError, "%s is not in deque", value);
399+
throw raise(ValueError, "%s is not in deque", value);
401400
}
402401

403402
@Specialization
@@ -420,13 +419,26 @@ int doGeneric(VirtualFrame frame, PDeque self, Object value, Object start, Objec
420419
return doWithIntSlice(self, value, istart, istop);
421420
}
422421

422+
private PythonObjectLibrary ensureItemLib() {
423+
if (itemLib == null) {
424+
CompilerDirectives.transferToInterpreterAndInvalidate();
425+
itemLib = insert(PythonObjectLibrary.getFactory().createDispatched(3));
426+
}
427+
return itemLib;
428+
}
429+
423430
private static int normalize(int i, int size) {
424431
int res = i;
425432
if (res < 0) {
426433
res += size;
427434
}
428435
return Math.max(res, 0);
429436
}
437+
438+
@TruffleBoundary
439+
private static Object next(Iterator<?> it) {
440+
return it.next();
441+
}
430442
}
431443

432444
// deque.pop()

0 commit comments

Comments
 (0)