From 3c32e5b8d5f894f7b85dd3389391fa194646c5ff Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Sep 2026 22:57:14 +0000 Subject: [PATCH] Render MAC addresses with bytes.hex(":") bytes_to_mac built each address with ":".join(format(o, "02x") for o in data) -- seven generator steps and six format() calls per address, twice per Ethernet frame, plus once per ARP hardware address and NDP link-layer option. A corpus profile attributed 88,200 generator calls and 75,600 format() calls to it. bytes.hex() has taken a separator since 3.8 and does the whole thing in one C call: 16-29x faster on the call depending on run. Output is byte-for-byte identical, verified against the old implementation for both bytes and memoryview input -- memoryview.hex also takes a separator, so the decode-time view path is unaffected. Closes #83 Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01QJnVMNGwTRDktC4rkABtgt --- CHANGELOG.md | 9 +++++++++ src/netprotocols/_base.py | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 444eebb..cf743d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- `bytes_to_mac` renders addresses with `bytes.hex(":")` instead of a + generator over `format()`. The old form ran seven generator steps and + six `format()` calls per address, twice per Ethernet frame, and showed + up in a corpus profile as 88,200 generator calls. Measured 16–29× + faster on the call depending on run. Output is byte-for-byte + identical, and `memoryview.hex` takes a separator too, so a + decode-time view still works. No API change. + ### Fixed - **The release workflow can no longer publish untested code.** Pushing a `v*` tag ran `uv build` and `uv publish` with no dependency on the diff --git a/src/netprotocols/_base.py b/src/netprotocols/_base.py index 79fecd0..75c7af6 100644 --- a/src/netprotocols/_base.py +++ b/src/netprotocols/_base.py @@ -47,7 +47,11 @@ def mac_to_bytes(mac: str) -> bytes: def bytes_to_mac(data: bytes) -> str: """Render 6 raw bytes as a colon-separated lowercase MAC address.""" - return ":".join(format(octet, "02x") for octet in data) + # bytes.hex(sep) does this in one C call; the generator-plus-format + # equivalent it replaces ran seven generator steps and six format() + # calls per address, twice per Ethernet frame. memoryview.hex takes + # a separator too, so this still accepts a decode-time view. + return data.hex(":") def ipv4_to_bytes(addr: str) -> bytes: