5
5
*/
6
6
package com .oracle .graal .python .builtins .modules .json ;
7
7
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 ;
8
11
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
9
12
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
10
13
import static com .oracle .graal .python .nodes .SpecialMethodNames .__CALL__ ;
14
+ import static com .oracle .graal .python .nodes .object .IsBuiltinClassProfile .profileClassSlowPath ;
11
15
12
16
import java .util .List ;
13
17
@@ -71,11 +75,14 @@ public abstract static class CallEncoderNode extends PythonTernaryClinicBuiltinN
71
75
@ Child private CallUnaryMethodNode callDefaultFn = CallUnaryMethodNode .create ();
72
76
@ Child private CastToJavaStringNode castEncodeResult = CastToJavaStringNode .create ();
73
77
@ 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 ();
75
81
@ Child private HashingStorageLibrary dictLib = HashingStorageLibrary .getFactory ().createDispatched (6 );
76
82
@ 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 ();
79
86
@ Child private GetClassNode getDictClass = GetClassNode .create ();
80
87
@ Child private ConstructListNode constructList = ConstructListNode .create ();
81
88
@@ -209,7 +216,7 @@ private void appendDict(PJSONEncoder encoder, StringBuilder builder, PDict dict)
209
216
startRecursion (encoder , dict );
210
217
builder .append ('{' );
211
218
212
- if (!encoder .sortKeys && IsBuiltinClassProfile . profileClassSlowPath (getDictClass .execute (dict ), PythonBuiltinClassType . PDict )) {
219
+ if (!encoder .sortKeys && profileClassSlowPath (getDictClass .execute (dict ), PDict )) {
213
220
HashingStorageIterable <DictEntry > entries = dictLib .entries (storage );
214
221
boolean first = true ;
215
222
for (DictEntry entry : entries ) {
@@ -220,12 +227,12 @@ private void appendDict(PJSONEncoder encoder, StringBuilder builder, PDict dict)
220
227
if (encoder .sortKeys ) {
221
228
sortList .execute (null , items , PythonUtils .EMPTY_OBJECT_ARRAY , PKeyword .EMPTY_KEYWORDS );
222
229
}
223
- Object iter = callGetIter .executeObject (null , items );
230
+ Object iter = callGetDictIter .executeObject (null , items );
224
231
boolean first = true ;
225
232
while (true ) {
226
233
Object item ;
227
234
try {
228
- item = callNext .execute (null , iter );
235
+ item = callDictNext .execute (null , iter );
229
236
} catch (PException e ) {
230
237
e .expectStopIteration (stopDictIterationProfile );
231
238
break ;
@@ -279,11 +286,30 @@ private void appendList(PJSONEncoder encoder, StringBuilder builder, PSequence l
279
286
startRecursion (encoder , list );
280
287
builder .append ('[' );
281
288
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 );
285
312
}
286
- appendListObj (encoder , builder , storage .getItemNormalized (i ));
287
313
}
288
314
289
315
builder .append (']' );
0 commit comments