FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-64862: Use iter() with stop_exception in the stdlib by serhiy-storchaka · Pull Request #156770 · python/cpython · GitHub

Repository navigation

gh-64862: Use iter() with stop_exception in the stdlib - #156770

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:iter-stop-exception-uses
Open

serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:iter-stop-exception-uses

Conversation

Copy link
Copy Markdown
Member

Replace hand-written loops which drain a container or a queue with iter(callable, stop_exception=...), added in GH-156298.

All of them except XMLPullParser.read_events() already caught the exception raised by the exhausted container; read_events() tested the queue for emptiness before popping from it, which is also one step less atomic.

Loops which drain a small container are left as they are: raising and catching the exception costs about 100 ns, which only pays off above ~35 items (measured on a PGO build; the per-item cost of the exception form is ~19% lower, because it does not test the container in every iteration).

Replace hand-written loops which drain a container or a queue.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

encukou commented Sep 2, 2026

Copy link
Copy Markdown
Member

What's the benefit of this change again?

Copy link
Copy Markdown
Member Author

Clearer code.

encukou commented Sep 3, 2026

Copy link
Copy Markdown
Member

Then this as unnecessary churn. The existing code is already clear.

Copy link
Copy Markdown
Member Author

It replaces 5 lines with 2 or 1.

Other reason -- dogfooding. By using the new idiom in our code we add additional testing surface and show users how to use it.

brettcannon removed their request for review September 10, 2026 19:16

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I like the change if only to introduce reader to the new idiom. But in two cases it's detrimental to performance at runtime so I would avoid changing those.

Comment thread Lib/_weakrefset.py
itemref = self.data.pop()
except KeyError:
raise KeyError('pop from empty WeakSet') from None
for itemref in iter(self.data.pop, stop_exception=KeyError):

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Not worth putting here. Most WeakSet.pop() calls return at the first live ref, which is almost always the first call. The old code raised and allocated nothing on that path. The new code sets up the iter(), binds the method, checks for the exception type, etc. etc.

I measured and that's 40% slower.

events = self._events_queue
while events:
event = events.popleft()
for event in iter(self._events_queue.popleft,

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is also slower because it raises an exception on every call and the old loop never raised.

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL