Skip to content

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

gh-64862: Use iter() with stop_exception in the stdlib#156770
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:iter-stop-exception-uses

Conversation

@serhiy-storchaka

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

1 participant