Skip to content

Commit 8002da6

Browse files
committed
Add shortcut for PBytes in PyObjectSizeNode
1 parent f3f031a commit 8002da6

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectSizeNode.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
4646

4747
import com.oracle.graal.python.builtins.objects.PNone;
48+
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
49+
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
4850
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageLen;
4951
import com.oracle.graal.python.builtins.objects.dict.PDict;
5052
import com.oracle.graal.python.builtins.objects.list.PList;
@@ -59,9 +61,11 @@
5961
import com.oracle.graal.python.nodes.PRaiseNode;
6062
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
6163
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode;
62-
import com.oracle.graal.python.nodes.object.GetClassNode;
64+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode;
65+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode.GetPythonObjectClassNode;
6366
import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
6467
import com.oracle.graal.python.runtime.exception.PException;
68+
import com.oracle.truffle.api.dsl.Bind;
6569
import com.oracle.truffle.api.dsl.Cached;
6670
import com.oracle.truffle.api.dsl.Cached.Shared;
6771
import com.oracle.truffle.api.dsl.Fallback;
@@ -94,39 +98,51 @@ static int doTruffleString(TruffleString str,
9498
return codePointLengthNode.execute(str, TS_ENCODING);
9599
}
96100

97-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
101+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
98102
static int doList(PList object,
99-
@Shared("getClass") @SuppressWarnings("unused") @Cached GetClassNode getClassNode) {
103+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
104+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode) {
100105
return object.getSequenceStorage().length();
101106
}
102107

103-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
108+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
104109
static int doTuple(PTuple object,
105-
@Shared("getClass") @SuppressWarnings("unused") @Cached GetClassNode getClassNode) {
110+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
111+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode) {
106112
return object.getSequenceStorage().length();
107113
}
108114

109-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
115+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
110116
static int doDict(PDict object,
111-
@Shared("getClass") @SuppressWarnings("unused") @Cached GetClassNode getClassNode,
117+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
118+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode,
112119
@Shared("hashingStorageLen") @Cached HashingStorageLen lenNode) {
113120
return lenNode.execute(object.getDictStorage());
114121
}
115122

116-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
123+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
117124
static int doSet(PSet object,
118-
@Shared("getClass") @SuppressWarnings("unused") @Cached GetClassNode getClassNode,
125+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
126+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode,
119127
@Shared("hashingStorageLen") @Cached HashingStorageLen lenNode) {
120128
return lenNode.execute(object.getDictStorage());
121129
}
122130

123-
@Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1")
131+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
124132
static int doPString(PString object,
125-
@Shared("getClass") @SuppressWarnings("unused") @Cached GetClassNode getClassNode,
133+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
134+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode,
126135
@Cached StringNodes.StringLenNode lenNode) {
127136
return lenNode.execute(object);
128137
}
129138

139+
@Specialization(guards = "cannotBeOverridden(object, inliningTarget, getClassNode)", limit = "1")
140+
static int doPBytes(PBytes object,
141+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
142+
@Shared("getClass") @SuppressWarnings("unused") @Cached GetPythonObjectClassNode getClassNode) {
143+
return BytesBuiltins.LenNode.len(object);
144+
}
145+
130146
@Fallback
131147
static int doOthers(VirtualFrame frame, Object object,
132148
@Cached PyObjectSizeGenericNode genericNode) {
@@ -142,14 +158,15 @@ abstract static class PyObjectSizeGenericNode extends Node {
142158

143159
@Specialization(rewriteOn = UnexpectedResultException.class)
144160
static int doInt(VirtualFrame frame, Object object,
145-
@Shared("getClass") @Cached GetClassNode getClassNode,
161+
@Bind("this") Node inliningTarget,
162+
@Shared("getClass") @Cached InlinedGetClassNode getClassNode,
146163
@Shared("lookupLen") @Cached(parameters = "Len") LookupSpecialMethodSlotNode lookupLen,
147164
@Shared("callLen") @Cached CallUnaryMethodNode callLen,
148165
@Shared("index") @Cached PyNumberIndexNode indexNode,
149166
@Shared("castLossy") @Cached CastToJavaIntLossyNode castLossy,
150167
@Shared("asSize") @Cached PyNumberAsSizeNode asSizeNode,
151168
@Shared("raise") @Cached PRaiseNode raiseNode) throws UnexpectedResultException {
152-
Object lenDescr = lookupLen.execute(frame, getClassNode.execute(object), object);
169+
Object lenDescr = lookupLen.execute(frame, getClassNode.execute(inliningTarget, object), object);
153170
if (lenDescr == PNone.NO_VALUE) {
154171
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_HAS_NO_LEN, object);
155172
}
@@ -163,14 +180,15 @@ static int doInt(VirtualFrame frame, Object object,
163180

164181
@Specialization(replaces = "doInt")
165182
static int doObject(VirtualFrame frame, Object object,
166-
@Shared("getClass") @Cached GetClassNode getClassNode,
183+
@Bind("this") Node inliningTarget,
184+
@Shared("getClass") @Cached InlinedGetClassNode getClassNode,
167185
@Shared("lookupLen") @Cached(parameters = "Len") LookupSpecialMethodSlotNode lookupLen,
168186
@Shared("callLen") @Cached CallUnaryMethodNode callLen,
169187
@Shared("index") @Cached PyNumberIndexNode indexNode,
170188
@Shared("castLossy") @Cached CastToJavaIntLossyNode castLossy,
171189
@Shared("asSize") @Cached PyNumberAsSizeNode asSizeNode,
172190
@Shared("raise") @Cached PRaiseNode raiseNode) {
173-
Object lenDescr = lookupLen.execute(frame, getClassNode.execute(object), object);
191+
Object lenDescr = lookupLen.execute(frame, getClassNode.execute(inliningTarget, object), object);
174192
if (lenDescr == PNone.NO_VALUE) {
175193
throw raiseNode.raise(TypeError, ErrorMessages.OBJ_HAS_NO_LEN, object);
176194
}

0 commit comments

Comments
 (0)