Skip to content

Use tagged ints for faster iteration #132554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
markshannon opened this issue Apr 15, 2025 · 3 comments
Open

Use tagged ints for faster iteration #132554

markshannon opened this issue Apr 15, 2025 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@markshannon
Copy link
Member

markshannon commented Apr 15, 2025

Iteration over tuples and short lists is quite inefficient as we need to create an iterator object, only to have to destroy it again moments later. Not only that, fetching values from iterators involves additional indirection compared to fetching them from sequences.

Instead we can push a pair of values to the stack. For common sequences, like tuple, list, strings, some ranges and a few others, we push the sequence and the integer index (initially 0) to the stack. For other iterables, we push the iterator and NULL.

GET_ITER will have the signature:
iterable -- iter, index_or_null
FOR_ITER now has the signature:
iter, index_or_null -- iter, index_or_null, next.

What makes this efficient is tagged integers. By using tagged integers, no objects need to be created.

Examples

GET_ITER

[ <tuple at ...> ] -> [ <tuple at ...>, 0 ]

[ <file at ...> ] -> [ <file iterator at ...>, NULL ]

FOR_ITER

[ <tuple at ...>, 0 ] -> [ <tuple at ...>, 1, item0 ]

[ <file iterator at ...>, NULL ] -> [ <file iterator at ...>, NULL, line ]

Linked PRs

@markshannon
Copy link
Member Author

@picnixz picnixz added type-feature A feature request or enhancement performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Apr 17, 2025
@mdboom mdboom self-assigned this Apr 21, 2025
@mdboom
Copy link
Contributor

mdboom commented Apr 21, 2025

As discussed in our meeting, I'm going to take a first crack at this.

@mdboom mdboom removed their assignment Apr 21, 2025
@mdboom
Copy link
Contributor

mdboom commented Apr 21, 2025

As discussed in our meeting, I'm going to take a first crack at this.

Actually, realizing @markshannon already started that, I'm instead going to follow-on and do GET_ITER specializations.

markshannon added a commit that referenced this issue Apr 29, 2025
* Add stats for GET_ITER

* Look for common iterable types, not iterator types

* Add stats for self iter and fix naming in summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants