GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-132554: "Virtual" iterators by markshannon · Pull Request #132555 · python/cpython

Conversation

@markshannon

@markshannon markshannon commented Apr 15, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Just a draft PR until I have performance numbers.

  • Issue: Use tagged ints for faster iteration #132554

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Apr 15, 2025
Use tagged ints for faster iteration #132554
Open
@markshannon
markshannon force-pushed the virtual-iterators branch from 417cd59 to 6c955e0 Compare April 16, 2025 09:26
@markshannon

markshannon commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

Performance is good. Nothing amazing, but a small speedup.

Stats show no significant changes

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon added 7 commits April 29, 2025 18:22
@markshannon
FOR_ITER now pushes NULL as well as the iterator. Preparation for vir…
7476442
…tual iterators
@markshannon
use tagged ints for indexes. Work in progress
38a429f
@markshannon
Remove debug code
2caf56f
@markshannon
Regenerate files
588943a
@markshannon
Tidy up list of non-escaping functions
aa62e39
@markshannon
Fix up list iteration for FT build
88562ec
@markshannon
Fix tier 2 FT build
025049d
@markshannon
markshannon force-pushed the virtual-iterators branch from a4b740d to 025049d Compare April 29, 2025 17:29
@markshannon markshannon added the skip news label Apr 29, 2025
markshannon added 5 commits April 30, 2025 10:11
@markshannon
Remove debugging code
35e389d
@markshannon
Merge branch 'main' into virtual-iterators
e8f4ce6
@markshannon
Rename function
ed89950
@markshannon
Restrict specialization in free-threaded build
ec8d797
@markshannon
Merge branch 'main' into virtual-iterators
cad1946
@markshannon
markshannon marked this pull request as ready for review April 30, 2025 11:38
@markshannon
markshannon requested review from Fidget-Spinner, ericsnowcurrently and iritkatriel as code owners April 30, 2025 11:38
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 30, 2025
@markshannon
Add news
428735b
iritkatriel
iritkatriel reviewed Apr 30, 2025
View reviewed changes
Comment thread Include/internal/pycore_stackref.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/specialize.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/stackrefs.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

markshannon added 2 commits April 30, 2025 17:12
@markshannon
handle tagged ints when doing type checks
4c83848
@markshannon
Fix long check
f43ccc3
Fidget-Spinner
Fidget-Spinner reviewed Apr 30, 2025
View reviewed changes
Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

markshannon added 2 commits April 30, 2025 20:05
@markshannon
Attempt to make _PyForIter_NextWithIndex thread safe.
3fd46c3
@markshannon
Add review comment
433282f
Fidget-Spinner
Fidget-Spinner reviewed May 1, 2025
View reviewed changes
Comment thread Python/specialize.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

markshannon added 3 commits May 8, 2025 10:55
@markshannon
Simplify list indexing code
e915a05
@markshannon
Merge branch 'main' into virtual-iterators
69a328d
@markshannon
GET_ITER may leave the iterable on the stack
6d1b93e
@markshannon markshannon mentioned this pull request May 8, 2025
LOAD_FAST_BORROW not being used even when safe to do so, if value is live at BB end. #133672
Open
@markshannon
Fix test_dis
37ca285
@ljfp ljfp mentioned this pull request May 8, 2025
gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW #133721
Closed
@Fidget-Spinner

Fidget-Spinner commented May 11, 2025

Copy link
Copy Markdown
Member

@markshannon I don't think it matters here, but you didn't update in https://github.com/python/cpython/pull/132545/files Py_TAG_BITS to 3. It's still 1. I know it "technically" doesn't matter because we use a bitshift to zero it out, but we should still update it to be consistent.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon
Merge branch 'main' into virtual-iterators
f3b9074
@markshannon
markshannon force-pushed the virtual-iterators branch from efe465d to f3b9074 Compare May 19, 2025 13:42
markshannon added 3 commits May 20, 2025 16:49
@markshannon
Merge branch 'main' into virtual-iterators
0efab59
@markshannon
Merge branch 'main' into virtual-iterators
a0d814b
@markshannon
Merge branch 'main' into virtual-iterators
5cd55c4
@markshannon

markshannon commented May 22, 2025

Copy link
Copy Markdown
Member Author

@markshannon I don't think it matters here, but you didn't update in https://github.com/python/cpython/pull/132545/files Py_TAG_BITS to 3. It's still 1. I know it "technically" doesn't matter because we use a bitshift to zero it out, but we should still update it to be consistent.

Fixed in #134244

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon
Merge branch 'main' into virtual-iterators
867bd10
@markshannon markshannon mentioned this pull request May 27, 2025
[3.14] GH-128161: Remove redundant GET_ITER from list comprehension code #134778
Merged
iritkatriel
iritkatriel reviewed May 27, 2025
View reviewed changes
Comment thread Include/internal/pycore_stackref.h Outdated
static inline _PyStackRef
PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
{
assert(ref.bits != (uintptr_t)-1); // Deosn't overflow

@iritkatriel iritkatriel May 27, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I don't understand why you use this condition. Should it not be assert(ref.bits + 4 > ref.bits) or something like that?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
markshannon added 2 commits May 27, 2025 12:15
@markshannon
Clarify assert
d9dc597
@markshannon
Checked for tagged ints
d60ef23
iritkatriel
iritkatriel reviewed May 27, 2025
View reviewed changes
Comment thread Include/internal/pycore_stackref.h Outdated
return false;
}
return PyFunction_Check(PyStackRef_AsPyObjectBorrow(stackref));
}

@iritkatriel iritkatriel May 27, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Would it help to define these via a macro? Something like

#define STACKREF_CHECK_FUNC(T) \
    static inline bool \
    PyStackRef_ ## T ## Check(_PyStackRef stackref) \
        if (PyStackRef_IsTaggedInt(stackref)) { \
            return false; \
        } \
        return Py ## T ## _Check(PyStackRef_AsPyObjectBorrow(stackref)); \
    }

...
STACKREF_CHECK_FUNC(Exception);
STACKREF_CHECK_FUNC(Code);
STACKREF_CHECK_FUNC(Function);

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Fidget-Spinner Fidget-Spinner May 27, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think it might not work when want to define variants of Check and CheckExact, though we can always define two macros for that if we go down this route.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_list.py
iterable doesn't prematurely free the iterable"""

def foo(x):
r = 0

@iritkatriel iritkatriel May 27, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'd add this to make sure the test is testing what the comment is saying.
assert(sys.getrefcount(x) == 1)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/stackrefs.c Outdated
_PyStackRef
PyStackRef_IncrementTaggedIntNoOverflow(_PyStackRef ref)
{
assert(ref.index != (uintptr_t)-1); // Overflow

@iritkatriel iritkatriel May 27, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Do here same as in d9dc597 ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
Address review comments
9a9d20d
iritkatriel
iritkatriel approved these changes May 27, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 27, 2025
@markshannon
markshannon merged commit f6f4e8a into python:main May 27, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label May 27, 2025
@markshannon
markshannon deleted the virtual-iterators branch May 27, 2025 14:59
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@markshannon @Pranjal095
pythonGH-132554: "Virtual" iterators (pythonGH-132555)
03f4000
* FOR_ITER now pushes either the iterator and NULL or leaves the iterable and pushes tagged zero

* NEXT_ITER uses the tagged int as the index into the sequence or, if TOS is NULL, iterates as before.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@markshannon @taegyunkim
pythonGH-132554: "Virtual" iterators (pythonGH-132555)
b3fb901
* FOR_ITER now pushes either the iterator and NULL or leaves the iterable and pushes tagged zero

* NEXT_ITER uses the tagged int as the index into the sequence or, if TOS is NULL, iterates as before.
@ljfp ljfp mentioned this pull request Apr 25, 2026
gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW #148999
Open
gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
@markshannon @gostak-dd
pythonGH-132554: "Virtual" iterators (pythonGH-132555)
88df9e9
* FOR_ITER now pushes either the iterator and NULL or leaves the iterable and pushes tagged zero

* NEXT_ITER uses the tagged int as the index into the sequence or, if TOS is NULL, iterates as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner left review comments
@iritkatriel iritkatriel iritkatriel approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@markshannon @Fidget-Spinner @iritkatriel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.