Skip to content

vendor(apptype): emit TransportProtocol.tcp instead of TransportProtocol.get(...) in 23,938 places #768

Description

@JarryShaw

Is your feature request related to a problem? Please describe.

The generated registries call TransportProtocol.get('tcp') to name a constant. There are 23,938
such calls across pcapkit/const/reg/apptype/, every one executed at class-definition time, i.e. on
every import of pcapkit. get() is a real function doing an isinstance check, key.lower()
twice, a __members__ membership test and a __getitem__ — to reach a member that plain attribute
access returns in one lookup.

Describe the solution you'd like

Emit TransportProtocol.tcp instead of TransportProtocol.get('tcp'). One line, in
pcapkit/vendor/reg/apptype/apptype.py:573-574:

return ' | '.join(f'TransportProtocol.get({proto!r})' for proto in sorted(protos, key=order.index))

Measured, on this checkout:

T.get('tcp') is T.tcp  ->  True        (same for udp / sctp / dccp / undefined)
per call:   get() 524 ns   attribute 24 ns   ->  21.4x
23,938 sites:  12.5 ms  ->  0.6 ms    ~12 ms saved per import
generated size: ~187 KB smaller (29 chars -> 21 per site)

Safe because the generator only ever emits the five declared names (TRANSPORTS + ('undefined',)),
so get()'s extend_enum fallback for an unknown name cannot fire in generated code. get() itself
stays — AppType.get(..., proto=...) and other callers need it.

Describe alternatives you've considered

Leaving it: the cost is small in absolute terms. But it is pure waste on every import, and the
explicit form also reads better — cls.__transport__ is TransportProtocol.tcp says plainly that it
is an identity comparison against a canonical member, which is the thing #732 wants to make safe.

Additional context

Note the members are lowercase: TransportProtocol.TCP raises AttributeError, since only
get() lowercases. Requested by the maintainer. Should land together with #744's one-line change to
:717 in the same file — both are mechanical and one regeneration covers both, where separate PRs
would produce conflicting ~12,000-line diffs over the same generated files.

Activity

  1. added
    enhancementIssues requesting a new capability (set by the feature request template)
    perfPull requests that improve performance (perf: subject prefix)
    wipWork in flight - a covering PR is open or an agent is actively on it
    on Sep 25, 2026
  2. JarryShaw commented on Sep 25, 2026

    @JarryShaw
    OwnerAuthor

    Two corrections to the figures above, both mine.

    The saving is 7 bytes per site, not 8. TransportProtocol.get('tcp') is 28 characters, not 29:

    TransportProtocol.get('tcp') = 28 chars
    TransportProtocol.tcp        = 21 chars
    saving per site              = 7 bytes
    

    So the "~187 KB" above is wrong. Measured on the actual regeneration in #769: the .get(→attribute change alone removes 167,587 bytes (163.7 KiB), and the whole diff — including the dropped per-member annotations and five pragmas — removes 254,615 bytes (248.6 KiB). The accounting closes exactly: 190 (5 pragmas) + 86,838 (12,391 annotations) + 167,587 (7 × 23,941 sites).

    And the site count is 23,941 rewritten, not 23,938. The 23,942nd TransportProtocol.get( in the tree is _dispatch's TransportProtocol.get(proto.lower()), which resolves a caller-supplied string at runtime and is deliberately kept — so 23,941 were rewritten and exactly 1 survives, which is what #769 asserts.

  3. added a commit that references this issue on Sep 25, 2026
    9ae0e29
  4. removed
    wipWork in flight - a covering PR is open or an agent is actively on it
    on Sep 25, 2026
  5. added this to the 1.5 milestone on Oct 6, 2026
  6. moved this to Done in PyPCAPKiton Oct 6, 2026
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

    enhancementIssues requesting a new capability (set by the feature request template)perfPull requests that improve performance (perf: subject prefix)

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions