From: Tamir Duberstein <tamird@kernel.org>
To: "Kernel.org Tools" <tools@kernel.org>
Cc: Konstantin Ryabitsev <konstantin@linuxfoundation.org>,
Tamir Duberstein <tamird@kernel.org>
Subject: [PATCH b4 09/12] Avoid duplicate map lookups
Date: Tue, 07 Apr 2026 12:48:38 -0400 [thread overview]
Message-ID: <20260407-ruff-check-v1-9-c9568541ff67@kernel.org> (raw)
In-Reply-To: <20260407-ruff-check-v1-0-c9568541ff67@kernel.org>
Use dict.get and defaultdict to avoid repeated membership checks before
indexing into the same map. This keeps the existing behavior while
making later type narrowing easier for mypy and pyright.
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
src/b4/__init__.py | 100 ++++++++++++++++++++++++++---------------------------
src/b4/ez.py | 10 +++---
2 files changed, 55 insertions(+), 55 deletions(-)
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index f38787b..e6eae3e 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -29,6 +29,7 @@ import tempfile
import textwrap
import time
import urllib.parse
+from collections import defaultdict
from contextlib import contextmanager
from pathlib import Path
from typing import (
@@ -190,7 +191,7 @@ class LoreMailbox:
msgid_map: Dict[str, 'LoreMessage']
series: Dict[int, 'LoreSeries']
covers: Dict[int, 'LoreMessage']
- trailer_map: Dict[str, List['LoreMessage']]
+ trailer_map: defaultdict[str, List['LoreMessage']]
followups: List['LoreMessage']
unknowns: List['LoreMessage']
@@ -198,7 +199,7 @@ class LoreMailbox:
self.msgid_map = dict()
self.series = dict()
self.covers = dict()
- self.trailer_map = dict()
+ self.trailer_map = defaultdict(list)
self.followups = list()
self.unknowns = list()
@@ -215,11 +216,6 @@ class LoreMailbox:
return '\n'.join(out)
- def get_by_msgid(self, msgid: str) -> Optional['LoreMessage']:
- if msgid in self.msgid_map:
- return self.msgid_map[msgid]
- return None
-
def partial_reroll(self, revision: int, sloppytrailers: bool) -> None:
# Is it a partial reroll?
# To qualify for a partial reroll:
@@ -236,11 +232,13 @@ class LoreMailbox:
for patch in lser.patches:
if patch is None:
continue
- if patch.in_reply_to is None or patch.in_reply_to not in self.msgid_map:
+ if (
+ patch.in_reply_to is None
+ or (ppatch := self.msgid_map.get(patch.in_reply_to)) is None
+ ):
logger.debug('Patch not sent as a reply-to')
sane = False
break
- ppatch = self.msgid_map[patch.in_reply_to]
found = False
while True:
if patch.counter == ppatch.counter and patch.expected == ppatch.expected:
@@ -248,9 +246,12 @@ class LoreMailbox:
found = True
break
# Do we have another level up?
- if ppatch.in_reply_to is None or ppatch.in_reply_to not in self.msgid_map:
+ if (
+ ppatch.in_reply_to is None
+ or (npatch := self.msgid_map.get(ppatch.in_reply_to)) is None
+ ):
break
- ppatch = self.msgid_map[ppatch.in_reply_to]
+ ppatch = npatch
if not found:
sane = False
@@ -305,9 +306,7 @@ class LoreMailbox:
logger.debug('Retrieved %s matching code-review messages', len(qmsgs))
patchid_map = map_codereview_trailers(qmsgs, ignore_msgids=set(self.msgid_map.keys()))
for patchid, fmsgs in patchid_map.items():
- if patchid not in self.trailer_map:
- self.trailer_map[patchid] = list()
- self.trailer_map[patchid] += fmsgs
+ self.trailer_map[patchid].extend(fmsgs)
def get_latest_revision(self) -> Optional[int]:
@@ -328,10 +327,10 @@ class LoreMailbox:
revision = self.get_latest_revision()
if revision is None:
return None
- elif revision not in self.series:
- return None
- lser = self.series[revision]
+ lser = self.series.get(revision)
+ if lser is None:
+ return None
# Is it empty?
empty = True
@@ -347,15 +346,15 @@ class LoreMailbox:
self.partial_reroll(revision, sloppytrailers)
# Grab our cover letter if we have one
- if revision in self.covers:
- lser.add_patch(self.covers[revision])
+ if (cover := self.covers.get(revision)) is not None:
+ lser.add_patch(cover)
lser.has_cover = True
else:
# Let's find the first patch with an in-reply-to and see if that
# is our cover letter
for member in lser.patches:
if member is not None and member.in_reply_to is not None:
- potential = self.get_by_msgid(member.in_reply_to)
+ potential = self.msgid_map.get(member.in_reply_to)
if potential is not None and potential.has_diffstat and not potential.has_diff:
# This is *probably* the cover letter
lser.patches[0] = potential
@@ -380,12 +379,13 @@ class LoreMailbox:
if fmsg.in_reply_to is None:
# Check if there's something matching in References
for refid in fmsg.references:
- if refid in self.msgid_map and refid != fmsg.msgid:
- pmsg = self.msgid_map[refid]
+ if (
+ refid != fmsg.msgid
+ and (pmsg := self.msgid_map.get(refid)) is not None
+ ):
logger.debug('Found a references entry %s in msgid_map', refid)
break
- elif fmsg.in_reply_to in self.msgid_map:
- pmsg = self.msgid_map[fmsg.in_reply_to]
+ elif (pmsg := self.msgid_map.get(fmsg.in_reply_to)) is not None:
logger.debug('Found in-reply-to %s in msgid_map', fmsg.in_reply_to)
if pmsg is None:
# Can't find the message we're replying to here
@@ -407,8 +407,6 @@ class LoreMailbox:
# previous revisions to current revision if patch id did
# not change
if pmsg.git_patch_id:
- if pmsg.git_patch_id not in self.trailer_map:
- self.trailer_map[pmsg.git_patch_id] = list()
self.trailer_map[pmsg.git_patch_id].append(fmsg)
pmsg.followup_trailers += trailers
break
@@ -416,15 +414,18 @@ class LoreMailbox:
# Could be a cover letter
pmsg.followup_trailers += trailers
break
- if pmsg.in_reply_to and pmsg.in_reply_to in self.msgid_map:
+ if (
+ pmsg.in_reply_to
+ and (nmsg := self.msgid_map.get(pmsg.in_reply_to)) is not None
+ ):
# Avoid bad message id causing infinite loop
- if pmsg == self.msgid_map[pmsg.in_reply_to]:
+ if pmsg == nmsg:
break
lvl += 1
for pltr in pmsg.trailers:
pltr.lmsg = pmsg
trailers.append(pltr)
- pmsg = self.msgid_map[pmsg.in_reply_to]
+ pmsg = nmsg
continue
break
@@ -434,25 +435,24 @@ class LoreMailbox:
if lmsg is None or lmsg.git_patch_id is None:
continue
logger.debug(' matching patch_id %s from: %s', lmsg.git_patch_id, lmsg.full_subject)
- if lmsg.git_patch_id in self.trailer_map:
- for fmsg in self.trailer_map[lmsg.git_patch_id]:
- logger.debug(' matched: %s', fmsg.msgid)
- fltrs, fmis = fmsg.get_trailers(sloppy=sloppytrailers)
- for fltr in fltrs:
- if fltr in lmsg.trailers:
- logger.debug(' trailer already exists')
- continue
- if fltr in lmsg.followup_trailers:
- logger.debug(' identical trailer received for this series')
- continue
- logger.debug(' carrying over the trailer to this series (may be duplicate)')
- logger.debug(' %s', lmsg.full_subject)
- logger.debug(' + %s', fltr.as_string())
- if fltr.lmsg:
- logger.debug(' via: %s', fltr.lmsg.msgid)
- lmsg.followup_trailers.append(fltr)
- for fltr in fmis:
- lser.trailer_mismatches.add((fltr.name, fltr.value, fmsg.fromname, fmsg.fromemail))
+ for fmsg in self.trailer_map.get(lmsg.git_patch_id, ()):
+ logger.debug(' matched: %s', fmsg.msgid)
+ fltrs, fmis = fmsg.get_trailers(sloppy=sloppytrailers)
+ for fltr in fltrs:
+ if fltr in lmsg.trailers:
+ logger.debug(' trailer already exists')
+ continue
+ if fltr in lmsg.followup_trailers:
+ logger.debug(' identical trailer received for this series')
+ continue
+ logger.debug(' carrying over the trailer to this series (may be duplicate)')
+ logger.debug(' %s', lmsg.full_subject)
+ logger.debug(' + %s', fltr.as_string())
+ if fltr.lmsg:
+ logger.debug(' via: %s', fltr.lmsg.msgid)
+ lmsg.followup_trailers.append(fltr)
+ for fltr in fmis:
+ lser.trailer_mismatches.add((fltr.name, fltr.value, fmsg.fromname, fmsg.fromemail))
return lser
@@ -487,7 +487,7 @@ class LoreMailbox:
if lmsg.revision_inferred and lmsg.in_reply_to:
# We have an inferred revision here.
# Do we have an upthread cover letter that specifies a revision?
- irt = self.get_by_msgid(lmsg.in_reply_to)
+ irt = self.msgid_map.get(lmsg.in_reply_to)
if irt is not None and irt.has_diffstat and not irt.has_diff:
# Yes, this is very likely our cover letter
logger.debug(' fixed revision to v%s', irt.revision)
@@ -3396,7 +3396,7 @@ def get_config_from_git(regexp: str, defaults: Optional[Dict[str, Any]] = None,
chunks = key.split('.')
cfgkey = chunks[-1].lower()
if cfgkey in multivals:
- if cfgkey not in gitconfig or gitconfig[cfgkey] is None:
+ if gitconfig.get(cfgkey) is None:
gitconfig[cfgkey] = list()
gitconfig[cfgkey].append(value)
else:
diff --git a/src/b4/ez.py b/src/b4/ez.py
index b54e04c..51ded98 100644
--- a/src/b4/ez.py
+++ b/src/b4/ez.py
@@ -26,6 +26,7 @@ import textwrap
import time
import urllib.parse
import uuid
+from collections import defaultdict
from email.message import EmailMessage
from string import Template
from typing import Any, Dict, List, Optional, Set, Tuple, Union
@@ -1988,7 +1989,7 @@ def cmd_send(cmdargs: argparse.Namespace) -> None:
seen: Set[str] = set()
excludes: Set[str] = set()
- pccs: Dict[str, List[Tuple[str, str]]] = dict()
+ pccs: defaultdict[str, List[Tuple[str, str]]] = defaultdict(list)
if cmdargs.preview_to or cmdargs.no_trailer_to_cc:
todests = list()
@@ -2011,10 +2012,9 @@ def cmd_send(cmdargs: argparse.Namespace) -> None:
if btr.addr[1] in seen:
continue
if commit:
- if commit not in pccs:
- pccs[commit] = list()
- if btr.addr not in pccs[commit]:
- pccs[commit].append(btr.addr)
+ cpccs = pccs[commit]
+ if btr.addr not in cpccs:
+ cpccs.append(btr.addr)
continue
seen.add(btr.addr[1])
if btr.lname == 'to':
--
2.53.0
next prev parent reply other threads:[~2026-04-07 16:48 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-07 16:48 [PATCH b4 00/12] Enable stricter local checks Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 01/12] Configure ruff format with single quotes Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 02/12] Fix ruff check warnings Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 03/12] Use ruff to sort imports Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 04/12] Import dependencies unconditionally Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 05/12] Fix tests under uv with complex git config Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 06/12] Fix typings in misc/ Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 07/12] Enable mypy unreachable warnings Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 08/12] Enable and fix pyright diagnostics Tamir Duberstein
2026-04-07 16:48 ` Tamir Duberstein [this message]
2026-04-07 16:48 ` [PATCH b4 10/12] Add ty and configuration Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 11/12] Enable pyright strict mode Tamir Duberstein
2026-04-07 16:48 ` [PATCH b4 12/12] Add local CI review check Tamir Duberstein
2026-04-10 15:05 ` [PATCH b4 00/12] Enable stricter local checks Tamir Duberstein
2026-04-10 15:21 ` Konstantin Ryabitsev
2026-04-10 22:39 ` Tamir Duberstein
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260407-ruff-check-v1-9-c9568541ff67@kernel.org \
--to=tamird@kernel.org \
--cc=konstantin@linuxfoundation.org \
--cc=tools@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox