Skip to content

Commit 4f6e09d

Browse files
Update for_helpers.py
1 parent 8ccf2c3 commit 4f6e09d

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

mypyc/irbuild/for_helpers.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,22 +1209,33 @@ def check_type(obj: Any, typ: Type[Any]) -> bool:
12091209

12101210
# forward involves in the best case one pyssize_t comparison, else one length check + the comparison
12111211
# reverse is slightly slower than forward, with one extra check
1212-
for_sequence_reverse = [
1212+
for_sequence_reverse_with_len_check = [
12131213
g
12141214
for g in gens
1215-
if check_type(g, ForSequence) and (g.gen if isinstance(g, ForEnumerate) else g).reverse
1215+
if check_type(g, ForSequence) and (g := (g.gen if isinstance(g, ForEnumerate) else g)).reverse and g.len_reg is not None
12161216
]
1217-
for_sequence_forward = [
1217+
for_sequence_reverse_no_len_check = [
1218+
g
1219+
for g in gens
1220+
if check_type(g, ForSequence) and (g := (g.gen if isinstance(g, ForEnumerate) else g)).reverse and g.len_reg is None
1221+
]
1222+
for_sequence_forward_with_len_check = [
1223+
g
1224+
for g in gens
1225+
if check_type(g, ForSequence)
1226+
and not (g := (g.gen if isinstance(g, ForEnumerate) else g)).reverse and g.len_reg is not None
1227+
]
1228+
for_sequence_forward_no_len_check = [
12181229
g
12191230
for g in gens
12201231
if check_type(g, ForSequence)
1221-
and not (g.gen if isinstance(g, ForEnumerate) else g).reverse
1232+
and not (g := (g.gen if isinstance(g, ForEnumerate) else g)).reverse and g.len_reg is None
12221233
]
12231234

12241235
# these are really fast, just a C int equality check
12251236
for_range = [g for g in gens if isinstance(g, ForRange)]
12261237

1227-
ordered = for_range + for_sequence_forward + for_sequence_reverse + for_native + for_dict
1238+
ordered = for_range + for_sequence_forward_no_len_check + for_sequence_forward_no_len_check + for_sequence_forward_with_len_check + for_sequence_reverse_with_len_check + for_native + for_dict
12281239

12291240
# this is a failsafe for ForHelper classes which might have been added after this commit but not added to this function's code
12301241
leftovers = [g for g in gens if g not in ordered + for_iterable]

0 commit comments

Comments
 (0)