Skip to content

gh-162350: Add AC vectorcall for itertools - #162590

Merged
cmaloney merged 8 commits into
python:mainfrom
tanloong:itertools_vectorcall
Nov 26, 2026
Merged

cmaloney merged 8 commits into
python:mainfrom
tanloong:itertools_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Add @vectorcall to existing AC.

Benchmark:

+-------------------------------------+--------+----------------------+
| Benchmark                           | main   | patch                |
+=====================================+========+======================+
| batched(x, 2)                       | 171 ns | 143 ns: 1.20x faster |
+-------------------------------------+--------+----------------------+
| pairwise(x)                         | 174 ns | 145 ns: 1.20x faster |
+-------------------------------------+--------+----------------------+
| groupby(x)                          | 173 ns | 131 ns: 1.32x faster |
+-------------------------------------+--------+----------------------+
| cycle(x)                            | 171 ns | 143 ns: 1.20x faster |
+-------------------------------------+--------+----------------------+
| dropwhile(pred, x)                  | 162 ns | 127 ns: 1.28x faster |
+-------------------------------------+--------+----------------------+
| takewhile(pred, x)                  | 162 ns | 128 ns: 1.27x faster |
+-------------------------------------+--------+----------------------+
| starmap(pow, pairs)                 | 173 ns | 136 ns: 1.28x faster |
+-------------------------------------+--------+----------------------+
| combinations(x, 2)                  | 208 ns | 157 ns: 1.32x faster |
+-------------------------------------+--------+----------------------+
| combinations_with_replacement(x, 2) | 201 ns | 160 ns: 1.26x faster |
+-------------------------------------+--------+----------------------+
| permutations(x)                     | 199 ns | 156 ns: 1.27x faster |
+-------------------------------------+--------+----------------------+
| accumulate(x)                       | 169 ns | 134 ns: 1.26x faster |
+-------------------------------------+--------+----------------------+
| compress(x, selectors)              | 172 ns | 143 ns: 1.20x faster |
+-------------------------------------+--------+----------------------+
| filterfalse(pred, x)                | 159 ns | 125 ns: 1.27x faster |
+-------------------------------------+--------+----------------------+
| count()                             | 133 ns | 116 ns: 1.15x faster |
+-------------------------------------+--------+----------------------+
Details
import pyperf

SETUP = """
import itertools
x = [1, 2, 3]
pred = lambda v: False
pairs = [(2, 3)]
selectors = [1, 0, 1]
"""

CASES = [
    ('batched(x, 2)', 'itertools.batched(x, 2)'),
    ('pairwise(x)', 'itertools.pairwise(x)'),
    # _grouper is internal; exercised through groupby()
    ('groupby(x)', 'itertools.groupby(x)'),
    # teedataobject is internal; exercised through tee()
    ('tee(x, 2)', 'itertools.tee(x, 2)'),
    ('cycle(x)', 'itertools.cycle(x)'),
    ('dropwhile(pred, x)', 'itertools.dropwhile(pred, x)'),
    ('takewhile(pred, x)', 'itertools.takewhile(pred, x)'),
    ('starmap(pow, pairs)', 'itertools.starmap(pow, pairs)'),
    ('combinations(x, 2)', 'itertools.combinations(x, 2)'),
    ('combinations_with_replacement(x, 2)', 'itertools.combinations_with_replacement(x, 2)'),
    ('permutations(x)', 'itertools.permutations(x)'),
    ('accumulate(x)', 'itertools.accumulate(x)'),
    ('compress(x, selectors)', 'itertools.compress(x, selectors)'),
    ('filterfalse(pred, x)', 'itertools.filterfalse(pred, x)'),
    ('count()', 'itertools.count()'),
]

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

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this into smaller more reviewable pieces. The just add @vectorcall can all be one PR / that just depends on codegen being right + the implementations being sure they take strong references generally.

For the ones adding signatures I'm okay with 2-3 in a single PR but try to keep them small / related cases. For all of them the more we can make things line up between here and the docs so inspect.signature gives the right thing the better. Not everything is supported in AC but can always use @text_signature to override.

Comment thread Modules/itertoolsmodule.c Outdated
@vectorcall
@classmethod
itertools.islice.__new__ as islice_new
iterable: object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we more closelkly match the docs names for the parameters? (esp. since this makes the signature visible): https://docs.python.org/3/library/itertools.html#itertools.islice

Comment thread Modules/itertoolsmodule.c Outdated
PyTypeObject *chain_type = state->chain_type;
if ((type == chain_type || type->tp_init == chain_type->tp_init) &&
!_PyArg_NoKeywords("chain", kwds))
PyObject *source = PyTuple_FromArray(iterables, iterables_length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for cases like this where we always are going to need a tuple other converters are likely more efficient. It may actually be a case to avoid Vectorcall as we will always end up constructing a tuple anyways so avoiding one in the call doesn't help a lot.

Comment thread Modules/itertoolsmodule.c
[clinic start generated code]*/

static PyObject *
itertools__tee_impl(PyTypeObject *type, PyObject *iterable)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this one isn't actually used in the a common construction path tee is implemented by itertools_tee_impl which ends up calling teedataobject_newinternal rather than this. That means this is just adding code / binary size without providing benefit.

@tanloong

Copy link
Copy Markdown
Contributor Author

Split two separate PRs: #162663 (for itertools.islice / repeat; renamed islice's parameters), #162664 (for itertools.product / zip_longest; reverted vectorcall of chain). Reverted vectorcall of tee in this PR.

@cmaloney
cmaloney merged commit 9e107e3 into python:main Nov 26, 2026
56 checks passed
@cmaloney

Copy link
Copy Markdown
Contributor

That looks good! Should have it all reviewed by Monday (holiday weekend here).

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.

2 participants