Skip to content

Commit 4cb3ecf

Browse files
committed
refactor: switch _seen_logs to dict[str, None] for FIFO eviction
`set.pop()` removes an arbitrary element (hash-bucket order), not the oldest one — the "recency window" framing in ba5de3a was misleading. Switch the dedup containers to insertion-ordered `dict[str, None]` and evict via `del seen[next(iter(seen))]` so eviction is truly FIFO: the oldest entry is dropped per overflow, and `_MAX_SEEN_LOGS` becomes a genuine recency window. The eviction-path delta vs set+pop (~165 vs ~77 ns/call in a microbench) is dwarfed by the `str(sys.exc_info()[1])` interpolation on the calling path; this only runs when a packet has already failed to parse.
1 parent ba5de3a commit 4cb3ecf

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/zeroconf/_logger.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ def set_logger_level_if_unset() -> None:
4040

4141

4242
_MAX_SEEN_LOGS = 512
43-
_seen_logs: set[str] = set()
43+
_seen_logs: dict[str, None] = {}
4444

4545

46-
def _mark_seen(seen: set[str], key: str) -> bool:
46+
def _mark_seen(seen: dict[str, None], key: str) -> bool:
4747
"""Record ``key`` in ``seen`` and return True if it was newly added.
4848
49-
Bounds the set so callers passing attacker-influenced keys (peer
49+
Bounds the dict so callers passing attacker-influenced keys (peer
5050
addresses, packet offsets) cannot grow it without bound. Evicts
51-
one arbitrary entry per overflow so warning-level re-emissions
52-
stay smooth rather than arriving in bursts.
51+
the oldest entry per overflow (dict preserves insertion order on
52+
Python 3.7+), so ``_MAX_SEEN_LOGS`` is a recency window.
5353
"""
5454
if key in seen:
5555
return False
5656
if len(seen) >= _MAX_SEEN_LOGS:
57-
seen.pop()
58-
seen.add(key)
57+
del seen[next(iter(seen))]
58+
seen[key] = None
5959
return True
6060

6161

src/zeroconf/_protocol/incoming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
DECODE_EXCEPTIONS = (IndexError, struct.error, IncomingDecodeError)
6464

6565

66-
_seen_logs: set[str] = set()
66+
_seen_logs: dict[str, None] = {}
6767
_str = str
6868
_int = int
6969

0 commit comments

Comments
 (0)