You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mypyc] feat: cache len for container creation from expressions with length known at compile time (#19503)
Currently, if a user uses an immutable type as the sequence input for a
for loop, the length is checked once at each iteration which, while
necessary for some container types such as list and dictionaries, is not
necessary for iterating over immutable types tuple, str, and bytes.
This PR modifies the codebase such that the length is only checked at
the first iteration, and reused from there.
Also, in cases where a simple genexp is the input argument for a tuple,
the length is currently checked one additional time before entering the
iteration (this is done to determine how to size the new tuple). In
those cases, we don't even need a length check at the first iteration
step, and can reuse the result of that first `len` call (or compile-time
determined constant) instead.
Lastly, in cases where a tuple is created from a genexp and the length
of the genexp is knowable at compile time, this PR replaces
PyList_AsTuple with the tuple constructor fast-path.
0 commit comments