Repository navigation
perf: call field.__copy__() directly instead of copy.copy(self) - #733
Conversation
…lf) (#730) - copy.copy(self) at the 8 per-field-per-packet __call__ sites in field.py, collections.py and misc.py still has to find FieldBase.__copy__ before calling it -- getattr(cls, '__copy__', None) -- and that lookup alone profiled at 55,846 calls (~1.7% of an extract() run) on http.pcap. - copier = getattr(cls, '__copy__', None); copier(x) in copy.py is exactly x.__copy__(), so calling self.__copy__() directly removes the lookup without changing when a field is copied or what the copy contains. - Verified: NumberField/_TextField's __call__ unconditionally mutate the copy their super().__call__() returns, and OptionField.unpack mutates _option_padding on whatever __call__ returned, so skipping the copy itself (rather than just how it dispatches) was ruled out as unsafe. - Added byte-identity coverage across 17 classic-PCAP sample captures and a mechanism test proving copy.copy is no longer reached at any of the 8 sites (fails without the fix). Build: `pytest tests/corekit/` -- 199 passed, 1915 subtests. Broader protocol/integration run -- 81 passed, 1 skipped, 477 subtests. mypy/pylint/ isort clean relative to baseline.
|
✅ GOOD TO MERGE @ Cross-review ran on Opus against Sonnet-authored work and returned GOOD TO GO. Posted here by the coordinator: that reviewer was a read-only helper with no
Not a "skip the copy when callbacks are trivial" change, and the author was right to reject that: Two caveats carried forward honestly from the author rather than dropped:
✅ GOOD TO MERGE @ |
make pylint,make mypy,make isort) -- scoped to the changed files; findings are pre-existing, verified against baselinemake testpasses, and a test case covers the change -- ranpytest tests/corekit/(199 passed, 1915 subtests) plus a broader protocol/integration set (81 passed, 1 skipped, 477 subtests); did not run the full suiteWhat is the purpose of your pull request?
perf— changes performance, not behaviourDescription
copy.copy(self)at the 8 per-field-per-packet__call__sites still has to findFieldBase.__copy__before calling it:getattr(cls, '__copy__', None). That lookup alone profiled at 55,846 calls (~1.7% of anextract()run) onexamples/captures/http.pcap-- reproduced here exactly.copier = getattr(cls, '__copy__', None); copier(x)incopy.pyisx.__copy__(), so calling it directly removes the lookup without changing when a field is copied or what the copy contains.Skipping the copy itself (rather than just its dispatch) was considered and ruled out:
NumberField/_TextField.__call__unconditionally mutate the copy theirsuper().__call__()returns, andOptionField.unpackmutates_option_paddingon whatever__call__handed back, so a field with no callback can still need a fresh copy for reasons unrelated to the callback.Byte-identity verified across 17 classic-PCAP samples (1500 subtests, before and after, identical). End-to-end effect is below this host's noise floor (25-rep spread ~0.43s vs. a ~2ms expected shift); the isolated dispatch saving is real and reproducible in a tight microbenchmark (~0.58us -> ~0.42us per copy).
Fixes #730.