Skip to content

Virtual iterators #724

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 Mar 31, 2025 · 1 comment
Open

Virtual iterators #724

markshannon opened this issue Mar 31, 2025 · 1 comment

Comments

@markshannon
Copy link
Member

Discussed in #392

Originally posted by markshannon May 20, 2022
Much like we have "virtual bound-methods" for calls to methods, we could have "virtual iterators" for iteration over sequences.

The LOAD_METHOD instruction pushes NULL func or func self to the stack, the latter being a representation of the bound-method types.MethodType(func, self).
We could do something similar for GET_ITER.
For non-sequence iterators, like range, generators etc, it would push NULL iter. For sequences, it would push seq 0.
The behavior of FOR_ITER would depend on whether SECOND is NULL.

  • NULL: Push next(iter)
  • Not NULL: Pop index; push index+1; push seq[index]

If we allow unboxed integers on the stack, then this could be reasonably efficient with specialization.
For this to be useful, we would need have tagged integers on the stack.
I doubt it would be worth implementing tagged ints just for this, but if we had tagged values for another reason, e.g. deferred reference counts, then it could be a worthwhile enhancement.

@markshannon
Copy link
Member Author

We should be able to do this now, without general support for tagged ints.

The only instructions that would see the tagged index would be FOR_ITER and its specializations, and they should be able to handle tagged integers just fine.
The GC and frame clearing code already ignores immortal objects, so should need only minimal modification, if any.

This would also make specializing GET_ITER more profitable, as GET_ITER_TUPLE and GET_ITER_LIST would simply push 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant