Skip to content

gh-138325: steal list items in INTRINSIC_LIST_TO_TUPLE#149960

Open
eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:list_to_tuple_intrinsic_steal
Open

gh-138325: steal list items in INTRINSIC_LIST_TO_TUPLE#149960
eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:list_to_tuple_intrinsic_steal

Conversation

@eendebakpt
Copy link
Copy Markdown
Contributor

@eendebakpt eendebakpt commented May 17, 2026

INTRINSIC_LIST_TO_TUPLE is only emitted by the compiler for a freshly-built, uniquely-referenced temporary list. We can steal its items into the tuple via _PyList_AsTupleAndClear instead of copying them with PyTuple_FromArray.

Benchmark results:

Benchmark main PR
tuple(genexpr) 23.6 us 22.2 us: 1.07x faster
(*genexpr,) 26.1 us 24.8 us: 1.05x faster
(*[listcomp],) 19.1 us 17.7 us: 1.08x faster
tuple([listcomp]) 17.5 us 17.5 us: not significant
Geometric mean (ref) 1.05x faster

tuple([listcomp]) (a plain tuple() call, no intrinsic) is a control benchmark

Benchmark script
import pyperf

CASES = {
    "tuple(genexpr)":    "tuple(2*x for x in range(1000))",
    "tuple([listcomp])": "tuple([2*x for x in range(1000)])",
    "(*genexpr,)":       "(*(2*x for x in range(1000)),)",
    "(*[listcomp],)":    "(*[2*x for x in range(1000)],)",
}

runner = pyperf.Runner()
for name, stmt in CASES.items():
    runner.timeit(name=name, stmt=stmt)

Run: python bench.py -o OUT.json, then
python -m pyperf compare_to main.json branch.json --table.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant