cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-142863: optimize list and set calls with generator expressions

Open kumaraditya303 opened this issue 1 month ago • 4 comments

  • Issue: gh-142863

kumaraditya303 avatar Dec 17 '25 09:12 kumaraditya303

Benchmark results:

Before:

❯ ./python.exe -m timeit "list(x for x in range(10000))"
2000 loops, best of 5: 188 usec per loop
❯ ./python.exe -m timeit "set(x for x in range(10000))"
1000 loops, best of 5: 226 usec per loop

After:

❯ ./python.exe -m timeit "list(x for x in range(10000))"
2000 loops, best of 5: 150 usec per loop
❯ ./python.exe -m timeit "set(x for x in range(10000))"
2000 loops, best of 5: 178 usec per loop

kumaraditya303 avatar Dec 23 '25 08:12 kumaraditya303

This looks promising.

@kumaraditya303 can you benchmark this with small sequences, as those tend to more common.

@iritkatriel could you take a look, as author of the tuple/any/all optimization.

markshannon avatar Dec 23 '25 11:12 markshannon

can you benchmark this with small sequences, as those tend to more common.

Benchmark with smaller sequence: Before:

❯ ./python.exe -m timeit "list(x for x in range(25))" 
500000 loops, best of 5: 483 nsec per loop

After:

 ❯ ./python.exe -m timeit "list(x for x in range(25))" 
500000 loops, best of 5: 390 nsec per loop 

kumaraditya303 avatar Dec 23 '25 11:12 kumaraditya303

@markshannon I have fixed the merge conflicts, can you review this?

kumaraditya303 avatar Jan 05 '26 14:01 kumaraditya303