Skip to content

Commit 111de38

Browse files
committed
Avoid virtual calls
1 parent 7257ccf commit 111de38

File tree

6 files changed

+91
-69
lines changed

6 files changed

+91
-69
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
import com.oracle.graal.python.builtins.objects.PNone;
5454
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
5555
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
56-
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
56+
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
57+
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
5758
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5859
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ToByteArrayNodeGen;
5960
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
@@ -144,7 +145,13 @@ Object run(String str) {
144145
}
145146

146147
@Specialization
147-
Object run(PIBytesLike str) {
148+
Object run(PBytes str) {
149+
byte[] bytes = doBytes(getToByteArrayNode().execute(str.getSequenceStorage()));
150+
return factory().createByteArray(bytes);
151+
}
152+
153+
@Specialization
154+
Object run(PByteArray str) {
148155
byte[] bytes = doBytes(getToByteArrayNode().execute(str.getSequenceStorage()));
149156
return factory().createByteArray(bytes);
150157
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4343
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins.BytesLikeNoGeneralizationNode;
4444
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
45+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4546
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4647
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes;
4748
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
@@ -100,8 +101,9 @@ protected Object doGeneric(Object self, Object idx) {
100101
public abstract static class IAddNode extends PythonBinaryBuiltinNode {
101102
@Specialization
102103
public PByteArray add(PByteArray self, PIBytesLike other,
104+
@Cached SequenceNodes.GetSequenceStorageNode getStorage,
103105
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode) {
104-
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), other.getSequenceStorage());
106+
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), getStorage.execute(other));
105107
updateSequenceStorage(self, res);
106108
return self;
107109
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,10 @@ public abstract static class EqNode extends PythonBinaryBuiltinNode {
181181
@Child private SequenceStorageNodes.CmpNode eqNode;
182182

183183
@Specialization
184-
boolean eq(VirtualFrame frame, PIBytesLike self, PIBytesLike other) {
185-
return getEqNode().execute(frame, self.getSequenceStorage(), other.getSequenceStorage());
184+
boolean eq(VirtualFrame frame, PIBytesLike self, PIBytesLike other,
185+
@Cached GetSequenceStorageNode getSelfStorage,
186+
@Cached GetSequenceStorageNode getOtherStorage) {
187+
return getEqNode().execute(frame, getSelfStorage.execute(self), getOtherStorage.execute(other));
186188
}
187189

188190
@SuppressWarnings("unused")
@@ -209,8 +211,10 @@ public abstract static class NeNode extends PythonBinaryBuiltinNode {
209211
@Child SequenceStorageNodes.CmpNode eqNode;
210212

211213
@Specialization
212-
boolean ne(VirtualFrame frame, PIBytesLike self, PIBytesLike other) {
213-
return !getEqNode().execute(frame, self.getSequenceStorage(), other.getSequenceStorage());
214+
boolean ne(VirtualFrame frame, PIBytesLike self, PIBytesLike other,
215+
@Cached GetSequenceStorageNode getSelfStorage,
216+
@Cached GetSequenceStorageNode getOtherStorage) {
217+
return !getEqNode().execute(frame, getSelfStorage.execute(self), getOtherStorage.execute(other));
214218
}
215219

216220
@SuppressWarnings("unused")
@@ -343,8 +347,9 @@ public boolean doByte(PBytes byteArray) {
343347

344348
@Specialization
345349
boolean doLen(PIBytesLike operand,
346-
@Cached("create()") SequenceStorageNodes.LenNode lenNode) {
347-
return lenNode.execute(operand.getSequenceStorage()) != 0;
350+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
351+
@Cached GetSequenceStorageNode getSelfStorage) {
352+
return lenNode.execute(getSelfStorage.execute(operand)) != 0;
348353
}
349354

350355
@Fallback
@@ -583,8 +588,9 @@ public Object doGeneric(Object self, Object arg) {
583588
public abstract static class LenNode extends PythonUnaryBuiltinNode {
584589
@Specialization
585590
public int len(PIBytesLike self,
586-
@Cached("create()") SequenceStorageNodes.LenNode lenNode) {
587-
return lenNode.execute(self.getSequenceStorage());
591+
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
592+
@Cached GetSequenceStorageNode getSelfStorage) {
593+
return lenNode.execute(getSelfStorage.execute(self));
588594
}
589595
}
590596

@@ -603,20 +609,23 @@ private int getLength(SequenceStorage s) {
603609

604610
@Specialization
605611
boolean contains(VirtualFrame frame, PIBytesLike self, PIBytesLike other,
606-
@Cached("create()") BytesNodes.FindNode findNode) {
607-
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
612+
@Cached("create()") BytesNodes.FindNode findNode,
613+
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
614+
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
608615
}
609616

610617
@Specialization
611618
boolean contains(VirtualFrame frame, PIBytesLike self, int other,
612-
@Cached("create()") BytesNodes.FindNode findNode) {
613-
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
619+
@Cached("create()") BytesNodes.FindNode findNode,
620+
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
621+
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
614622
}
615623

616624
@Specialization
617625
boolean contains(VirtualFrame frame, PIBytesLike self, long other,
618-
@Cached("create()") BytesNodes.FindNode findNode) {
619-
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
626+
@Cached("create()") BytesNodes.FindNode findNode,
627+
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
628+
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
620629
}
621630

622631
@Specialization(guards = {"!isBytes(other)"})
@@ -936,22 +945,25 @@ abstract static class FindNode extends PythonBuiltinNode {
936945
@Specialization
937946
int find(VirtualFrame frame, PIBytesLike self, Object sub, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end,
938947
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
939-
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
940-
return find(frame, self, sub, 0, lenNode.execute(self.getSequenceStorage()), findNode);
948+
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
949+
@Cached GetSequenceStorageNode getSelfStorage) {
950+
return find(frame, self, sub, 0, lenNode.execute(getSelfStorage.execute(self)), findNode);
941951
}
942952

943953
@Specialization
944954
int find(VirtualFrame frame, PIBytesLike self, Object sub, int start, @SuppressWarnings("unused") PNone end,
945955
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
946-
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
947-
return find(frame, self, sub, start, lenNode.execute(self.getSequenceStorage()), findNode);
956+
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
957+
@Cached GetSequenceStorageNode getSelfStorage) {
958+
return find(frame, self, sub, start, lenNode.execute(getSelfStorage.execute(self)), findNode);
948959
}
949960

950961
@Specialization
951962
int find(VirtualFrame frame, PIBytesLike self, Object sub, Object start, @SuppressWarnings("unused") PNone end,
952963
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
953-
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
954-
return find(frame, self, sub, start, lenNode.execute(self.getSequenceStorage()), findNode);
964+
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
965+
@Cached GetSequenceStorageNode getSelfStorage) {
966+
return find(frame, self, sub, start, lenNode.execute(getSelfStorage.execute(self)), findNode);
955967
}
956968

957969
@Specialization

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,11 @@ public void toNative(
402402
abstract static class ToNativeArrayNode extends Node {
403403
public abstract Object execute(PySequenceArrayWrapper object);
404404

405-
@Specialization(guards = "isPSequence(lib.getDelegate(object))", limit = "1")
405+
@Specialization(guards = "isPSequence(lib.getDelegate(object))")
406406
Object doPSequence(PySequenceArrayWrapper object,
407407
@Cached SequenceNodes.GetSequenceStorageNode getStorage,
408-
@CachedLibrary("object") PythonNativeWrapperLibrary lib,
408+
@Cached SequenceNodes.SetSequenceStorageNode setStorage,
409+
@CachedLibrary(limit = "3") PythonNativeWrapperLibrary lib,
409410
@Exclusive @Cached ToNativeStorageNode toNativeStorageNode) {
410411
PSequence sequence = (PSequence) lib.getDelegate(object);
411412
NativeSequenceStorage nativeStorage = toNativeStorageNode.execute(getStorage.execute(sequence));
@@ -414,13 +415,13 @@ Object doPSequence(PySequenceArrayWrapper object,
414415
throw new IllegalStateException("could not allocate native storage");
415416
}
416417
// switch to native storage
417-
sequence.setSequenceStorage(nativeStorage);
418+
setStorage.execute(sequence, nativeStorage);
418419
return nativeStorage.getPtr();
419420
}
420421

421-
@Specialization(guards = "!isPSequence(lib.getDelegate(object))", limit = "1")
422+
@Specialization(guards = "!isPSequence(lib.getDelegate(object))")
422423
Object doGeneric(PySequenceArrayWrapper object,
423-
@SuppressWarnings("unused") @CachedLibrary("object") PythonNativeWrapperLibrary lib,
424+
@SuppressWarnings("unused") @CachedLibrary(limit = "3") PythonNativeWrapperLibrary lib,
424425
@Exclusive @Cached PCallCapiFunction callNativeHandleForArrayNode) {
425426
// TODO correct element size
426427
return callNativeHandleForArrayNode.call(FUN_NATIVE_HANDLE_FOR_ARRAY, object, 8L);
@@ -438,8 +439,9 @@ abstract static class ToNativeStorageNode extends Node {
438439

439440
@Specialization(guards = "!isNative(s)")
440441
NativeSequenceStorage doManaged(SequenceStorage s,
441-
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode) {
442-
return storageToNativeNode.execute(s.getInternalArrayObject());
442+
@Shared("storageToNativeNode") @Cached SequenceStorageNodes.StorageToNativeNode storageToNativeNode,
443+
@Cached SequenceStorageNodes.GetInternalArrayNode getInternalArrayNode) {
444+
return storageToNativeNode.execute(getInternalArrayNode.execute(s));
443445
}
444446

445447
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingCollectionNodes.java

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodesFactory.LenNodeGen;
4545
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodesFactory.SetDictStorageNodeGen;
4646
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodesFactory.SetItemNodeGen;
47+
import com.oracle.graal.python.builtins.objects.dict.PDict;
48+
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
49+
import com.oracle.graal.python.builtins.objects.set.PBaseSet;
4750
import com.oracle.graal.python.nodes.PGuards;
4851
import com.oracle.graal.python.nodes.PNodeWithContext;
4952
import com.oracle.truffle.api.dsl.Cached;
@@ -61,17 +64,11 @@ public abstract class HashingCollectionNodes {
6164
public abstract static class LenNode extends PNodeWithContext {
6265
public abstract int execute(PHashingCollection c);
6366

64-
@Specialization(limit = "4", guards = {"c.getClass() == cachedClass"})
65-
static int getLenCached(PHashingCollection c,
66-
@CachedLibrary("c.getDictStorage()") HashingStorageLibrary lib,
67-
@Cached("c.getClass()") Class<? extends PHashingCollection> cachedClass) {
68-
return lib.length(cachedClass.cast(c).getDictStorage());
69-
}
70-
71-
@Specialization(replaces = "getLenCached", limit = "1")
72-
static int getLenGeneric(PHashingCollection c,
73-
@CachedLibrary("c.getDictStorage()") HashingStorageLibrary lib) {
74-
return lib.length(c.getDictStorage());
67+
@Specialization(limit = "4")
68+
static int getLen(PHashingCollection c,
69+
@Cached GetDictStorageNode getStorage,
70+
@CachedLibrary("getStorage.execute(c)") HashingStorageLibrary lib) {
71+
return lib.length(getStorage.execute(c));
7572
}
7673

7774
public static LenNode create() {
@@ -83,23 +80,15 @@ public static LenNode create() {
8380
public abstract static class SetItemNode extends PNodeWithContext {
8481
public abstract void execute(VirtualFrame frame, PHashingCollection c, Object key, Object value);
8582

86-
@Specialization(limit = "4", guards = {"c.getClass() == cachedClass"})
87-
void doSetItemCached(VirtualFrame frame, PHashingCollection c, Object key, Object value,
88-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
89-
@CachedLibrary("c.getDictStorage()") HashingStorageLibrary lib,
90-
@Cached("c.getClass()") Class<? extends PHashingCollection> cachedClass) {
91-
HashingStorage storage = cachedClass.cast(c).getDictStorage();
92-
storage = lib.setItemWithFrame(storage, key, value, hasFrame, frame);
93-
cachedClass.cast(c).setDictStorage(storage);
94-
}
95-
96-
@Specialization(replaces = "doSetItemCached", limit = "1")
97-
void doSetItemGeneric(VirtualFrame frame, PHashingCollection c, Object key, Object value,
83+
@Specialization(limit = "4")
84+
void doSetItem(VirtualFrame frame, PHashingCollection c, Object key, Object value,
9885
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
86+
@Cached GetDictStorageNode getStorage,
87+
@Cached SetDictStorageNode setStorage,
9988
@CachedLibrary("c.getDictStorage()") HashingStorageLibrary lib) {
100-
HashingStorage storage = c.getDictStorage();
89+
HashingStorage storage = getStorage.execute(c);
10190
storage = lib.setItemWithFrame(storage, key, value, hasFrame, frame);
102-
c.setDictStorage(storage);
91+
setStorage.execute(c, storage);
10392
}
10493

10594
public static SetItemNode create() {
@@ -113,14 +102,18 @@ public abstract static class GetDictStorageNode extends PNodeWithContext {
113102

114103
public abstract HashingStorage execute(PHashingCollection c);
115104

116-
@Specialization(limit = "4", guards = {"c.getClass() == cachedClass"})
117-
HashingStorage getStorageCached(PHashingCollection c,
118-
@Cached("c.getClass()") Class<? extends PHashingCollection> cachedClass) {
119-
return cachedClass.cast(c).getDictStorage();
105+
@Specialization
106+
static HashingStorage get(PBaseSet c) {
107+
return c.getDictStorage();
108+
}
109+
110+
@Specialization
111+
static HashingStorage get(PDict c) {
112+
return c.getDictStorage();
120113
}
121114

122-
@Specialization(replaces = "getStorageCached")
123-
static HashingStorage getStorageGeneric(PHashingCollection c) {
115+
@Specialization
116+
static HashingStorage get(PMappingproxy c) {
124117
return c.getDictStorage();
125118
}
126119

@@ -139,14 +132,18 @@ public abstract static class SetDictStorageNode extends PNodeWithContext {
139132

140133
public abstract void execute(PHashingCollection c, HashingStorage storage);
141134

142-
@Specialization(limit = "4", guards = {"c.getClass() == cachedClass"})
143-
void setStorageCached(PHashingCollection c, HashingStorage storage,
144-
@Cached("c.getClass()") Class<? extends PHashingCollection> cachedClass) {
145-
cachedClass.cast(c).setDictStorage(storage);
135+
@Specialization
136+
static void set(PBaseSet c, HashingStorage storage) {
137+
c.setDictStorage(storage);
138+
}
139+
140+
@Specialization
141+
static void set(PDict c, HashingStorage storage) {
142+
c.setDictStorage(storage);
146143
}
147144

148-
@Specialization(replaces = "setStorageCached")
149-
static void getStorageGeneric(PHashingCollection c, HashingStorage storage) {
145+
@Specialization
146+
static void set(PMappingproxy c, HashingStorage storage) {
150147
c.setDictStorage(storage);
151148
}
152149

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.graal.python.builtins.PythonBuiltins;
4444
import com.oracle.graal.python.builtins.objects.PNone;
4545
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
46+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4647
import com.oracle.graal.python.builtins.objects.list.PList;
4748
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4849
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
@@ -140,9 +141,10 @@ public Object args(PBaseException self, @SuppressWarnings("unused") PNone none,
140141

141142
@Specialization(guards = "!isNoValue(value)")
142143
public Object args(VirtualFrame frame, PBaseException self, Object value,
143-
@Cached("create()") CastToListNode castToList) {
144+
@Cached CastToListNode castToList,
145+
@Cached SequenceStorageNodes.CopyInternalArrayNode copy) {
144146
PList list = castToList.execute(frame, value);
145-
self.setArgs(factory().createTuple(list.getSequenceStorage().getCopyOfInternalArray()));
147+
self.setArgs(factory().createTuple(copy.execute(list.getSequenceStorage())));
146148
return PNone.NONE;
147149
}
148150

0 commit comments

Comments
 (0)