@@ -415,25 +415,38 @@ public PNone extendSequence(PList list, Object source,
415
415
public PNone extend (PList list , Object source ,
416
416
@ Cached ("create()" ) GetIteratorNode getIterator ,
417
417
@ Cached ("create()" ) GetNextNode next ,
418
- @ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ) {
418
+ @ Cached ("createBinaryProfile()" ) ConditionProfile errorProfile ,
419
+ @ Cached ("createAppend()" ) SequenceStorageNodes .AppendNode appendNode ) {
419
420
Object workSource = list != source ? source : factory ().createList (((PList ) source ).getSequenceStorage ().copy ());
421
+ SequenceStorage s = list .getSequenceStorage ();
420
422
Object iterator = getIterator .executeWith (workSource );
421
423
while (true ) {
422
424
Object value ;
423
425
try {
424
426
value = next .execute (iterator );
425
427
} catch (PException e ) {
426
428
e .expectStopIteration (getCore (), errorProfile );
429
+ updateSequenceStorage (list , s );
427
430
return PNone .NONE ;
428
431
}
429
- list .append (value );
432
+ s = appendNode .execute (s , value );
433
+ }
434
+ }
435
+
436
+ private static void updateSequenceStorage (PList list , SequenceStorage s ) {
437
+ if (list .getSequenceStorage () != s ) {
438
+ list .setSequenceStorage (s );
430
439
}
431
440
}
432
441
433
442
protected boolean isPSequenceWithStorage (Object source ) {
434
443
return (source instanceof PSequence && !(source instanceof PTuple || source instanceof PRange ));
435
444
}
436
445
446
+ protected static SequenceStorageNodes .AppendNode createAppend () {
447
+ return SequenceStorageNodes .AppendNode .create (() -> ListGeneralizationNode .create ());
448
+ }
449
+
437
450
}
438
451
439
452
// list.insert(i, x)
@@ -1082,76 +1095,18 @@ protected boolean isNotSameStorage(PList left, PList right) {
1082
1095
@ Builtin (name = __MUL__ , fixedNumOfPositionalArgs = 2 )
1083
1096
@ GenerateNodeFactory
1084
1097
abstract static class MulNode extends PythonBinaryBuiltinNode {
1085
- public static String CANNOT_FIT_MESSAGE = "cannot fit 'int' into an index-sized integer" ;
1086
1098
1087
1099
@ Specialization
1088
- PList doPListInt (PList left , boolean right ,
1089
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1090
- return doPListInt (left , PInt .intValue (right ), profile );
1091
- }
1092
-
1093
- @ Specialization
1094
- PList doPListInt (PList left , int right ,
1095
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1100
+ PList doPListInt (PList left , Object right ,
1101
+ @ Cached ("create()" ) SequenceStorageNodes .RepeatNode repeatNode ) {
1096
1102
try {
1097
- return right > 0 ? left .__mul__ (profile , right ) : factory ().createList ();
1103
+ SequenceStorage repeated = repeatNode .execute (left .getSequenceStorage (), right );
1104
+ return factory ().createList (repeated );
1098
1105
} catch (ArithmeticException | OutOfMemoryError e ) {
1099
1106
throw raise (MemoryError );
1100
1107
}
1101
1108
}
1102
1109
1103
- @ Specialization (guards = "right <= 0" )
1104
- PList doPListLongNegative (@ SuppressWarnings ("unused" ) PList left , @ SuppressWarnings ("unused" ) long right ) {
1105
- return factory ().createList ();
1106
- }
1107
-
1108
- @ Specialization (guards = "right > 0" , rewriteOn = ArithmeticException .class )
1109
- PList doPListLong (PList left , long right ,
1110
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1111
- return doPListInt (left , PInt .intValueExact (right ), profile );
1112
- }
1113
-
1114
- @ Specialization (replaces = "doPListLong" )
1115
- PList doPListLongOvf (PList left , long right ,
1116
- @ Cached ("create()" ) BranchProfile notPositiveProfile ,
1117
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1118
- if (right <= 0 ) {
1119
- notPositiveProfile .enter ();
1120
- return factory ().createList ();
1121
- }
1122
- try {
1123
- return doPListInt (left , PInt .intValueExact (right ), profile );
1124
- } catch (ArithmeticException e ) {
1125
- throw raise (OverflowError , CANNOT_FIT_MESSAGE );
1126
- }
1127
- }
1128
-
1129
- @ Specialization (guards = "right.isZeroOrNegative()" , rewriteOn = ArithmeticException .class )
1130
- PList doPListBigIntNegative (@ SuppressWarnings ("unused" ) PList left , @ SuppressWarnings ("unused" ) PInt right ) {
1131
- return factory ().createList ();
1132
- }
1133
-
1134
- @ Specialization (guards = "!right.isZeroOrNegative()" , rewriteOn = ArithmeticException .class )
1135
- PList doPListBigInt (PList left , PInt right ,
1136
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1137
- return doPListInt (left , right .intValueExact (), profile );
1138
- }
1139
-
1140
- @ Specialization (replaces = "doPListBigInt" )
1141
- PList doPListBigIntOvf (PList left , PInt right ,
1142
- @ Cached ("create()" ) BranchProfile notPositiveProfile ,
1143
- @ Cached ("createClassProfile()" ) ValueProfile profile ) {
1144
- if (right .isZeroOrNegative ()) {
1145
- notPositiveProfile .enter ();
1146
- return factory ().createList ();
1147
- }
1148
- try {
1149
- return doPListInt (left , right .intValueExact (), profile );
1150
- } catch (ArithmeticException | OutOfMemoryError e ) {
1151
- throw raise (OverflowError , CANNOT_FIT_MESSAGE );
1152
- }
1153
- }
1154
-
1155
1110
@ SuppressWarnings ("unused" )
1156
1111
@ Fallback
1157
1112
PNotImplemented doGeneric (Object left , Object right ) {
@@ -1163,6 +1118,7 @@ PNotImplemented doGeneric(Object left, Object right) {
1163
1118
@ GenerateNodeFactory
1164
1119
abstract static class IMulNode extends PythonBuiltinNode {
1165
1120
protected static final String ERROR_MSG = "can't multiply sequence by non-int of type '%p'" ;
1121
+ public static String CANNOT_FIT_MESSAGE = "cannot fit 'int' into an index-sized integer" ;
1166
1122
1167
1123
public abstract PList execute (PList list , Object value );
1168
1124
@@ -1182,7 +1138,7 @@ PList doEmptyLong(PList list, long right) {
1182
1138
PInt .intValueExact (right );
1183
1139
return list ;
1184
1140
} catch (ArithmeticException e ) {
1185
- throw raise (OverflowError , MulNode . CANNOT_FIT_MESSAGE );
1141
+ throw raise (OverflowError , CANNOT_FIT_MESSAGE );
1186
1142
}
1187
1143
}
1188
1144
@@ -1192,7 +1148,7 @@ PList doEmptyPInt(PList list, PInt right) {
1192
1148
right .intValueExact ();
1193
1149
return list ;
1194
1150
} catch (ArithmeticException e ) {
1195
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1151
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1196
1152
}
1197
1153
}
1198
1154
@@ -1217,7 +1173,7 @@ PList doIntInt(PList list, int right) {
1217
1173
} catch (OutOfMemoryError e ) {
1218
1174
throw raise (MemoryError );
1219
1175
} catch (ArithmeticException e ) {
1220
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1176
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1221
1177
}
1222
1178
}
1223
1179
@@ -1226,7 +1182,7 @@ PList doIntLong(PList list, long right) {
1226
1182
try {
1227
1183
return doIntInt (list , PInt .intValueExact (right ));
1228
1184
} catch (ArithmeticException e ) {
1229
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1185
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1230
1186
}
1231
1187
}
1232
1188
@@ -1235,7 +1191,7 @@ PList doIntPInt(PList list, PInt right) {
1235
1191
try {
1236
1192
return doIntInt (list , right .intValueExact ());
1237
1193
} catch (ArithmeticException e ) {
1238
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1194
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1239
1195
}
1240
1196
}
1241
1197
@@ -1260,7 +1216,7 @@ PList doLongInt(PList list, int right) {
1260
1216
} catch (OutOfMemoryError e ) {
1261
1217
throw raise (MemoryError );
1262
1218
} catch (ArithmeticException e ) {
1263
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1219
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1264
1220
}
1265
1221
}
1266
1222
@@ -1269,7 +1225,7 @@ PList doLongLong(PList list, long right) {
1269
1225
try {
1270
1226
return doLongInt (list , PInt .intValueExact (right ));
1271
1227
} catch (ArithmeticException e ) {
1272
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1228
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1273
1229
}
1274
1230
}
1275
1231
@@ -1278,7 +1234,7 @@ PList doLongPInt(PList list, PInt right) {
1278
1234
try {
1279
1235
return doLongInt (list , right .intValueExact ());
1280
1236
} catch (ArithmeticException e ) {
1281
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1237
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1282
1238
}
1283
1239
}
1284
1240
@@ -1303,7 +1259,7 @@ PList doDoubleInt(PList list, int right) {
1303
1259
} catch (OutOfMemoryError e ) {
1304
1260
throw raise (MemoryError );
1305
1261
} catch (ArithmeticException e ) {
1306
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1262
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1307
1263
}
1308
1264
}
1309
1265
@@ -1312,7 +1268,7 @@ PList doDoubleLong(PList list, long right) {
1312
1268
try {
1313
1269
return doDoubleInt (list , PInt .intValueExact (right ));
1314
1270
} catch (ArithmeticException e ) {
1315
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1271
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1316
1272
}
1317
1273
}
1318
1274
@@ -1321,7 +1277,7 @@ PList doDoublePInt(PList list, PInt right) {
1321
1277
try {
1322
1278
return doLongInt (list , right .intValueExact ());
1323
1279
} catch (ArithmeticException e ) {
1324
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1280
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1325
1281
}
1326
1282
}
1327
1283
@@ -1346,7 +1302,7 @@ PList doObjectInt(PList list, int right) {
1346
1302
} catch (OutOfMemoryError e ) {
1347
1303
throw raise (MemoryError );
1348
1304
} catch (ArithmeticException e ) {
1349
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1305
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1350
1306
}
1351
1307
}
1352
1308
@@ -1355,7 +1311,7 @@ PList doObjectLong(PList list, long right) {
1355
1311
try {
1356
1312
return doObjectInt (list , PInt .intValueExact (right ));
1357
1313
} catch (ArithmeticException e ) {
1358
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1314
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1359
1315
}
1360
1316
}
1361
1317
@@ -1364,7 +1320,7 @@ PList doObjectPInt(PList list, PInt right) {
1364
1320
try {
1365
1321
return doObjectInt (list , right .intValueExact ());
1366
1322
} catch (ArithmeticException e ) {
1367
- throw raise (OverflowError , MulNode .CANNOT_FIT_MESSAGE );
1323
+ throw raise (OverflowError , IMulNode .CANNOT_FIT_MESSAGE );
1368
1324
}
1369
1325
}
1370
1326
0 commit comments