Order multipart parser states by frequency in the dispatch ladder#303
Closed
Kludex wants to merge 1 commit into
Closed
Order multipart parser states by frequency in the dispatch ladder#303Kludex wants to merge 1 commit into
Kludex wants to merge 1 commit into
Conversation
The PART_DATA state machine walked its `if/elif state == ...` ladder in lifecycle order, so the hottest states (PART_DATA, the header states) sat near the bottom and paid a failed comparison for every state above them - ~84 state comparisons per part for a 100-field form. Reorder the branches by how often each state is actually active: boundary and part-data first, the once-per-stream START/END states last. The branches are mutually exclusive on `state`, so this is a pure reorder with no behavior change - verified byte-identical to the prior parser across ~135k differential comparisons (every chunk-split strategy incl. byte-by-byte and boundary-edge sweeps, crossed with callback subsets). large ~9% faster (400 to 363 us), simple ~7%, worstcase_crlf ~10%; file upload unchanged.
There was a problem hiding this comment.
No issues found across 1 file
Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more
Re-trigger cubic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The PART_DATA state machine walked its
if/elif state == ...ladder in lifecycle order, so the hottest states (PART_DATA, the header states) sat near the bottom and paid a failed comparison for every state above them - ~84state ==comparisons per part for a 100-field form.Reorder the branches by how often each state is actually active (measured): boundary and part-data states first, the once-per-stream
START/ENDstates last. The branches are mutually exclusive onstate, so this is a pure reorder - no branch body changes, no behavior change.Impact
This targets the per-part Python-level work that
simple/largeare bound by - the cases where this parser trailed the others - without touching the bulk paths where it already leads.Correctness
Verified byte-identical to the prior parser across ~135k differential comparisons of the full callback-event stream and error type/offset: every chunk-split strategy (whole, byte-by-byte, fixed-size, random) plus boundary-edge sweeps, crossed with enabled-callback subsets. Zero mismatches. 158 tests pass, 100% coverage. A content-level check confirms every non-comment line is unchanged - only the branch order and
if/elifkeywords differ.AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.