From 049756e2f6b7239336d047ab3244937c2a41697e Mon Sep 17 00:00:00 2001 From: Daniel Giger Date: Thu, 5 May 2022 22:16:22 -0400 Subject: [PATCH 1/3] optimize notify() --- Lib/threading.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index 642f93e1eec31c..14cca63e989c03 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -369,15 +369,13 @@ def notify(self, n=1): if not self._is_owned(): raise RuntimeError("cannot notify on un-acquired lock") all_waiters = self._waiters - waiters_to_notify = _deque(_islice(all_waiters, n)) - if not waiters_to_notify: + + if not all_waiters: return - for waiter in waiters_to_notify: + num_to_notify = min(n, len(all_waiters)) + for i in range(num_to_notify): + waiter = all_waiters.popleft() waiter.release() - try: - all_waiters.remove(waiter) - except ValueError: - pass def notify_all(self): """Wake up all threads waiting on this condition. From b95022806fd4f073dfffe5f7fd426af59c2d5f4d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 6 May 2022 03:27:58 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-05-06-03-27-58.gh-issue-92352.Tv7wYm.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-06-03-27-58.gh-issue-92352.Tv7wYm.rst diff --git a/Misc/NEWS.d/next/Library/2022-05-06-03-27-58.gh-issue-92352.Tv7wYm.rst b/Misc/NEWS.d/next/Library/2022-05-06-03-27-58.gh-issue-92352.Tv7wYm.rst new file mode 100644 index 00000000000000..c71ee228ee849b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-06-03-27-58.gh-issue-92352.Tv7wYm.rst @@ -0,0 +1 @@ +Optimizes ``Condition.notify()`` by removing unneeded construction of a ``_deque`` From 25d7683dc288006a6f46ccbc8cc994f393376ca8 Mon Sep 17 00:00:00 2001 From: Daniel Giger Date: Thu, 5 May 2022 23:31:02 -0400 Subject: [PATCH 3/3] rename all_waiters to waiters --- Lib/threading.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index 14cca63e989c03..c783b1540c859a 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -368,13 +368,13 @@ def notify(self, n=1): """ if not self._is_owned(): raise RuntimeError("cannot notify on un-acquired lock") - all_waiters = self._waiters + waiters = self._waiters - if not all_waiters: + if not waiters: return - num_to_notify = min(n, len(all_waiters)) + num_to_notify = min(n, len(waiters)) for i in range(num_to_notify): - waiter = all_waiters.popleft() + waiter = waiters.popleft() waiter.release() def notify_all(self):