Skip to content

Commit 6bd361b

Browse files
committed
simplify LenNode and its users
1 parent d5822e4 commit 6bd361b

File tree

2 files changed

+5
-37
lines changed

2 files changed

+5
-37
lines changed

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import static com.oracle.graal.python.nodes.HiddenAttributes.ID_KEY;
6666
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
6767
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
68-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
6968
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
7069
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUBCLASSCHECK__;
7170
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
@@ -142,7 +141,6 @@
142141
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
143142
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
144143
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
145-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode.NoAttributeHandler;
146144
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
147145
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
148146
import com.oracle.graal.python.nodes.control.GetNextNode;
@@ -1206,30 +1204,10 @@ public Object iter(Object callable, Object sentinel) {
12061204
@Builtin(name = LEN, minNumOfPositionalArgs = 1)
12071205
@GenerateNodeFactory
12081206
public abstract static class LenNode extends PythonUnaryBuiltinNode {
1209-
1210-
private static final Supplier<NoAttributeHandler> NO_LEN = () -> new NoAttributeHandler() {
1211-
@Child private PRaiseNode raiseNode;
1212-
1213-
@Override
1214-
public Object execute(Object receiver) {
1215-
if (raiseNode == null) {
1216-
CompilerDirectives.transferToInterpreterAndInvalidate();
1217-
raiseNode = insert(PRaiseNode.create());
1218-
}
1219-
throw raiseNode.raise(TypeError, "object of type '%p' has no len()", receiver);
1220-
}
1221-
};
1222-
1223-
public abstract Object executeWith(VirtualFrame frame, Object object);
1224-
1225-
protected static LookupAndCallUnaryNode createLen() {
1226-
return LookupAndCallUnaryNode.create(__LEN__, NO_LEN);
1227-
}
1228-
1229-
@Specialization
1230-
public Object len(VirtualFrame frame, Object obj,
1231-
@Cached("createLen()") LookupAndCallUnaryNode dispatch) {
1232-
return dispatch.executeObject(frame, obj);
1207+
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
1208+
public int len(VirtualFrame frame, Object obj,
1209+
@CachedLibrary("obj") PythonObjectLibrary lib) {
1210+
return lib.lengthWithState(obj, PArguments.getThreadState(frame));
12331211
}
12341212
}
12351213

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ abstract static class LZMANode extends PythonBuiltinNode {
173173

174174
@Child private GetItemNode getItemNode;
175175
@Child private PythonObjectLibrary castToLongNode;
176-
@Child private BuiltinFunctions.LenNode lenNode;
177176
@CompilationFinal private IsBuiltinClassProfile keyErrorProfile;
178177

179178
@TruffleBoundary
@@ -195,7 +194,7 @@ protected static LZMA2Options parseLZMAOptions(int preset) {
195194

196195
// corresponds to 'parse_filter_chain_spec' in '_lzmamodule.c'
197196
protected FilterOptions[] parseFilterChainSpec(VirtualFrame frame, Object filters, PythonObjectLibrary library) {
198-
int n = len(frame, filters);
197+
int n = library.lengthWithState(filters, PArguments.getThreadState(frame));
199198
FilterOptions[] optionsChain = new FilterOptions[n];
200199
for (int i = 0; i < n; i++) {
201200
optionsChain[i] = convertLZMAFilter(frame, getItem(frame, filters, i), library);
@@ -285,14 +284,6 @@ private int asInt(VirtualFrame frame, Object obj) {
285284
return castToLongNode.asSizeWithState(obj, PArguments.getThreadState(frame));
286285
}
287286

288-
private int len(VirtualFrame frame, Object obj) {
289-
if (lenNode == null) {
290-
CompilerDirectives.transferToInterpreterAndInvalidate();
291-
lenNode = insert(BuiltinFunctionsFactory.LenNodeFactory.create());
292-
}
293-
return asInt(frame, lenNode.execute(frame, obj));
294-
}
295-
296287
protected static boolean isNoneOrNoValue(Object obj) {
297288
return PGuards.isNone(obj) || PGuards.isNoValue(obj);
298289
}
@@ -419,7 +410,6 @@ private static LZMAOutputStream createLZMAOutputStream(ByteArrayOutputStream bos
419410
abstract static class LZMADecompressorNode extends LZMANode {
420411

421412
@Child private GetItemNode getItemNode;
422-
@Child private BuiltinFunctions.LenNode lenNode;
423413

424414
@CompilationFinal private IsBuiltinClassProfile keyErrorProfile;
425415

0 commit comments

Comments
 (0)