Repository navigation
Simplify _tokenize.py with no reading moved - #1527
Merged
Merged
Conversation
Release checklist step 0's pass over tokenize, quality only: - Delete `_BIDI`, which no library code has read since the scan moved to the classes built from `_BIDI_CHARS` (#1436); its test builds the class from `_BIDI_CHARS` and its regex-sync roster row goes with it. - Record a comma's or a transcription dot's offset in the branch that decided it, rather than asking the character's class a second time. - Skip the ambiguity-origin generator when there is no ambiguity: one frame fewer on every parse without one (195.72 against 196.72 on py3.11, inside the band). - Point the bisect comment at its scaling row and decisions entry instead of carrying its measurement, and the `\s` claim at the test that holds it. - Correct two docstrings still saying only the interpunct marks a transcription: the fullwidth full stop does too since #1312. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sions/parse-cost The bisect comment's figures move here, re-measured, with the scaling row that guards the resolution, the one frame the pass saves, and the token comparison that shows nothing moved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Comments: the T2 and T3 citations say which clause the code implements; the emoji note no longer claims the bidi marks are integers; _stripped and _flank say which characters are transparent and name the reader; the extracted-region note says a transcription dot still divides there; the bisect note counts stray closers, not delimiter characters; a 115-column docstring line is rewrapped. - Pin the saved generator frame: the ±2% band cannot see it return, so test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one holds it at no call on three names without a report, with a recorded control of one call each on af58ca0 (checked by running the test against master's tokenize). - decisions/parse-cost: the entry carries #1527; the old 180ms is attributed to the two quadratics 81ea9c1 fixed together; the clock and the linear loop are written out; the unmatched_closes row's own ratio with the loop planted is recorded (12.53x and 11.95x against 6.0); per-name counts are re-taken by a stated recipe under both orders, with 3.15's reference row; the comparison grid's pieces and composition are listed so it can be rebuilt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test review mutated the bisect (bisect_left, a missing or doubled `- 1`) and the resolution's gate, and test_tokenize.py and the case table passed each mutant: only test_extract.py and test_script_segment.py held them. Four rows put the stray character at each end of a token and at each end of the name; each mutant fails three or four of them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The grid's jamo piece is ga, not the syllable before it; the two orders' counts went through different entry points, one frame apart; the comparator sentence now says what it runs over. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Release checklist step 0: a /simplify pass over
nameparser/_pipeline/_tokenize.py, which changed 185 lines since v3.1.0 (#1436's scan rewrite). This is quality only, with no reading moved. The diff touches no other library file: no_vocab.py,_pieces.py,_state.py,_group.py,_classify.pyor_suggest.py, and no pattern shared with render.Changes
_BIDIdeleted. No library code has read it since Parse cost could fall by about 18% with no reading moved #1436 moved the scan onto the classes built from_BIDI_CHARS.+off_BIDI.patternto rebuild the class, and the class's row intest_regex_sync.py's roster._BIDI_CHARS, and the row goes with the pattern, whichtest_every_declared_pattern_still_existsrequires.any(). The generator was entered on every parse, even with no ambiguity. The change is one frame fewer on such a parse, inside the band. A new test pins it (below).\s/isspace()claim names the test that holds it over every code point.台邦.撒沙勒gives one given name) #1312._flank's "stripped invisibles" read as if it covered_INVISIBLE_SEPARATORS, which are not stripped. The extracted-region note said a dot there is "a mere separator"; it still divides only between classified flanks. The emoji note claimed the bidi marks were integers too.test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one(test_benchmark.py) pins the saved generator frame at no call on three names without a report. Its recorded control is one call each on af58ca0, checked by running the test against master's tokenize.test_a_stray_delimiters_report_points_at_the_token_holding_it(test_tokenize.py) puts a stray character at each end of a token and of the name. The test review mutated the bisect (bisect_left, a missing or doubled- 1) and the resolution's gate.test_tokenize.pyand the case table passed every one of those mutants; onlytest_extract.pyandtest_script_segment.pyheld them. Each mutant fails three or four of the new rows.No test expectation was edited.
Proof that nothing moved
Snapshot and suite:
tools/differential/snapshot.jsonlis byte-identical, and nothing was re-recorded.mypyandruff checkare clean.Tokens and spans, compared across both trees. The inputs were every distinct text of the committed
tools/differential/*.jsonlfiles, plus a grid of one to five pieces. The pieces were Han, Hangul (composed and in conjoining jamo), Mongolian and Latin words, both transcription dots, U+202F, the bidi marks, an emoji, the commas, the name dots, the invisible separators and the delimiters.strip_bidi/strip_emojisettings.reprand tokens.Local corpora.
measure.py --baseagainst an export of af58ca0 ran 30 steps, and every one reports no moves. The steps were the committed snapshot, CLDR and GND agreement, 18 local snapshots (ctgov ×2, cz, dof, fec, govuk, jrc, my, nepal, parliaments, orcid, patentsview, psc, tse, tw, uit, ukec, vn), six field-agreement scores and four cross-script "alike" checks. It ran through a scratchNAMEPARSER_TOOLS_CACHEof links to the corpus files, so nothing shared was written or re-recorded.Reads line.
uv run python tools/stage_reads.py --stage tokenizereportsPolicy.strip_bidiandPolicy.strip_emoji, which is what theReads:line says.Serial and timed rows.
uv run pytest -n0 -m serialpasses 39 of 39 on both trees (py3.11, at a load average of 200–340 from parallel sessions; the timed rows read CPU time). The scaling rows tokenize's work reaches, read with the tests' own_ratioat_BASE800 → 3,200, twice on each tree, give ratios for 4x the input against_MAX_RATIO6.0, with no side consistently higher:| row | master af58ca0 | this PR |
|---|---|---|
|
plain_tokens| 4.75, 4.43 | 4.56, 4.04 ||
commas| 4.45, 3.99 | 4.70, 4.67 ||
unmatched_closes| 4.24, 4.17 | 4.71, 4.48 ||
unmatched_opens| 4.74, 4.45 | 4.47, 4.78 ||
delimiter_pairs| 4.15, 4.41 | 4.79, 4.48 ||
quote_pairs| 4.42, 4.80 | 4.74, 4.30 |The new tests also pass on 3.15.
Frames per parse
These are
sys.setprofile"call" events fromtools/perf/call_count.py, with each tree loaded by path and its__file__checked._CALL_BASELINEPer name, on py3.11 (default order /
FAMILY_FIRST):John SmithSmith, JohnJohn (Jack) Smith王·小明A name holding a report, such as
John "Jack ) Smith, is unchanged. Every row stays inside the band, so there is no release-log bullet.Behavior-changing candidates
None was raised. Each reviewer was asked for them, and every finding was behaviour-preserving.
Considered and left alone
Spanbuilt withtuple.__new__(efficiency). This would save one frame per token, about −6 on the reference row, outside the band. It is a hand copy of NamedTuple's generated constructor at two sites, and it would need_CALL_BASELINEre-taken on every interpreter.WorkTokenbuilt withobject.__new__(efficiency). One frame per token. It would hand-copy the dataclass init, silently bypassing any later__post_init__. The right place would be a constructor helper in_state, which is outside this pass._STRIPPED,_IGNORABLEand_BOUNDARY(altitude). The patterns would be byte-identical, and so would the readers' cost. It is a reshuffle with no reader simplified._strippedinto_flank(altitude). It saves a frame per flank character, on dot input only._strippedis the reader the class test asserts on directly._extract._opens_a_listed_particle, a loop over the characters that calls_ignorablefor each one. It belongs in a pass over_extract.py, outside this one._EMOJI_CHARSwith_policy._ranges_pattern(reuse). The match set is identical, but it needs a body-only helper split out of_policy, a shared helper outside this pass._INTERPUNCT, an arithmetic form of_mongolian_letter, and skipping the sorts of offsets and tokens that are already sorted. These are untouched code or cheap defensive guarantees.offset < tokens[i].span.end). The test review found no input that reaches it: a 60,000-string fuzz and hand probes of masked, extracted and custom-delimiter shapes. Extract never reports a delimiter inside a masked region. It stays as a free defensive guard. A test would need a hand-builtParseState.Follow-ups for other passes
_extract._overlaps' docstring still carries an inline measurement ("400 pairs spent 5.4ms here, against 2.5ms before the sweep existed"), the class of figure this pass moved out of tokenize. It is worth taking to decisions/parse-cost in an extract pass._vocab.py:107-112still cites "the _EMOJI_RANGES precedent in _tokenize.py". It is accurate, but it is history about tokenize living in another module, and_vocab.pybelongs to another worker this cycle.Reviews
/simplify, four reviewers (reuse, simplification, efficiency, altitude). The findings applied are listed under Changes, and the rest under Considered.code-reviewer: no defects. Each change was argued equivalent on every input. One long docstring line was fixed.comment-analyzer: no errors. It sent the improvements listed under Changes, plus the T2/T3 clause lines and the bisect's "per stray closer". It also found the_extract._overlapsfollow-up above.pr-test-analyzer: mutated eleven sites in scratch copies. Every behavioural mutant fails the full suite. The bisect and gate mutants passedtest_tokenize.pyand the case table, hence the new rows. The end-check mutants survive everything (see Considered).design-docs-revieweron the parse-cost entry found six problems, all fixed in 5eec819:FAMILY_FIRST" was false;unmatched_closesat its own 800 → 3,200, 4.21x and 4.35x on the change against 12.53x and 11.95x with a linear scan planted, against_MAX_RATIO6.0.type-design-analyzer(no type changed),silent-failure-hunter(no error path touched) andcode-simplifier(the/simplifypass is this PR).code-revieweron 5eec819 and dacacc9: no findings. An AST comparison with docstrings blanked shows the two commits change only comments and docstrings in_tokenize.py. The new frame pin fails on master's tokenize and cannot pass vacuously.design-docs-reviewer, second round: all six fixes confirmed, and every figure re-taken. It found three new details, fixed in e402171:🤖 Generated with Claude Code