Target: 1.4.0 (Tier 1 — earn the benchmark)
Every header's __post_init__ runs the address validators, including
when the instance was built by decode(). So on the decode path:
bytes_to_mac() turns six raw bytes into "00:07:0d:af:f4:54".__post_init__ immediately runs validate_mac_addr() on it, which
matches that string against mac_regex.
The regex cannot fail — the string was mechanically generated from six
bytes moments earlier. The same applies to validate_ipv4_addr()
against socket.inet_ntop() output.
After #82 and #83 land, re.Pattern.match becomes the single largest
remaining cost in a corpus profile (27,680 calls in an 80-repetition
run).
Measured
300,000 calls: validate_mac_addr 0.229 s, validate_ipv4_addr
0.120 s → 2.3 µs per Ethernet+IPv4 frame spent proving something
already true.
What to do
Let decode() construct through a path that skips validation, while
keeping strict validation for user-supplied values. Strictness on
construction is a deliberate differentiator and must not be weakened —
the only claim here is that the decoder needn't re-check its own
output.
Options worth weighing: a private _unvalidated() classmethod using
object.__new__ plus object.__setattr__; or a module-private
sentinel that __post_init__ checks. The first avoids a per-instance
branch; the second is less invasive. Whichever is chosen, it must not
appear in the public API.
Acceptance criteria
Target: 1.4.0 (Tier 1 — earn the benchmark)
Every header's
__post_init__runs the address validators, includingwhen the instance was built by
decode(). So on the decode path:bytes_to_mac()turns six raw bytes into"00:07:0d:af:f4:54".__post_init__immediately runsvalidate_mac_addr()on it, whichmatches that string against
mac_regex.The regex cannot fail — the string was mechanically generated from six
bytes moments earlier. The same applies to
validate_ipv4_addr()against
socket.inet_ntop()output.After #82 and #83 land,
re.Pattern.matchbecomes the single largestremaining cost in a corpus profile (27,680 calls in an 80-repetition
run).
Measured
300,000 calls:
validate_mac_addr0.229 s,validate_ipv4_addr0.120 s → 2.3 µs per Ethernet+IPv4 frame spent proving something
already true.
What to do
Let
decode()construct through a path that skips validation, whilekeeping strict validation for user-supplied values. Strictness on
construction is a deliberate differentiator and must not be weakened —
the only claim here is that the decoder needn't re-check its own
output.
Options worth weighing: a private
_unvalidated()classmethod usingobject.__new__plusobject.__setattr__; or a module-privatesentinel that
__post_init__checks. The first avoids a per-instancebranch; the second is less invasive. Whichever is chosen, it must not
appear in the public API.
Acceptance criteria
Ethernet(dst="nonsense", ...)still raisesInvalidMACAddressError.Ethernet.decode(...)performs no regex matching.