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

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (6) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 2 additions & 5 deletions Lib/_weakrefset.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ def copy(self):
return self.__class__(self)

def pop(self):
while True:
try:
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.

item = itemref()
if item is not None:
return item
raise KeyError('pop from empty WeakSet')

def remove(self, item):
self.data.remove(ref(item))
Expand Down
14 changes: 4 additions & 10 deletions Lib/concurrent/futures/thread.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,8 @@ def _initializer_failed(self):
self._broken = ('A thread initializer failed, the thread pool '
'is not usable anymore')
# Drain work queue and mark pending futures failed
while True:
try:
work_item = self._work_queue.get_nowait()
except queue.Empty:
break
for work_item in iter(self._work_queue.get_nowait,
stop_exception=queue.Empty):
if work_item is not None:
work_item.future.set_exception(self.BROKEN(self._broken))

Expand All @@ -257,11 +254,8 @@ def shutdown(self, wait=True, *, cancel_futures=False):
if cancel_futures:
# Drain all work items from the queue, and then cancel their
# associated futures.
while True:
try:
work_item = self._work_queue.get_nowait()
except queue.Empty:
break
for work_item in iter(self._work_queue.get_nowait,
stop_exception=queue.Empty):
if work_item is not None:
work_item.future.cancel()

Expand Down
8 changes: 2 additions & 6 deletions Lib/importlib/_bootstrap.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ def clear(self):
self.data = {}

def _commit_removals(self):
pop = self._pending_removals.pop
d = self.data
while True:
try:
key = pop()
except IndexError:
return
for key in iter(self._pending_removals.pop,
stop_exception=IndexError):
_weakref._remove_dead_weakref(d, key)

def get(self, key, default=None):
Expand Down
7 changes: 2 additions & 5 deletions Lib/multiprocessing/heap.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,8 @@ def _remove_allocated_block(self, block):

def _free_pending_blocks(self):
# Free all the blocks in the pending list - called with the lock held.
while True:
try:
block = self._pending_free_blocks.pop()
except IndexError:
break
for block in iter(self._pending_free_blocks.pop,
stop_exception=IndexError):
self._add_free_block(block)
self._remove_allocated_block(block)

Expand Down
7 changes: 2 additions & 5 deletions Lib/multiprocessing/resource_tracker.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,8 @@ def _ensure_running_and_write(self, msg=None):
else:
self._launch()

while True:
try:
reentrant_msg = self._reentrant_messages.popleft()
except IndexError:
break
for reentrant_msg in iter(self._reentrant_messages.popleft,
stop_exception=IndexError):
self._write(reentrant_msg)
if msg is not None:
self._write(msg)
Expand Down
5 changes: 2 additions & 3 deletions Lib/xml/etree/ElementTree.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -1336,9 +1336,8 @@ def read_events(self):
Events are consumed from the internal event queue as they are
retrieved from the iterator.
"""
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.

stop_exception=IndexError):
if isinstance(event, Exception):
raise event
else:
Expand Down
Loading

Back | FazBrowse Home | New Git URL