Skip to content

Commit 456b25f

Browse files
committed
use iterators to get elements from derived list/tuple sequences
1 parent 5022d2d commit 456b25f

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONEncoderBuiltins.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66
package com.oracle.graal.python.builtins.modules.json;
77

8+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PDict;
9+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PList;
10+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PTuple;
811
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
912
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
1013
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
14+
import static com.oracle.graal.python.nodes.object.IsBuiltinClassProfile.profileClassSlowPath;
1115

1216
import java.util.List;
1317

@@ -71,11 +75,14 @@ public abstract static class CallEncoderNode extends PythonTernaryClinicBuiltinN
7175
@Child private CallUnaryMethodNode callDefaultFn = CallUnaryMethodNode.create();
7276
@Child private CastToJavaStringNode castEncodeResult = CastToJavaStringNode.create();
7377
@Child private LookupAndCallUnaryNode callGetItems = LookupAndCallUnaryNode.create(SpecialMethodNames.ITEMS);
74-
@Child private LookupAndCallUnaryNode callGetIter = LookupAndCallUnaryNode.create(SpecialMethodNames.__ITER__);
78+
@Child private LookupAndCallUnaryNode callGetDictIter = LookupAndCallUnaryNode.create(SpecialMethodNames.__ITER__);
79+
@Child private GetNextNode callDictNext = GetNextNode.create();
80+
@Child private IsBuiltinClassProfile stopDictIterationProfile = IsBuiltinClassProfile.create();
7581
@Child private HashingStorageLibrary dictLib = HashingStorageLibrary.getFactory().createDispatched(6);
7682
@Child private ListSortNode sortList = ListSortNode.create();
77-
@Child private IsBuiltinClassProfile stopDictIterationProfile = IsBuiltinClassProfile.create();
78-
@Child private GetNextNode callNext = GetNextNode.create();
83+
@Child private LookupAndCallUnaryNode callGetListIter = LookupAndCallUnaryNode.create(SpecialMethodNames.__ITER__);
84+
@Child private GetNextNode callListNext = GetNextNode.create();
85+
@Child private IsBuiltinClassProfile stopListIterationProfile = IsBuiltinClassProfile.create();
7986
@Child private GetClassNode getDictClass = GetClassNode.create();
8087
@Child private ConstructListNode constructList = ConstructListNode.create();
8188

@@ -209,7 +216,7 @@ private void appendDict(PJSONEncoder encoder, StringBuilder builder, PDict dict)
209216
startRecursion(encoder, dict);
210217
builder.append('{');
211218

212-
if (!encoder.sortKeys && IsBuiltinClassProfile.profileClassSlowPath(getDictClass.execute(dict), PythonBuiltinClassType.PDict)) {
219+
if (!encoder.sortKeys && profileClassSlowPath(getDictClass.execute(dict), PDict)) {
213220
HashingStorageIterable<DictEntry> entries = dictLib.entries(storage);
214221
boolean first = true;
215222
for (DictEntry entry : entries) {
@@ -220,12 +227,12 @@ private void appendDict(PJSONEncoder encoder, StringBuilder builder, PDict dict)
220227
if (encoder.sortKeys) {
221228
sortList.execute(null, items, PythonUtils.EMPTY_OBJECT_ARRAY, PKeyword.EMPTY_KEYWORDS);
222229
}
223-
Object iter = callGetIter.executeObject(null, items);
230+
Object iter = callGetDictIter.executeObject(null, items);
224231
boolean first = true;
225232
while (true) {
226233
Object item;
227234
try {
228-
item = callNext.execute(null, iter);
235+
item = callDictNext.execute(null, iter);
229236
} catch (PException e) {
230237
e.expectStopIteration(stopDictIterationProfile);
231238
break;
@@ -279,11 +286,30 @@ private void appendList(PJSONEncoder encoder, StringBuilder builder, PSequence l
279286
startRecursion(encoder, list);
280287
builder.append('[');
281288

282-
for (int i = 0; i < storage.length(); i++) {
283-
if (i > 0) {
284-
builder.append(encoder.itemSeparator);
289+
if (profileClassSlowPath(getDictClass.execute(list), PTuple) || profileClassSlowPath(getDictClass.execute(list), PList)) {
290+
for (int i = 0; i < storage.length(); i++) {
291+
if (i > 0) {
292+
builder.append(encoder.itemSeparator);
293+
}
294+
appendListObj(encoder, builder, storage.getItemNormalized(i));
295+
}
296+
} else {
297+
Object iter = callGetListIter.executeObject(null, list);
298+
boolean first = true;
299+
while (true) {
300+
Object item;
301+
try {
302+
item = callListNext.execute(null, iter);
303+
} catch (PException e) {
304+
e.expectStopIteration(stopListIterationProfile);
305+
break;
306+
}
307+
if (first) {
308+
builder.append(encoder.itemSeparator);
309+
first = false;
310+
}
311+
appendListObj(encoder, builder, item);
285312
}
286-
appendListObj(encoder, builder, storage.getItemNormalized(i));
287313
}
288314

289315
builder.append(']');

0 commit comments

Comments
 (0)