Skip to content

Stop re-validating addresses the decoder just generated #84

Description

@EONRaider

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:

  1. bytes_to_mac() turns six raw bytes into "00:07:0d:af:f4:54".
  2. __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

  • Ethernet(dst="nonsense", ...) still raises
    InvalidMACAddressError.
  • Ethernet.decode(...) performs no regex matching.
  • Round-trip and corpus tests unchanged and green.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions