Skip to content

Hoist protocol dispatch into module-level tables #82

Description

@EONRaider

Target: 1.4.0 (Tier 1 — earn the benchmark)

_ethertype_class() (layer2/ethernet.py:21) and
_ip_protocol_class() (layer3/ip.py:48) both run their deferred
imports and rebuild a dict literal on every single call. They are
called once per layer per frame, so this is the hottest path in the
library.

Profiling a corpus walk puts _ip_protocol_class at the top by
tottime, with _ethertype_class third.

Measured

Against a module-level dict built once:

Function Speedup
_ip_protocol_class 91×
_ethertype_class 42×

What to do

Build each table once at import (lazily on first use, to preserve the
existing import-cycle avoidance) and reduce the call to a dict lookup.
The ipv6= gating of _IPV6_ONLY_NUMBERS must be preserved — an IPv4
packet with protocol=0 must still not decode a Hop-by-Hop layer.

This is the same change as the public registry in #87 (Tier 2).
Doing it as a plain table now and generalising it into the registry
later is fine, but the two should not be designed in isolation.

Acceptance criteria

  • Dispatch is a lookup, not a construction, on the hot path.
  • The IPv6-only gating behaviour is unchanged and still tested.
  • Full suite green.

Part of #103.

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