Skip to content

gh-162350: Use AC vectorcall for itertools.islice/ repeat - #162664

Open
tanloong wants to merge 11 commits into
python:mainfrom
tanloong:islice_repeat_vectorcall
Open

tanloong wants to merge 11 commits into
python:mainfrom
tanloong:islice_repeat_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written constructors with AC + vectorcall. itertools.repeat(1, 2**100) now raises OverflowError: cannot fit 'int' into an index-sized integer instead of OverflowError: Python int too large to convert to C ssize_t.

Benchmark:

+----------------+--------+----------------------+
| Benchmark      | main   | patch                |
+================+========+======================+
| islice(x, 2)   | 191 ns | 148 ns: 1.29x faster |
+----------------+--------+----------------------+
| repeat(1)      | 182 ns | 115 ns: 1.59x faster |
+----------------+--------+----------------------+
| Geometric mean | (ref)  | 1.43x faster         |
+----------------+--------+----------------------+
Details
import pyperf

SETUP = """
import itertools
x = [1, 2, 3]
"""

CASES = [
    ('islice(x, 2)', 'itertools.islice(x, 2)'),
    ('repeat(1)', 'itertools.repeat(1)'),
]

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

Comment thread Modules/itertoolsmodule.c Resolved
Comment thread Modules/itertoolsmodule.c Resolved
@cmaloney

Copy link
Copy Markdown
Contributor

Perf gains for this one are lot bigger than I expected. Nice!

@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.

Are some borrowed references which need a strong reference taken before arbitrary code is called. Want to see if you can find them yourself?

@tanloong

tanloong commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

PyNumber_AsSsize_t() calls _PyNumber_Index(item) which calls Py_TYPE(item)->tp_as_number->nb_index(item), and can run arbitrary __index__ code. it = PyObject_GetIter(iterable) can run arbitrary __iter__ code. Updated to add strong references.

@rhettinger rhettinger self-assigned this Nov 29, 2026
@rhettinger

Copy link
Copy Markdown
Contributor
  • The INCREFs seem unnecessary. We don't do that elsewhere and there are no tests to prove that it is necessary.

  • In the two-argument form the parameter named start holds the stop value, giving stop_val = PyNumber_AsSsize_t(start, …). The old neutral a1, a2, a3 read better. Please restore the old names and make the diff as minimal as possible.

@cmaloney

Copy link
Copy Markdown
Contributor

re: INCREFs do it only when needed such as in #162748.

From the base conversion issue:

tp_call keeps strong references in the argument tuple. In vectorcall the arguments are borrowed references. Make sure to hold a strong reference (Py_INCREF) before calling arbitrary Python code.

@tanloong

tanloong commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

For context, the argument naming was suggested to change from a1/a2/a3 to start/stop/step in #162590, at which point the PR had not yet been split.

Reverted to use old names. (My gut feeling is to add a clarifying comment to the seemingly counterintuitive stop_val = PyNumber_AsSsize_t(start, ...); but in that case the diff is still large.)

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.

3 participants