From 2cfe3a8aec532176b15b0f1893fc60074f92089e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 22 Nov 2026 00:26:36 -0800 Subject: [PATCH 1/5] Simplify _tokenize.py with no reading moved 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 --- nameparser/_pipeline/_tokenize.py | 58 ++++++++++++++---------------- tests/v2/pipeline/test_tokenize.py | 14 ++++---- tests/v2/test_regex_sync.py | 1 - 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/nameparser/_pipeline/_tokenize.py b/nameparser/_pipeline/_tokenize.py index 96316742b..72bee2243 100644 --- a/nameparser/_pipeline/_tokenize.py +++ b/nameparser/_pipeline/_tokenize.py @@ -46,16 +46,15 @@ _EMOJI_RANGES = ((0x1F300, 0x1F64F), (0x1F680, 0x1F6FF), (0x2600, 0x26FF), (0x2700, 0x27BF)) _BIDI_CHARS = '\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069' -_BIDI = re.compile(f'[{_BIDI_CHARS}]+') -# The same ranges as a character class, for the classes below +# The emoji ranges as a character class, for the classes below _EMOJI_CHARS = ''.join(f'{chr(lo)}-{chr(hi)}' for lo, hi in _EMOJI_RANGES) # rules/T2: "the katakana middle dot, its halfwidth twin and the # Ethiopic wordspace divide a name like whitespace, always." They # record no offset: the nakaguro also divides kanji roster pairs # (高橋・一郎, read family-first by the script license, #272), so it is -# not a transcription marker; only the Chinese 间隔号 is (#298). The -# Ethiopic wordspace U+1361 joined them (decisions/T2, #1202). +# not a transcription marker; _TRANSCRIPTION_DOTS below are +# (decisions/T2 and T3, #298, #1202, #1312). _NAME_DOT_SEPARATORS = frozenset({"\u30FB", "\uFF65", "\u1361"}) # rules/T6: "a narrow no-break space between two Mongolian letters @@ -105,11 +104,11 @@ def _chars(chars: frozenset[str]) -> str: # match against them, and the boundary pattern _tokenize_region scans # with is the ignorable class plus the characters that separate only by # context (the commas, which record an offset, and the transcription -# dots, whose flanks decide). One ignorable character is context too: -# U+202F, whitespace, which rules/T6 has join two Mongolian letters, so -# the scan's three branches (comma, U+202F, dot) are the whole of -# context. `\s` matches exactly the characters str.isspace() accepts -# (checked over every code point). +# dots, whose flanks decide); U+202F is whitespace, ignorable, save +# where rules/T6 has it join two Mongolian letters. `\s` matches +# exactly what str.isspace() accepts, which +# test_the_separator_classes_hold_their_parts_over_every_code_point +# holds over every code point. _ALWAYS = r'\s' + _chars(_NAME_DOT_SEPARATORS | _INVISIBLE_SEPARATORS) _SWITCHES = ((False, False), (False, True), (True, False), (True, True)) @@ -139,14 +138,12 @@ def _mongolian_letter(ch: str) -> bool: def _stripped(ch: str, policy: Policy) -> bool: - """True when the strip policy removes `ch` from the token stream: - the reader _flank asks of the set _strip_class defines, which also - feeds _IGNORABLE and _BOUNDARY. A character the strip policy removes - must not occupy a flank position either, so a new strip class added - to _strip_class stays transparent to the interpunct guard by - construction. The separators - (whitespace, the name dots and _INVISIBLE_SEPARATORS) are not in - this set and do occupy one.""" + """True when the strip policy removes `ch` from the token stream, + the set _strip_class defines for _IGNORABLE and _BOUNDARY too, so a + stripped character never occupies a flank and a new strip class is + transparent to the transcription-dot guard by construction. The + separators (whitespace, the name dots and _INVISIBLE_SEPARATORS) + are not stripped and do occupy one.""" pattern = _STRIPPED[policy.strip_bidi, policy.strip_emoji] return pattern is not None and pattern.match(ch) is not None @@ -168,9 +165,8 @@ def _flank(text: str, indices: range, policy: Policy) -> str | None: def _ignorable(ch: str, state: ParseState) -> bool: """True when `ch` separates the words beside it: whitespace, the name dots, _INVISIBLE_SEPARATORS (all unconditional, not - policy-gated: commas RECORD an offset and these must not, and only - the 间隔号 marks a transcription, #298), and what the strip policy - removes. The one exception is decided in _tokenize_region, by + policy-gated, and recording no offset, which the commas and the + transcription dots do), and what the strip policy removes. The one exception is decided in _tokenize_region, by context: U+202F between two Mongolian letters joins them (rules/T6).""" policy = state.policy return _IGNORABLE[policy.strip_bidi, policy.strip_emoji].match( @@ -199,6 +195,7 @@ def _tokenize_region(state: ParseState, start: int, end: int, # #653) if not record_offsets: continue + commas.append(i) elif ch == _NARROW_NBSP: if (start < i < end - 1 and _mongolian_letter(text[i - 1]) and _mongolian_letter(text[i + 1])): @@ -218,15 +215,12 @@ def _tokenize_region(state: ParseState, start: int, end: int, right = _flank(text, range(i + 1, end), policy) if right is None or not _classified_char(right): continue + if record_offsets: + interpuncts.append(i) if tok_start < i: tokens.append(WorkToken(text[tok_start:i], Span(tok_start, i), role=role)) tok_start = i + 1 - if record_offsets: - if ch in COMMA_CHARS: - commas.append(i) - elif ch in _TRANSCRIPTION_DOTS: - interpuncts.append(i) if tok_start < end: tokens.append(WorkToken(text[tok_start:end], Span(tok_start, end), role=role)) @@ -261,14 +255,14 @@ def tokenize(state: ParseState) -> ParseState: # kind's contract already allows. # Bisect rather than rescan: the closer sweep can emit one # ambiguity per delimiter character, so a linear scan per ambiguity - # is quadratic on pathological input (') ' * 1600 spent 180ms here, - # against 9ms before the sweep existed). Spans are non-overlapping - # and sorted just above, so the candidate is the last token whose - # start is <= the offset. - # Nothing to resolve on the overwhelmingly common no-ambiguity - # parse, and building starts + the closure is not free. + # is quadratic (test_benchmark.py's `unmatched_closes` row; + # decisions/parse-cost, 2026-11-22). Spans are non-overlapping and + # sorted just above, so the candidate is the last token whose start + # is <= the offset. + # Nothing to resolve on the common parse with no ambiguity, which + # the empty-tuple test passes without the generator's frame. ambiguities = state.ambiguities - if any(a.origin is not None for a in ambiguities): + if ambiguities and any(a.origin is not None for a in ambiguities): starts = [t.span.start for t in tokens] def _containing(offset: int) -> tuple[int, ...]: diff --git a/tests/v2/pipeline/test_tokenize.py b/tests/v2/pipeline/test_tokenize.py index 1afb50843..e3782af44 100644 --- a/tests/v2/pipeline/test_tokenize.py +++ b/tests/v2/pipeline/test_tokenize.py @@ -100,16 +100,16 @@ def test_the_separator_classes_hold_their_parts_over_every_code_point() -> None: # #1436: the ignorable set is a regex class built from its parts, so # a region is scanned in C; held here, over every code point, to the # parts as their own predicates state them (str.isspace, the two - # sets, the emoji ranges, _BIDI), for each setting of the two strip - # switches, and the boundary class to the ignorable one plus the - # commas and transcription dots. Each class is matched whole, in C. - # It guards how the classes are ASSEMBLED, not which characters - # belong: _BIDI is compiled from the same string the classes are, - # so a mark dropped from it passes here. + # sets, the emoji ranges, _BIDI_CHARS), for each setting of the two + # strip switches, and the boundary class to the ignorable one plus + # the commas and transcription dots. Each class is matched whole, in + # C. It guards how the classes are ASSEMBLED, not which characters + # belong: the classes are built from _BIDI_CHARS too, so a mark + # dropped from it passes here. from nameparser._pipeline._state import COMMA_CHARS everything = "".join(map(chr, range(0x110000))) space = {c for c, ch in enumerate(everything) if ch.isspace()} - bidi = {m.start() for m in re.finditer(_tokenize._BIDI.pattern[:-1], + bidi = {m.start() for m in re.finditer(f"[{_tokenize._BIDI_CHARS}]", everything)} emoji = {c for lo, hi in _tokenize._EMOJI_RANGES for c in range(lo, hi + 1)} diff --git a/tests/v2/test_regex_sync.py b/tests/v2/test_regex_sync.py index 08d432205..b9fc320a6 100644 --- a/tests/v2/test_regex_sync.py +++ b/tests/v2/test_regex_sync.py @@ -176,7 +176,6 @@ def test_dotted_initial_is_the_period_alternative_of_initial() -> None: # #542: the name run after an Mc prefix, read on the word as # written once _MC has matched its composed spelling ("_render", "_NAME_RUN"): None, - ("_tokenize", "_BIDI"): None, # Mirrors _pipeline._state.COMMA_CHARS, a set rather than a # pattern -- pinned by its own test below ("_render", "_COMMA_CHAR"): None, From 06b36b0572102054b73271f64d37ce9597bcb8fe Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 22 Nov 2026 00:27:17 -0800 Subject: [PATCH 2/5] Record the tokenize pass's frame and the bisect's measurement in decisions/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 --- docs/design/decisions/parse-cost.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/design/decisions/parse-cost.md b/docs/design/decisions/parse-cost.md index 60359116b..02850f5a7 100644 --- a/docs/design/decisions/parse-cost.md +++ b/docs/design/decisions/parse-cost.md @@ -50,3 +50,4 @@ Open: none in this file. - 2026-11-11 #1352 — THE TIMED ROWS READ CPU TIME, AND THE COUNTS RUN IN PARALLEL; NO ROW, BOUND OR BASELINE MOVES. On a laptop running several agent sessions the wall-clock rows failed at random and passed alone. Measured on py3.11, 12 cores (6 performance, 6 efficiency), five `uv run pytest -n0 -m serial tests/v2/test_benchmark.py` runs beside 24 shell busy loops (load average 37-48): 20, 23, 21, 19 and 23 of the module's 62 tests failed on 970b54e5, at up to 10.0x against `_MAX_RATIO` 6.0; on the change, 0 of 34 in each of five runs at 24 busy loops (load 50-62) and at none, and in five at 12, which barely loaded the machine (the before tree failed none there either). Per row, three runs at 24 busy loops (load 18-75): `time.perf_counter()` read 27 of the 33 ratio rows at 6.0 or over at least once, up to 14.0x; `time.thread_time()`, measured back to back, none over 5.76x (as high as 5.76x with no busy loops too). CPU time is not load-immune -- interleaved best-of-7 read a clean row up to 5.6x, the thread charged for a slower core or a colder cache, and a user-interactive QoS hint did not help -- and noise can DEFLATE a ratio as well as inflate it: with more rounds taken only at or over the bound, a planted quadratic reading 6.7x quiet (`given_part_titles`) read 5.93x once at 24 busy loops and passed, and a loop stopping at its first reading under the bound could stop on a dip (both found by #1352's review). So `_ratio` interleaves the two inputs and, where its first 7 rounds read at or over 80% of the bound (`_NEAR`; `bound * _NEAR` is 4.800000000000001 in floating point), takes all 35 and judges the last minimums, each round only moving the two minimums toward the true costs; `test_ratio_takes_more_rounds_only_near_the_bound` holds that on a scripted clock, and fails against both earlier loops. Loaded, the serial pass takes 78-115s against 47s, the escalation's price; quiet it takes 18s, as before. The defects it guards are seen as before. Quiet, the old wall-clock `_best` and `_ratio` fail the same rows in every run under a planted C-level quadratic in the word count, every row within 0.35x. The recorded controls read on `_ratio`, quiet then at 24 busy loops: `link_run` 13.9-14.1 and 14.9-16.4 on b9ed1429; `given_part_run` 8.0 and 10.0-10.3 and `given_part_titles` 10.1 and 11.1-12.5 on 003b4962; `title_chain` 12.0 and 13.5-14.8, `pair_run` 10.0 and 10.2-12.8, `caps_pairs` 10.1-10.2 and 12.2-12.8 and `delimiter_cores` 10.4 and 11.8-13.1 on 9dd271b2; `connective_between_particles` 8.5 and 8.9-10.2 on 5752df69 (#939 before its review); and `slash_chain` 8.8-8.9 and 8.9-9.2 with #1103's per-step copy planted. That is against 3.97-4.19 quiet and up to 5.06 at 24 busy loops on 970b54e5. The weakest planted quadratic, 6.3-7.0x quiet on eleven rows, read 7.18x or more on every row in five runs at 24 busy loops. A planted backtracking regex reads 7.31 s of CPU over the thousand names, against 7.36 s wall. The 5s bound is CPU seconds now and no looser for work on the parse's own thread, CPU time being at most the wall time of a single-threaded parse; it no longer sees time spent blocked or on another thread, which a parse never spends. 962301f7's memory move reads 4.07-4.43 on the CPU clock and 4.07-4.50 on the wall clock, quiet, as the 2026-11-03 #1036 entry found for the wall clock; `_ShiftCountingList` holds it. The 28 untimed tests (the frame, call and shift counts and the table checks) lose the `serial` mark. Each passes alone in a fresh process, after the 21,065 tests of `test_cases.py` and `test_match.py` in one, in three full parallel runs, and under `--cov` on 3.11 and 3.14. The order dependence of an absolute count (the 2026-11-09 #1170 entry) stays inside a ratio, a band, or an exact count of one named function's calls (`only=`/`caller=`, `== 0` up to `== 6`), none of which counts an `lru_cache`'d function. - 2026-11-18 #1435 — NO ROW MOVES; A RUN OF INITIALS BEHIND A FULL NAME ASKS NO COUNT. #1238's C16 block in `_comma.decide` put a dotted run spelling a listed credential, every letter a capital or the name in one case, into `maybe`, so `John Smith, M.D.` paid C4's `name_word_count` and the C11 books, which only a one-word head reads the run by. Where the run is the part's only entry in `maybe` (no word of the class and no caps-shape candidate beside it), `decide` now asks `_lone_word` first (once, its answer kept for the reading), and behind a head that is not one word skips the count and the books: the run stays in `maybe`, so the part is still read as the count's part and never takes the listing form's early return, and with nothing else in `maybe` the count licenses nothing and `head_named` meets no member. Frames, `test_benchmark._frames_for` for one parse in a fresh process, master (b78d5af2) then branch: `John Smith, M.D.` 221 → 199 on 3.11 and 203 → 185 on 3.15; `John Smith, MD` 190 and 178 and `Smith, John` 136 and 125 unchanged; `Smith, J.P.`, which still counts, 233 and 218 unchanged. The issue's table (209 for `John Smith, M.D.` on master) was taken in a process that had parsed other names first, which moves the absolute count and not the difference (the 2026-11-09 #1170 entry). The issue's proposed gate (append the run only beside another class word or behind a one-word head) was not taken: it sends such a part down the no-`maybe` path, equal only where the listing early return and C1's fallback agree, which nothing holds by construction. Checked against master: a grid of 215,280 parses (65 heads, from empty, nickname, title, particle and title-particle heads alone and in pairs to full and one-case names, × 46 tails of dotted runs, listed and ambiguous credentials, paired initials and their company × three comma spellings and a later part × the three orders × six policies) byte-identical, the issue's gate also identical there, and the harness shown to see a move (on the same grid, a `_lone_word` always true moves 34,848 rows, and skipping the count behind a one-word head too moves 4,536, `Smith van, M.D.` reading given 'M.', middle 'D.', family 'Smith van' where master reads given 'Smith', family 'van', suffix 'M.D.', `_lone_word` reading that head as one word while the count finds two); `snapshot.jsonl` byte-identical; and `snapshot.py --local ctgov-officials` and `ctgov-contacts` against master, no move. `test_a_run_of_initials_behind_a_full_name_takes_no_count` holds the saving, its control recorded in its docstring, and `test_the_count_c16_skips_decides_no_reading` (tests/v2/pipeline/test_comma.py) the readings a wrong gate moves. Under each of three mutants (the gate removed; the count skipped behind a one-word head too; `run_alone` forced true, so a run beside another entry of `maybe` such as `John Smith, M.D. M.A` skips the count as well) the frame test fails, and under the last two the reading test does too. A run beside `MA` (`John Smith, M.D. MA`) never joins `maybe`, `MA` being the run's company (C16), so it reaches none of this. - 2026-11-18 #1436 — EVERY ROW AND THE LINK PIN MOVE DOWN: `parse` 285 → 197 on 3.11 and 277 → 188 on 3.12 to 3.15, `_LINK_BASELINE` 2,418 → 1,716 and 2,398 → 1,706. Measured 2026-11-18 on 688991e7 (#1458's merge, the tree before the change) and on the change rebased onto it, each interpreter on its own (`uv run python tools/perf/call_count.py`, `--against 688991e7` on 3.11, and with `UV_PROJECT_ENVIRONMENT= uv run --python 3.1x` for 3.12 to 3.15): 285.72 → 197.72 on 3.11 and 277.72 → 188.72 on each of 3.12 to 3.15, a fall of 31% and 32%; the link run `_name_link_run(64)`, `_frames_for` in `tests/v2/test_benchmark.py`, 2,404 → 1,716 on 3.11 and 2,395 → 1,706 on 3.12 to 3.15. #1434's simplify pass had measured scratch patches at about 234 on 3.11 and left them for a deliberate move. The changes, each a commit of its own, py3.11 reference row: tokenize scans a region for its separators with a compiled class per setting of the two strip switches rather than calling `_ignorable` on every character (−27), the ignorable set now defined by those classes, which `_ignorable` and `_stripped` match, and held to its parts over every code point by `test_the_separator_classes_hold_their_parts_over_every_code_point`; tokenize's sort key an `attrgetter` (−6); classify's tokens and group's frozen pieces built without a generator (−6, −10); group's tail start read by identity rather than from a dict keyed by the enum, whose `__hash__` is Python (−3); and `Token.__post_init__` paying nothing on the parse's own tokens, its span checked without a generator and an exact `Span` kept, its frozenset tags spared the str and `Mapping` refusals, its role coerced only when it is not a `Role` (−24, −6, −6), the checks and their messages unchanged and each refusal held by a row of `test_token_rejects_a_span_of_anything_but_two_ints` and the tests beside it. On the 64-link run, which holds 67 tokens, the same commits in order read −143, −67, −67, −6, −3 and −268, −67, −67 (`git log 688991e7..` checked out commit by commit): a cost per token or per character is a cost per link there. `_is_emoji` looped over its ranges in an earlier commit (no move on the reference row, 336 → 271 on a Devanagari name) and is gone, its ranges read through the class. Per name, `_frames_for` for one warm parse, each in a fresh process, 688991e7 then the change, 3.11 and 3.15: `John Smith` 126 → 91 and 120 → 84, `Smith, John` 136 → 99 and 125 → 87, `John Smith, M.D.` 199 → 149 and 185 → 134, `Dr. John Smith Jr.` 233 → 170 and 225 → 161, `John Smith Jr., PhD` 258 → 195 and 244 → 180, `राम बहादुर के.सी.` 336 → 193 and 326 → 182. Time, which the frames cannot see: 1000 parses of the reference name, minimum of 15, the two trees run in turn three times (py3.11, b78d5af2 and the change before the rebase), 116.9-117.4 ms → 109.2-109.9 ms of CPU, and per 1000 names, minimum of 9, `Smith, John` 64.7 → 61.0 ms, the long name `Sir John Ronald Reuel Tolkien00 van der Berg Jr., PhD` 176.2 → 165.1 ms, the Devanagari name 105.9 → 92.9 ms, `高橋一郎` 61.8 → 57.9 ms and forty double-spaced words 358.4 → 320.3 ms: no frame was traded for slower C work. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical; `tools/differential/measure.py --base` against b78d5af2 before the rebase, 29 steps with no move; and, across two trees each asserting its `nameparser.__file__`, b78d5af2 against the change before the rebase and again 688991e7 against the change rebased onto it, a grid of 114,647 readings (NFC and NFD spellings, ZWJ and ZWNJ inside Devanagari words, an emoji ZWJ sequence, a variation selector, bidi marks and isolates, ZWSP, the interpunct, the fullwidth stop and the nakaguro between Han names, U+202F in Mongolian, in twelve comma and clause shapes under the three orders and both strip switches and `locales.ZH`, the comparator a line holding the repr, the ambiguities, `initials()`, both case views and every token's text, span, role and tags) byte-identical, the harness shown to see a move (U+200B dropped from the invisible separators moves 4,303 lines), and a differential fuzz of the old `_tokenize_region` against the new over 300,000 random texts with random region bounds, 0 differences (#1459's code review). By the four frame questions: tokenize's call and `Token`'s checks multiply with the input (question 1), every parse pays all of them (question 2), and they move the band (question 3); none inlines a predicate save the role test, `_coerce_enum`'s own first line, which returns its argument and cannot drift (question 4). The long link run keeps its margin: a per-link regression is +64 against a 2% band of 34.3. SUPERSEDES: the 2026-11-09 #1170 entry's rows, and the 2026-11-03 #1036 entry's link pin. +- 2026-11-22, release checklist step 0's pass over tokenize — NO ROW MOVES; tokenize resolves an ambiguity's character offset to its token by bisecting the token starts, and skips the work on a parse with no ambiguity. WHY THE BISECT: extract's unmatched-close sweep reports one ambiguity per stray closer, so a scan of the tokens per ambiguity is quadratic in them. The comment that held the figures until this pass read `') ' * 1600` at 180ms with a linear scan, against 9ms before the sweep existed (written with 81ea9c17, 2026-07-19). Re-measured 2026-11-22 on py3.11 (Apple silicon), best of five whole `Parser().parse` calls: 3.8ms at 400 closers, 16.1ms at 1,600 and 74.7ms at 6,400, each holding as many reports as closers, so 4.2x and 4.6x for 4x the input; with `bisect.bisect_right` swapped in that process for a Python loop over the starts, 8.3ms at 400 and 77.9ms at 1,600, 9.4x. The guard is `test_benchmark.py`'s `unmatched_closes` scaling row, whose `') '` input reaches this resolution. THE FRAME: the gate asked `any(...)` of every parse's ambiguity tuple, a generator frame even when the tuple is empty; it now tests the tuple first. Measured 2026-11-22, py3.11, one warm parse counted with a `sys.setprofile` hook, af58ca09 and the change, each tree loaded by path with `PYTHONSAFEPATH=1` and its `__file__` checked: `tools/perf/call_count.py`'s reference row 196.72 → 195.72, against the 197 row and its ±2% band; `John Smith` 90 → 89, `Smith, John` 98 → 97, `John (Jack) Smith` 144 → 143, `王·小明` 105 → 104, fifty words 1,285 → 1,284, each the same under `FAMILY_FIRST`; `John "Jack ) Smith`, which holds a report, 203 either way. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical, and the two trees read alike over every distinct text of the committed `tools/differential/*.jsonl` files (the `name` key, or the line's string) plus a grid of one to five pieces drawn from Han, Hangul (composed and in conjoining jamo), Mongolian and Latin words, the two transcription dots, U+202F, the bidi marks, an emoji, the commas, the name dots, the invisible separators and the delimiters, 90,889 texts, each under the four settings of `strip_bidi` and `strip_emoji`: the comparator a line per text and setting holding tokenize's own tokens, comma and interpunct offsets and ambiguity indices, then the whole parse's `repr` and tokens, the two files compared with `cmp`: 0 of 363,556 lines differ. The negative control, af58ca09 with tokenize recording a transcription dot's offset only at odd indices, reads 1,775. From 5eec819ad8aa420f85d39a323a5029791755868c Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 22 Nov 2026 00:38:38 -0800 Subject: [PATCH 3/5] Address the first review round of the tokenize pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 af58ca09 (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 81ea9c17 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 --- docs/design/decisions/parse-cost.md | 2 +- nameparser/_pipeline/_tokenize.py | 60 ++++++++++++++++------------- tests/v2/test_benchmark.py | 19 +++++++++ 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/docs/design/decisions/parse-cost.md b/docs/design/decisions/parse-cost.md index 02850f5a7..1d34f4594 100644 --- a/docs/design/decisions/parse-cost.md +++ b/docs/design/decisions/parse-cost.md @@ -50,4 +50,4 @@ Open: none in this file. - 2026-11-11 #1352 — THE TIMED ROWS READ CPU TIME, AND THE COUNTS RUN IN PARALLEL; NO ROW, BOUND OR BASELINE MOVES. On a laptop running several agent sessions the wall-clock rows failed at random and passed alone. Measured on py3.11, 12 cores (6 performance, 6 efficiency), five `uv run pytest -n0 -m serial tests/v2/test_benchmark.py` runs beside 24 shell busy loops (load average 37-48): 20, 23, 21, 19 and 23 of the module's 62 tests failed on 970b54e5, at up to 10.0x against `_MAX_RATIO` 6.0; on the change, 0 of 34 in each of five runs at 24 busy loops (load 50-62) and at none, and in five at 12, which barely loaded the machine (the before tree failed none there either). Per row, three runs at 24 busy loops (load 18-75): `time.perf_counter()` read 27 of the 33 ratio rows at 6.0 or over at least once, up to 14.0x; `time.thread_time()`, measured back to back, none over 5.76x (as high as 5.76x with no busy loops too). CPU time is not load-immune -- interleaved best-of-7 read a clean row up to 5.6x, the thread charged for a slower core or a colder cache, and a user-interactive QoS hint did not help -- and noise can DEFLATE a ratio as well as inflate it: with more rounds taken only at or over the bound, a planted quadratic reading 6.7x quiet (`given_part_titles`) read 5.93x once at 24 busy loops and passed, and a loop stopping at its first reading under the bound could stop on a dip (both found by #1352's review). So `_ratio` interleaves the two inputs and, where its first 7 rounds read at or over 80% of the bound (`_NEAR`; `bound * _NEAR` is 4.800000000000001 in floating point), takes all 35 and judges the last minimums, each round only moving the two minimums toward the true costs; `test_ratio_takes_more_rounds_only_near_the_bound` holds that on a scripted clock, and fails against both earlier loops. Loaded, the serial pass takes 78-115s against 47s, the escalation's price; quiet it takes 18s, as before. The defects it guards are seen as before. Quiet, the old wall-clock `_best` and `_ratio` fail the same rows in every run under a planted C-level quadratic in the word count, every row within 0.35x. The recorded controls read on `_ratio`, quiet then at 24 busy loops: `link_run` 13.9-14.1 and 14.9-16.4 on b9ed1429; `given_part_run` 8.0 and 10.0-10.3 and `given_part_titles` 10.1 and 11.1-12.5 on 003b4962; `title_chain` 12.0 and 13.5-14.8, `pair_run` 10.0 and 10.2-12.8, `caps_pairs` 10.1-10.2 and 12.2-12.8 and `delimiter_cores` 10.4 and 11.8-13.1 on 9dd271b2; `connective_between_particles` 8.5 and 8.9-10.2 on 5752df69 (#939 before its review); and `slash_chain` 8.8-8.9 and 8.9-9.2 with #1103's per-step copy planted. That is against 3.97-4.19 quiet and up to 5.06 at 24 busy loops on 970b54e5. The weakest planted quadratic, 6.3-7.0x quiet on eleven rows, read 7.18x or more on every row in five runs at 24 busy loops. A planted backtracking regex reads 7.31 s of CPU over the thousand names, against 7.36 s wall. The 5s bound is CPU seconds now and no looser for work on the parse's own thread, CPU time being at most the wall time of a single-threaded parse; it no longer sees time spent blocked or on another thread, which a parse never spends. 962301f7's memory move reads 4.07-4.43 on the CPU clock and 4.07-4.50 on the wall clock, quiet, as the 2026-11-03 #1036 entry found for the wall clock; `_ShiftCountingList` holds it. The 28 untimed tests (the frame, call and shift counts and the table checks) lose the `serial` mark. Each passes alone in a fresh process, after the 21,065 tests of `test_cases.py` and `test_match.py` in one, in three full parallel runs, and under `--cov` on 3.11 and 3.14. The order dependence of an absolute count (the 2026-11-09 #1170 entry) stays inside a ratio, a band, or an exact count of one named function's calls (`only=`/`caller=`, `== 0` up to `== 6`), none of which counts an `lru_cache`'d function. - 2026-11-18 #1435 — NO ROW MOVES; A RUN OF INITIALS BEHIND A FULL NAME ASKS NO COUNT. #1238's C16 block in `_comma.decide` put a dotted run spelling a listed credential, every letter a capital or the name in one case, into `maybe`, so `John Smith, M.D.` paid C4's `name_word_count` and the C11 books, which only a one-word head reads the run by. Where the run is the part's only entry in `maybe` (no word of the class and no caps-shape candidate beside it), `decide` now asks `_lone_word` first (once, its answer kept for the reading), and behind a head that is not one word skips the count and the books: the run stays in `maybe`, so the part is still read as the count's part and never takes the listing form's early return, and with nothing else in `maybe` the count licenses nothing and `head_named` meets no member. Frames, `test_benchmark._frames_for` for one parse in a fresh process, master (b78d5af2) then branch: `John Smith, M.D.` 221 → 199 on 3.11 and 203 → 185 on 3.15; `John Smith, MD` 190 and 178 and `Smith, John` 136 and 125 unchanged; `Smith, J.P.`, which still counts, 233 and 218 unchanged. The issue's table (209 for `John Smith, M.D.` on master) was taken in a process that had parsed other names first, which moves the absolute count and not the difference (the 2026-11-09 #1170 entry). The issue's proposed gate (append the run only beside another class word or behind a one-word head) was not taken: it sends such a part down the no-`maybe` path, equal only where the listing early return and C1's fallback agree, which nothing holds by construction. Checked against master: a grid of 215,280 parses (65 heads, from empty, nickname, title, particle and title-particle heads alone and in pairs to full and one-case names, × 46 tails of dotted runs, listed and ambiguous credentials, paired initials and their company × three comma spellings and a later part × the three orders × six policies) byte-identical, the issue's gate also identical there, and the harness shown to see a move (on the same grid, a `_lone_word` always true moves 34,848 rows, and skipping the count behind a one-word head too moves 4,536, `Smith van, M.D.` reading given 'M.', middle 'D.', family 'Smith van' where master reads given 'Smith', family 'van', suffix 'M.D.', `_lone_word` reading that head as one word while the count finds two); `snapshot.jsonl` byte-identical; and `snapshot.py --local ctgov-officials` and `ctgov-contacts` against master, no move. `test_a_run_of_initials_behind_a_full_name_takes_no_count` holds the saving, its control recorded in its docstring, and `test_the_count_c16_skips_decides_no_reading` (tests/v2/pipeline/test_comma.py) the readings a wrong gate moves. Under each of three mutants (the gate removed; the count skipped behind a one-word head too; `run_alone` forced true, so a run beside another entry of `maybe` such as `John Smith, M.D. M.A` skips the count as well) the frame test fails, and under the last two the reading test does too. A run beside `MA` (`John Smith, M.D. MA`) never joins `maybe`, `MA` being the run's company (C16), so it reaches none of this. - 2026-11-18 #1436 — EVERY ROW AND THE LINK PIN MOVE DOWN: `parse` 285 → 197 on 3.11 and 277 → 188 on 3.12 to 3.15, `_LINK_BASELINE` 2,418 → 1,716 and 2,398 → 1,706. Measured 2026-11-18 on 688991e7 (#1458's merge, the tree before the change) and on the change rebased onto it, each interpreter on its own (`uv run python tools/perf/call_count.py`, `--against 688991e7` on 3.11, and with `UV_PROJECT_ENVIRONMENT= uv run --python 3.1x` for 3.12 to 3.15): 285.72 → 197.72 on 3.11 and 277.72 → 188.72 on each of 3.12 to 3.15, a fall of 31% and 32%; the link run `_name_link_run(64)`, `_frames_for` in `tests/v2/test_benchmark.py`, 2,404 → 1,716 on 3.11 and 2,395 → 1,706 on 3.12 to 3.15. #1434's simplify pass had measured scratch patches at about 234 on 3.11 and left them for a deliberate move. The changes, each a commit of its own, py3.11 reference row: tokenize scans a region for its separators with a compiled class per setting of the two strip switches rather than calling `_ignorable` on every character (−27), the ignorable set now defined by those classes, which `_ignorable` and `_stripped` match, and held to its parts over every code point by `test_the_separator_classes_hold_their_parts_over_every_code_point`; tokenize's sort key an `attrgetter` (−6); classify's tokens and group's frozen pieces built without a generator (−6, −10); group's tail start read by identity rather than from a dict keyed by the enum, whose `__hash__` is Python (−3); and `Token.__post_init__` paying nothing on the parse's own tokens, its span checked without a generator and an exact `Span` kept, its frozenset tags spared the str and `Mapping` refusals, its role coerced only when it is not a `Role` (−24, −6, −6), the checks and their messages unchanged and each refusal held by a row of `test_token_rejects_a_span_of_anything_but_two_ints` and the tests beside it. On the 64-link run, which holds 67 tokens, the same commits in order read −143, −67, −67, −6, −3 and −268, −67, −67 (`git log 688991e7..` checked out commit by commit): a cost per token or per character is a cost per link there. `_is_emoji` looped over its ranges in an earlier commit (no move on the reference row, 336 → 271 on a Devanagari name) and is gone, its ranges read through the class. Per name, `_frames_for` for one warm parse, each in a fresh process, 688991e7 then the change, 3.11 and 3.15: `John Smith` 126 → 91 and 120 → 84, `Smith, John` 136 → 99 and 125 → 87, `John Smith, M.D.` 199 → 149 and 185 → 134, `Dr. John Smith Jr.` 233 → 170 and 225 → 161, `John Smith Jr., PhD` 258 → 195 and 244 → 180, `राम बहादुर के.सी.` 336 → 193 and 326 → 182. Time, which the frames cannot see: 1000 parses of the reference name, minimum of 15, the two trees run in turn three times (py3.11, b78d5af2 and the change before the rebase), 116.9-117.4 ms → 109.2-109.9 ms of CPU, and per 1000 names, minimum of 9, `Smith, John` 64.7 → 61.0 ms, the long name `Sir John Ronald Reuel Tolkien00 van der Berg Jr., PhD` 176.2 → 165.1 ms, the Devanagari name 105.9 → 92.9 ms, `高橋一郎` 61.8 → 57.9 ms and forty double-spaced words 358.4 → 320.3 ms: no frame was traded for slower C work. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical; `tools/differential/measure.py --base` against b78d5af2 before the rebase, 29 steps with no move; and, across two trees each asserting its `nameparser.__file__`, b78d5af2 against the change before the rebase and again 688991e7 against the change rebased onto it, a grid of 114,647 readings (NFC and NFD spellings, ZWJ and ZWNJ inside Devanagari words, an emoji ZWJ sequence, a variation selector, bidi marks and isolates, ZWSP, the interpunct, the fullwidth stop and the nakaguro between Han names, U+202F in Mongolian, in twelve comma and clause shapes under the three orders and both strip switches and `locales.ZH`, the comparator a line holding the repr, the ambiguities, `initials()`, both case views and every token's text, span, role and tags) byte-identical, the harness shown to see a move (U+200B dropped from the invisible separators moves 4,303 lines), and a differential fuzz of the old `_tokenize_region` against the new over 300,000 random texts with random region bounds, 0 differences (#1459's code review). By the four frame questions: tokenize's call and `Token`'s checks multiply with the input (question 1), every parse pays all of them (question 2), and they move the band (question 3); none inlines a predicate save the role test, `_coerce_enum`'s own first line, which returns its argument and cannot drift (question 4). The long link run keeps its margin: a per-link regression is +64 against a 2% band of 34.3. SUPERSEDES: the 2026-11-09 #1170 entry's rows, and the 2026-11-03 #1036 entry's link pin. -- 2026-11-22, release checklist step 0's pass over tokenize — NO ROW MOVES; tokenize resolves an ambiguity's character offset to its token by bisecting the token starts, and skips the work on a parse with no ambiguity. WHY THE BISECT: extract's unmatched-close sweep reports one ambiguity per stray closer, so a scan of the tokens per ambiguity is quadratic in them. The comment that held the figures until this pass read `') ' * 1600` at 180ms with a linear scan, against 9ms before the sweep existed (written with 81ea9c17, 2026-07-19). Re-measured 2026-11-22 on py3.11 (Apple silicon), best of five whole `Parser().parse` calls: 3.8ms at 400 closers, 16.1ms at 1,600 and 74.7ms at 6,400, each holding as many reports as closers, so 4.2x and 4.6x for 4x the input; with `bisect.bisect_right` swapped in that process for a Python loop over the starts, 8.3ms at 400 and 77.9ms at 1,600, 9.4x. The guard is `test_benchmark.py`'s `unmatched_closes` scaling row, whose `') '` input reaches this resolution. THE FRAME: the gate asked `any(...)` of every parse's ambiguity tuple, a generator frame even when the tuple is empty; it now tests the tuple first. Measured 2026-11-22, py3.11, one warm parse counted with a `sys.setprofile` hook, af58ca09 and the change, each tree loaded by path with `PYTHONSAFEPATH=1` and its `__file__` checked: `tools/perf/call_count.py`'s reference row 196.72 → 195.72, against the 197 row and its ±2% band; `John Smith` 90 → 89, `Smith, John` 98 → 97, `John (Jack) Smith` 144 → 143, `王·小明` 105 → 104, fifty words 1,285 → 1,284, each the same under `FAMILY_FIRST`; `John "Jack ) Smith`, which holds a report, 203 either way. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical, and the two trees read alike over every distinct text of the committed `tools/differential/*.jsonl` files (the `name` key, or the line's string) plus a grid of one to five pieces drawn from Han, Hangul (composed and in conjoining jamo), Mongolian and Latin words, the two transcription dots, U+202F, the bidi marks, an emoji, the commas, the name dots, the invisible separators and the delimiters, 90,889 texts, each under the four settings of `strip_bidi` and `strip_emoji`: the comparator a line per text and setting holding tokenize's own tokens, comma and interpunct offsets and ambiguity indices, then the whole parse's `repr` and tokens, the two files compared with `cmp`: 0 of 363,556 lines differ. The negative control, af58ca09 with tokenize recording a transcription dot's offset only at odd indices, reads 1,775. +- 2026-11-22 #1527 — NO ROW MOVES; release checklist step 0's pass over tokenize. tokenize resolves an ambiguity's character offset to its token by bisecting the token starts, and tests the ambiguity tuple before looking for an offset in it. WHY THE BISECT: extract's unmatched-close sweep reports one ambiguity per stray closer, so a scan of the tokens per ambiguity is quadratic in them. The comment that held the figures until this pass read `') ' * 1600` at 180ms, against 9ms before the sweep existed; that 180ms was taken before two quadratics were fixed together, this scan and `ParsedName.__post_init__`'s token-subset check (182ms → 12ms, 81ea9c17's message, 2026-07-19). Re-measured 2026-11-22 on py3.11 (Apple silicon), best of five whole `Parser().parse` calls timed by `time.perf_counter()`: 3.8ms at 400 closers, 16.1ms at 1,600 and 74.7ms at 6,400, each holding as many reports as closers, so 4.2x and 4.6x for 4x the input; with `_tokenize.bisect.bisect_right` swapped in that process for `def linear(a, x): i = 0; while i < len(a) and a[i] <= x: i += 1; return i`, 8.3ms at 400 and 77.9ms at 1,600, 9.4x (the absolute figures depend on the loop's form and the clock, the ratio does not: the design-docs review read 9.3-9.6x with a `for` loop on CPU time). The guard is `test_benchmark.py`'s `unmatched_closes` scaling row, whose `') '` reaches this resolution: read by its own `_ratio` at its own sizes (800 → 3,200, CPU time), it gave 4.21x and 4.35x on the change and 12.53x and 11.95x with that loop swapped in, against `_MAX_RATIO` 6.0. THE FRAME: the gate asked `any(...)` of every parse's ambiguity tuple, a generator frame even when the tuple is empty; it now tests the tuple first. Measured 2026-11-22 on af58ca09 and the change, each tree loaded by path with `PYTHONSAFEPATH=1` and its `__file__` checked: `tools/perf/call_count.py`'s reference row 196.72 → 195.72 on py3.11 and 187.72 → 186.72 on py3.15, against the 197 and 188 rows and their ±2% band; per name, `test_benchmark.py`'s `_frames_for` (one warm-up parse, then the name), each name in a fresh process, py3.11, default order then `FAMILY_FIRST`: `John Smith` 90 → 89 and 91 → 90, `Smith, John` 98 → 97 and 97 → 96, `John (Jack) Smith` 144 → 143 and 145 → 144, `王·小明` (U+00B7) 107 → 106 and 108 → 107; `John "Jack ) Smith`, which holds a report, 203 and 204 either way. The band cannot see the generator put back, so `test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one` pins it at no call on three names without a report, with a recorded control of one call each on af58ca09. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical, and the two trees read alike over every distinct text of the committed `tools/differential/*.jsonl` files save `snapshot.jsonl` (the `name` key, or the line's string) plus a grid built from 27 pieces, `王`, `明`, `ᠪᠠ` (U+182A U+1820), `John`, `Smith`, `김` (U+AE40), the same syllable in conjoining jamo (U+1100 U+1161), U+00B7, U+FF0E, U+202F, U+200F, U+200B, U+1F600, `,`, U+FF0C, U+3001, U+30FB, U+1361, a space, `(`, `)`, `"`, U+00A0, U+2066, U+FEFF, U+3002 and `ᠠ` (U+1820), joined with nothing: every sequence of one to three of them, every eleventh four-piece sequence in `itertools.product` order, and every 37th five-piece sequence of the first 14, 90,889 texts in all, each under the four settings of `strip_bidi` and `strip_emoji`. The comparator a line per text and setting holding tokenize's own tokens (text, span, role), comma and interpunct offsets and ambiguity kinds and indices, then the whole parse's `repr` and tokens, the two files compared with `cmp`: 0 of 363,556 lines differ. The negative control, af58ca09 with tokenize recording a transcription dot's offset only at odd indices, reads 1,775 lines. diff --git a/nameparser/_pipeline/_tokenize.py b/nameparser/_pipeline/_tokenize.py index 72bee2243..2a8a984ad 100644 --- a/nameparser/_pipeline/_tokenize.py +++ b/nameparser/_pipeline/_tokenize.py @@ -39,19 +39,20 @@ # Ported from v1's "emoji" and "bidi" patterns (the 1.x and 2.x # nameparser.config.regexes, removed in 3.0, #1008); this is now the -# only copy. Written as integer ranges, never as a literal regex class: -# CodeQL's py/overly-large-range false-positives on literal astral -# ranges (surrogate decomposition), so the class below is built from -# the integers. +# only copy. The emoji ranges are written as integers, never as a +# literal regex class: CodeQL's py/overly-large-range false-positives on +# literal astral ranges (surrogate decomposition), so their class, +# _EMOJI_CHARS, is built from the integers. The bidi marks are all in +# the BMP and stay a literal class body. _EMOJI_RANGES = ((0x1F300, 0x1F64F), (0x1F680, 0x1F6FF), (0x2600, 0x26FF), (0x2700, 0x27BF)) _BIDI_CHARS = '\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069' -# The emoji ranges as a character class, for the classes below _EMOJI_CHARS = ''.join(f'{chr(lo)}-{chr(hi)}' for lo, hi in _EMOJI_RANGES) # rules/T2: "the katakana middle dot, its halfwidth twin and the -# Ethiopic wordspace divide a name like whitespace, always." They -# record no offset: the nakaguro also divides kanji roster pairs +# Ethiopic wordspace divide a name like whitespace, always." -- the +# Then, for these three characters, in every context. They record no +# offset: the nakaguro also divides kanji roster pairs # (高橋・一郎, read family-first by the script license, #272), so it is # not a transcription marker; _TRANSCRIPTION_DOTS below are # (decisions/T2 and T3, #298, #1202, #1312). @@ -84,8 +85,9 @@ _TRANSCRIPTION_DOTS = frozenset({_INTERPUNCT, "\uFF0E"}) # rules/T3: "the interpunct divides a name only between two # characters of a classified East Asian script; anywhere else it is -# part of the word" (history: decisions/T3). The flank guard asks -# _policy._classified_char per CHAR: a one-char string is +# part of the word" -- the Then, for both _TRANSCRIPTION_DOTS, whose +# flank guard _tokenize_region applies (decisions/T3, #298, #1312). The +# guard asks _policy._classified_char per CHAR: a one-char string is # wholly-classified iff the character is. Context-sensitivity is why the # guard lives in _tokenize_region (where the index exists) and not in # _ignorable. @@ -138,23 +140,25 @@ def _mongolian_letter(ch: str) -> bool: def _stripped(ch: str, policy: Policy) -> bool: - """True when the strip policy removes `ch` from the token stream, - the set _strip_class defines for _IGNORABLE and _BOUNDARY too, so a - stripped character never occupies a flank and a new strip class is - transparent to the transcription-dot guard by construction. The - separators (whitespace, the name dots and _INVISIBLE_SEPARATORS) - are not stripped and do occupy one.""" + """True when the strip policy removes `ch` from the token stream. + It matches the class _strip_class builds, which _IGNORABLE and + _BOUNDARY are built from too. _flank skips what it matches, so a + stripped character never occupies a flank, and a strip class added + to _strip_class is transparent to the transcription-dot guard by + construction. The separators (whitespace, the name dots and + _INVISIBLE_SEPARATORS) are not stripped and do occupy one.""" pattern = _STRIPPED[policy.strip_bidi, policy.strip_emoji] return pattern is not None and pattern.match(ch) is not None def _flank(text: str, indices: range, policy: Policy) -> str | None: """The nearest flank character the strip policy would keep, or - None if the range exhausts. Stripped invisibles are TRANSPARENT + None if the range exhausts. What the strip policy removes (the + bidi marks and the emoji, each under its switch) is TRANSPARENT here: an RTL document quoting a transcription puts U+200F beside - the dot in visually identical text. Whitespace and the other - separators stay guard-defeating: a dot beside a space is not - between characters.""" + the dot in visually identical text. Whitespace, the name dots and + _INVISIBLE_SEPARATORS stay guard-defeating: a dot beside a space is + not between characters.""" for i in indices: ch = text[i] if not _stripped(ch, policy): @@ -166,8 +170,9 @@ def _ignorable(ch: str, state: ParseState) -> bool: """True when `ch` separates the words beside it: whitespace, the name dots, _INVISIBLE_SEPARATORS (all unconditional, not policy-gated, and recording no offset, which the commas and the - transcription dots do), and what the strip policy removes. The one exception is decided in _tokenize_region, by - context: U+202F between two Mongolian letters joins them (rules/T6).""" + transcription dots do), and what the strip policy removes. The + one exception is decided in _tokenize_region, by context: U+202F + between two Mongolian letters joins them (rules/T6).""" policy = state.policy return _IGNORABLE[policy.strip_bidi, policy.strip_emoji].match( ch) is not None @@ -238,8 +243,9 @@ def tokenize(state: ParseState) -> ParseState: for start, end in zip(boundaries[::2], boundaries[1::2]): _tokenize_region(state, start, end, None, True, tokens, commas, interpuncts) - # extracted regions: pre-set role; a comma is word text there and an - # interpunct a mere separator, neither recording an offset + # extracted regions: pre-set role; a comma is word text there, and a + # transcription dot divides where its flanks allow but records no + # offset for role, inner in state.extracted: _tokenize_region(state, inner.start, inner.end, role, False, tokens, commas, interpuncts) @@ -253,10 +259,10 @@ def tokenize(state: ParseState) -> ParseState: # offset back out of its detail string. An offset inside a masked # region belongs to no token; those keep an empty tuple, which the # kind's contract already allows. - # Bisect rather than rescan: the closer sweep can emit one - # ambiguity per delimiter character, so a linear scan per ambiguity - # is quadratic (test_benchmark.py's `unmatched_closes` row; - # decisions/parse-cost, 2026-11-22). Spans are non-overlapping and + # Bisect rather than rescan: the closer sweep reports one ambiguity + # per stray closing delimiter, so a linear scan per ambiguity is + # quadratic in them (test_benchmark.py's `unmatched_closes` row; + # decisions/parse-cost, 2026-11-22 #1527). Spans are non-overlapping and # sorted just above, so the candidate is the last token whose start # is <= the offset. # Nothing to resolve on the common parse with no ambiguity, which diff --git a/tests/v2/test_benchmark.py b/tests/v2/test_benchmark.py index 7a1a9015a..39a9a8cdc 100644 --- a/tests/v2/test_benchmark.py +++ b/tests/v2/test_benchmark.py @@ -1338,6 +1338,25 @@ def test_post_rules_helpers_return_before_their_frames() -> None: only="_leading_name_piece") >= 1 +def test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one() -> None: + """tokenize resolves a report's character offset only where extract + left a report, and tests the tuple before the `any()` generator + that looks for an offset, whose every resume is a frame + (decisions/parse-cost, 2026-11-22 #1527). The band cannot see the + generator put back. Recorded negative control (2026-11-22, py3.11, + on af58ca09, the tree before): the generator ran once on each name + below with no report.""" + if sys.getprofile() is not None: + pytest.skip("a profile hook is already installed; this test owns it") + for text in ("John Smith", "Smith, John", "John (Jack) Smith"): + assert _frames_for(text, only="", caller="tokenize") == 0, ( + text) + # REACHABILITY: a stray delimiter leaves a report with an offset, + # and the generator runs to find it + assert _frames_for('John "Jack ) Smith', only="", + caller="tokenize") >= 1 + + def test_a_script_with_no_order_entry_stops_the_script_walk() -> None: """`_effective_order` asks one word's script and returns the declared order where that script, outside the kana license, has no From dacacc9ffe6b40ee53ae912aa748bd6adb98a035 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 22 Nov 2026 01:21:17 -0800 Subject: [PATCH 4/5] Pin where tokenize points a stray delimiter's report, in its own tests 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 --- tests/v2/pipeline/test_tokenize.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/v2/pipeline/test_tokenize.py b/tests/v2/pipeline/test_tokenize.py index e3782af44..4372d84ed 100644 --- a/tests/v2/pipeline/test_tokenize.py +++ b/tests/v2/pipeline/test_tokenize.py @@ -537,3 +537,20 @@ def test_full_name_before_an_ethiopic_suffix_comma( order: tuple[Role, Role, Role], expect: dict[str, str]) -> None: name = Parser(policy=Policy(name_order=order)).parse("አበበ ከበደ፣ Jr.") assert {k: v for k, v in name.as_dict().items() if v} == expect + + +@pytest.mark.parametrize("text, held", [ + ('Jon "Nick Smith', ['"Nick']), # the offset opens its token + ('"John Smith', ['"John']), # offset 0, the first token + ("John Smith)", ["Smith)"]), # the offset ends its token + ('John Smith "', ['"']), # a lone delimiter, the last token +]) +def test_a_stray_delimiters_report_points_at_the_token_holding_it( + text: str, held: list[str]) -> None: + # extract reports a stray delimiter by its character offset, and + # tokenize rewrites the offset to the index of the token holding the + # character, by bisecting the token starts: a row at each end of a + # token, and at each end of the name + out = _tokenized(text) + assert [out.tokens[i].text for a in out.ambiguities + for i in a.indices] == held From e4021719764efd004afbe20f578758441defb1f3 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 22 Nov 2026 01:34:05 -0800 Subject: [PATCH 5/5] Correct three details of the parse-cost entry found by its second review 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 --- docs/design/decisions/parse-cost.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/design/decisions/parse-cost.md b/docs/design/decisions/parse-cost.md index 1d34f4594..5d0cd908a 100644 --- a/docs/design/decisions/parse-cost.md +++ b/docs/design/decisions/parse-cost.md @@ -50,4 +50,4 @@ Open: none in this file. - 2026-11-11 #1352 — THE TIMED ROWS READ CPU TIME, AND THE COUNTS RUN IN PARALLEL; NO ROW, BOUND OR BASELINE MOVES. On a laptop running several agent sessions the wall-clock rows failed at random and passed alone. Measured on py3.11, 12 cores (6 performance, 6 efficiency), five `uv run pytest -n0 -m serial tests/v2/test_benchmark.py` runs beside 24 shell busy loops (load average 37-48): 20, 23, 21, 19 and 23 of the module's 62 tests failed on 970b54e5, at up to 10.0x against `_MAX_RATIO` 6.0; on the change, 0 of 34 in each of five runs at 24 busy loops (load 50-62) and at none, and in five at 12, which barely loaded the machine (the before tree failed none there either). Per row, three runs at 24 busy loops (load 18-75): `time.perf_counter()` read 27 of the 33 ratio rows at 6.0 or over at least once, up to 14.0x; `time.thread_time()`, measured back to back, none over 5.76x (as high as 5.76x with no busy loops too). CPU time is not load-immune -- interleaved best-of-7 read a clean row up to 5.6x, the thread charged for a slower core or a colder cache, and a user-interactive QoS hint did not help -- and noise can DEFLATE a ratio as well as inflate it: with more rounds taken only at or over the bound, a planted quadratic reading 6.7x quiet (`given_part_titles`) read 5.93x once at 24 busy loops and passed, and a loop stopping at its first reading under the bound could stop on a dip (both found by #1352's review). So `_ratio` interleaves the two inputs and, where its first 7 rounds read at or over 80% of the bound (`_NEAR`; `bound * _NEAR` is 4.800000000000001 in floating point), takes all 35 and judges the last minimums, each round only moving the two minimums toward the true costs; `test_ratio_takes_more_rounds_only_near_the_bound` holds that on a scripted clock, and fails against both earlier loops. Loaded, the serial pass takes 78-115s against 47s, the escalation's price; quiet it takes 18s, as before. The defects it guards are seen as before. Quiet, the old wall-clock `_best` and `_ratio` fail the same rows in every run under a planted C-level quadratic in the word count, every row within 0.35x. The recorded controls read on `_ratio`, quiet then at 24 busy loops: `link_run` 13.9-14.1 and 14.9-16.4 on b9ed1429; `given_part_run` 8.0 and 10.0-10.3 and `given_part_titles` 10.1 and 11.1-12.5 on 003b4962; `title_chain` 12.0 and 13.5-14.8, `pair_run` 10.0 and 10.2-12.8, `caps_pairs` 10.1-10.2 and 12.2-12.8 and `delimiter_cores` 10.4 and 11.8-13.1 on 9dd271b2; `connective_between_particles` 8.5 and 8.9-10.2 on 5752df69 (#939 before its review); and `slash_chain` 8.8-8.9 and 8.9-9.2 with #1103's per-step copy planted. That is against 3.97-4.19 quiet and up to 5.06 at 24 busy loops on 970b54e5. The weakest planted quadratic, 6.3-7.0x quiet on eleven rows, read 7.18x or more on every row in five runs at 24 busy loops. A planted backtracking regex reads 7.31 s of CPU over the thousand names, against 7.36 s wall. The 5s bound is CPU seconds now and no looser for work on the parse's own thread, CPU time being at most the wall time of a single-threaded parse; it no longer sees time spent blocked or on another thread, which a parse never spends. 962301f7's memory move reads 4.07-4.43 on the CPU clock and 4.07-4.50 on the wall clock, quiet, as the 2026-11-03 #1036 entry found for the wall clock; `_ShiftCountingList` holds it. The 28 untimed tests (the frame, call and shift counts and the table checks) lose the `serial` mark. Each passes alone in a fresh process, after the 21,065 tests of `test_cases.py` and `test_match.py` in one, in three full parallel runs, and under `--cov` on 3.11 and 3.14. The order dependence of an absolute count (the 2026-11-09 #1170 entry) stays inside a ratio, a band, or an exact count of one named function's calls (`only=`/`caller=`, `== 0` up to `== 6`), none of which counts an `lru_cache`'d function. - 2026-11-18 #1435 — NO ROW MOVES; A RUN OF INITIALS BEHIND A FULL NAME ASKS NO COUNT. #1238's C16 block in `_comma.decide` put a dotted run spelling a listed credential, every letter a capital or the name in one case, into `maybe`, so `John Smith, M.D.` paid C4's `name_word_count` and the C11 books, which only a one-word head reads the run by. Where the run is the part's only entry in `maybe` (no word of the class and no caps-shape candidate beside it), `decide` now asks `_lone_word` first (once, its answer kept for the reading), and behind a head that is not one word skips the count and the books: the run stays in `maybe`, so the part is still read as the count's part and never takes the listing form's early return, and with nothing else in `maybe` the count licenses nothing and `head_named` meets no member. Frames, `test_benchmark._frames_for` for one parse in a fresh process, master (b78d5af2) then branch: `John Smith, M.D.` 221 → 199 on 3.11 and 203 → 185 on 3.15; `John Smith, MD` 190 and 178 and `Smith, John` 136 and 125 unchanged; `Smith, J.P.`, which still counts, 233 and 218 unchanged. The issue's table (209 for `John Smith, M.D.` on master) was taken in a process that had parsed other names first, which moves the absolute count and not the difference (the 2026-11-09 #1170 entry). The issue's proposed gate (append the run only beside another class word or behind a one-word head) was not taken: it sends such a part down the no-`maybe` path, equal only where the listing early return and C1's fallback agree, which nothing holds by construction. Checked against master: a grid of 215,280 parses (65 heads, from empty, nickname, title, particle and title-particle heads alone and in pairs to full and one-case names, × 46 tails of dotted runs, listed and ambiguous credentials, paired initials and their company × three comma spellings and a later part × the three orders × six policies) byte-identical, the issue's gate also identical there, and the harness shown to see a move (on the same grid, a `_lone_word` always true moves 34,848 rows, and skipping the count behind a one-word head too moves 4,536, `Smith van, M.D.` reading given 'M.', middle 'D.', family 'Smith van' where master reads given 'Smith', family 'van', suffix 'M.D.', `_lone_word` reading that head as one word while the count finds two); `snapshot.jsonl` byte-identical; and `snapshot.py --local ctgov-officials` and `ctgov-contacts` against master, no move. `test_a_run_of_initials_behind_a_full_name_takes_no_count` holds the saving, its control recorded in its docstring, and `test_the_count_c16_skips_decides_no_reading` (tests/v2/pipeline/test_comma.py) the readings a wrong gate moves. Under each of three mutants (the gate removed; the count skipped behind a one-word head too; `run_alone` forced true, so a run beside another entry of `maybe` such as `John Smith, M.D. M.A` skips the count as well) the frame test fails, and under the last two the reading test does too. A run beside `MA` (`John Smith, M.D. MA`) never joins `maybe`, `MA` being the run's company (C16), so it reaches none of this. - 2026-11-18 #1436 — EVERY ROW AND THE LINK PIN MOVE DOWN: `parse` 285 → 197 on 3.11 and 277 → 188 on 3.12 to 3.15, `_LINK_BASELINE` 2,418 → 1,716 and 2,398 → 1,706. Measured 2026-11-18 on 688991e7 (#1458's merge, the tree before the change) and on the change rebased onto it, each interpreter on its own (`uv run python tools/perf/call_count.py`, `--against 688991e7` on 3.11, and with `UV_PROJECT_ENVIRONMENT= uv run --python 3.1x` for 3.12 to 3.15): 285.72 → 197.72 on 3.11 and 277.72 → 188.72 on each of 3.12 to 3.15, a fall of 31% and 32%; the link run `_name_link_run(64)`, `_frames_for` in `tests/v2/test_benchmark.py`, 2,404 → 1,716 on 3.11 and 2,395 → 1,706 on 3.12 to 3.15. #1434's simplify pass had measured scratch patches at about 234 on 3.11 and left them for a deliberate move. The changes, each a commit of its own, py3.11 reference row: tokenize scans a region for its separators with a compiled class per setting of the two strip switches rather than calling `_ignorable` on every character (−27), the ignorable set now defined by those classes, which `_ignorable` and `_stripped` match, and held to its parts over every code point by `test_the_separator_classes_hold_their_parts_over_every_code_point`; tokenize's sort key an `attrgetter` (−6); classify's tokens and group's frozen pieces built without a generator (−6, −10); group's tail start read by identity rather than from a dict keyed by the enum, whose `__hash__` is Python (−3); and `Token.__post_init__` paying nothing on the parse's own tokens, its span checked without a generator and an exact `Span` kept, its frozenset tags spared the str and `Mapping` refusals, its role coerced only when it is not a `Role` (−24, −6, −6), the checks and their messages unchanged and each refusal held by a row of `test_token_rejects_a_span_of_anything_but_two_ints` and the tests beside it. On the 64-link run, which holds 67 tokens, the same commits in order read −143, −67, −67, −6, −3 and −268, −67, −67 (`git log 688991e7..` checked out commit by commit): a cost per token or per character is a cost per link there. `_is_emoji` looped over its ranges in an earlier commit (no move on the reference row, 336 → 271 on a Devanagari name) and is gone, its ranges read through the class. Per name, `_frames_for` for one warm parse, each in a fresh process, 688991e7 then the change, 3.11 and 3.15: `John Smith` 126 → 91 and 120 → 84, `Smith, John` 136 → 99 and 125 → 87, `John Smith, M.D.` 199 → 149 and 185 → 134, `Dr. John Smith Jr.` 233 → 170 and 225 → 161, `John Smith Jr., PhD` 258 → 195 and 244 → 180, `राम बहादुर के.सी.` 336 → 193 and 326 → 182. Time, which the frames cannot see: 1000 parses of the reference name, minimum of 15, the two trees run in turn three times (py3.11, b78d5af2 and the change before the rebase), 116.9-117.4 ms → 109.2-109.9 ms of CPU, and per 1000 names, minimum of 9, `Smith, John` 64.7 → 61.0 ms, the long name `Sir John Ronald Reuel Tolkien00 van der Berg Jr., PhD` 176.2 → 165.1 ms, the Devanagari name 105.9 → 92.9 ms, `高橋一郎` 61.8 → 57.9 ms and forty double-spaced words 358.4 → 320.3 ms: no frame was traded for slower C work. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical; `tools/differential/measure.py --base` against b78d5af2 before the rebase, 29 steps with no move; and, across two trees each asserting its `nameparser.__file__`, b78d5af2 against the change before the rebase and again 688991e7 against the change rebased onto it, a grid of 114,647 readings (NFC and NFD spellings, ZWJ and ZWNJ inside Devanagari words, an emoji ZWJ sequence, a variation selector, bidi marks and isolates, ZWSP, the interpunct, the fullwidth stop and the nakaguro between Han names, U+202F in Mongolian, in twelve comma and clause shapes under the three orders and both strip switches and `locales.ZH`, the comparator a line holding the repr, the ambiguities, `initials()`, both case views and every token's text, span, role and tags) byte-identical, the harness shown to see a move (U+200B dropped from the invisible separators moves 4,303 lines), and a differential fuzz of the old `_tokenize_region` against the new over 300,000 random texts with random region bounds, 0 differences (#1459's code review). By the four frame questions: tokenize's call and `Token`'s checks multiply with the input (question 1), every parse pays all of them (question 2), and they move the band (question 3); none inlines a predicate save the role test, `_coerce_enum`'s own first line, which returns its argument and cannot drift (question 4). The long link run keeps its margin: a per-link regression is +64 against a 2% band of 34.3. SUPERSEDES: the 2026-11-09 #1170 entry's rows, and the 2026-11-03 #1036 entry's link pin. -- 2026-11-22 #1527 — NO ROW MOVES; release checklist step 0's pass over tokenize. tokenize resolves an ambiguity's character offset to its token by bisecting the token starts, and tests the ambiguity tuple before looking for an offset in it. WHY THE BISECT: extract's unmatched-close sweep reports one ambiguity per stray closer, so a scan of the tokens per ambiguity is quadratic in them. The comment that held the figures until this pass read `') ' * 1600` at 180ms, against 9ms before the sweep existed; that 180ms was taken before two quadratics were fixed together, this scan and `ParsedName.__post_init__`'s token-subset check (182ms → 12ms, 81ea9c17's message, 2026-07-19). Re-measured 2026-11-22 on py3.11 (Apple silicon), best of five whole `Parser().parse` calls timed by `time.perf_counter()`: 3.8ms at 400 closers, 16.1ms at 1,600 and 74.7ms at 6,400, each holding as many reports as closers, so 4.2x and 4.6x for 4x the input; with `_tokenize.bisect.bisect_right` swapped in that process for `def linear(a, x): i = 0; while i < len(a) and a[i] <= x: i += 1; return i`, 8.3ms at 400 and 77.9ms at 1,600, 9.4x (the absolute figures depend on the loop's form and the clock, the ratio does not: the design-docs review read 9.3-9.6x with a `for` loop on CPU time). The guard is `test_benchmark.py`'s `unmatched_closes` scaling row, whose `') '` reaches this resolution: read by its own `_ratio` at its own sizes (800 → 3,200, CPU time), it gave 4.21x and 4.35x on the change and 12.53x and 11.95x with that loop swapped in, against `_MAX_RATIO` 6.0. THE FRAME: the gate asked `any(...)` of every parse's ambiguity tuple, a generator frame even when the tuple is empty; it now tests the tuple first. Measured 2026-11-22 on af58ca09 and the change, each tree loaded by path with `PYTHONSAFEPATH=1` and its `__file__` checked: `tools/perf/call_count.py`'s reference row 196.72 → 195.72 on py3.11 and 187.72 → 186.72 on py3.15, against the 197 and 188 rows and their ±2% band; per name, `test_benchmark.py`'s `_frames_for` (one warm-up parse, then the name), each name in a fresh process, py3.11, default order then `FAMILY_FIRST`: `John Smith` 90 → 89 and 91 → 90, `Smith, John` 98 → 97 and 97 → 96, `John (Jack) Smith` 144 → 143 and 145 → 144, `王·小明` (U+00B7) 107 → 106 and 108 → 107; `John "Jack ) Smith`, which holds a report, 203 and 204 either way. The band cannot see the generator put back, so `test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one` pins it at no call on three names without a report, with a recorded control of one call each on af58ca09. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical, and the two trees read alike over every distinct text of the committed `tools/differential/*.jsonl` files save `snapshot.jsonl` (the `name` key, or the line's string) plus a grid built from 27 pieces, `王`, `明`, `ᠪᠠ` (U+182A U+1820), `John`, `Smith`, `김` (U+AE40), the same syllable in conjoining jamo (U+1100 U+1161), U+00B7, U+FF0E, U+202F, U+200F, U+200B, U+1F600, `,`, U+FF0C, U+3001, U+30FB, U+1361, a space, `(`, `)`, `"`, U+00A0, U+2066, U+FEFF, U+3002 and `ᠠ` (U+1820), joined with nothing: every sequence of one to three of them, every eleventh four-piece sequence in `itertools.product` order, and every 37th five-piece sequence of the first 14, 90,889 texts in all, each under the four settings of `strip_bidi` and `strip_emoji`. The comparator a line per text and setting holding tokenize's own tokens (text, span, role), comma and interpunct offsets and ambiguity kinds and indices, then the whole parse's `repr` and tokens, the two files compared with `cmp`: 0 of 363,556 lines differ. The negative control, af58ca09 with tokenize recording a transcription dot's offset only at odd indices, reads 1,775 lines. +- 2026-11-22 #1527 — NO ROW MOVES; release checklist step 0's pass over tokenize. tokenize resolves an ambiguity's character offset to its token by bisecting the token starts, and tests the ambiguity tuple before looking for an offset in it. WHY THE BISECT: extract's unmatched-close sweep reports one ambiguity per stray closer, so a scan of the tokens per ambiguity is quadratic in them. The comment that held the figures until this pass read `') ' * 1600` at 180ms, against 9ms before the sweep existed; that 180ms was taken before two quadratics were fixed together, this scan and `ParsedName.__post_init__`'s token-subset check (182ms → 12ms, 81ea9c17's message, 2026-07-19). Re-measured 2026-11-22 on py3.11 (Apple silicon), best of five whole `Parser().parse` calls timed by `time.perf_counter()`: 3.8ms at 400 closers, 16.1ms at 1,600 and 74.7ms at 6,400, each holding as many reports as closers, so 4.2x and 4.6x for 4x the input; with `_tokenize.bisect.bisect_right` swapped in that process for `def linear(a, x): i = 0; while i < len(a) and a[i] <= x: i += 1; return i`, 8.3ms at 400 and 77.9ms at 1,600, 9.4x (the absolute figures depend on the loop's form and the clock, the ratio does not: the design-docs review read 9.3-9.6x with a `for` loop on CPU time). The guard is `test_benchmark.py`'s `unmatched_closes` scaling row, whose `') '` reaches this resolution: read by its own `_ratio` at its own sizes (800 → 3,200, CPU time), it gave 4.21x and 4.35x on the change and 12.53x and 11.95x with that loop swapped in, against `_MAX_RATIO` 6.0. THE FRAME: the gate asked `any(...)` of every parse's ambiguity tuple, a generator frame even when the tuple is empty; it now tests the tuple first. Measured 2026-11-22 on af58ca09 and the change, each tree loaded by path with `PYTHONSAFEPATH=1` and its `__file__` checked: `tools/perf/call_count.py`'s reference row 196.72 → 195.72 on py3.11 and 187.72 → 186.72 on py3.15, against the 197 and 188 rows and their ±2% band; per name, `test_benchmark.py`'s `_frames_for` (one warm-up parse, then the name; the default order through its default `parse()`, `FAMILY_FIRST` through `parse=Parser(policy=Policy(name_order=FAMILY_FIRST)).parse`, which lacks `parse()`'s own frame, so the two columns differ by entry point and not only by order), each name in a fresh process, py3.11, default order then `FAMILY_FIRST`: `John Smith` 90 → 89 and 91 → 90, `Smith, John` 98 → 97 and 97 → 96, `John (Jack) Smith` 144 → 143 and 145 → 144, `王·小明` (U+00B7) 107 → 106 and 108 → 107; `John "Jack ) Smith`, which holds a report, 203 and 204 either way. The band cannot see the generator put back, so `test_tokenize_asks_no_ambiguity_origin_of_a_parse_without_one` pins it at no call on three names without a report, with a recorded control of one call each on af58ca09. BEHAVIOR IDENTICAL: `snapshot.jsonl` byte-identical, and the two trees read alike over every distinct text of the committed `tools/differential/*.jsonl` files save `snapshot.jsonl` (the `name` key, or the line's string) plus a grid built from 27 pieces, `王`, `明`, `ᠪᠠ` (U+182A U+1820), `John`, `Smith`, `김` (U+AE40), `가` in conjoining jamo (U+1100 U+1161), U+00B7, U+FF0E, U+202F, U+200F, U+200B, U+1F600, `,`, U+FF0C, U+3001, U+30FB, U+1361, a space, `(`, `)`, `"`, U+00A0, U+2066, U+FEFF, U+3002 and `ᠠ` (U+1820), joined with nothing: every sequence of one to three of them, every eleventh four-piece sequence in `itertools.product` order, and every 37th five-piece sequence of the first 14, 90,889 texts in all, each under the four settings of `strip_bidi` and `strip_emoji`. The comparator: a line per text and setting holding tokenize's own tokens (text, span, role), run over extract's output with an empty lexicon, its comma and interpunct offsets and its ambiguity kinds and indices, then the whole parse's `repr` and tokens; the two files compared with `cmp`: 0 of 363,556 lines differ. The negative control, af58ca09 with tokenize recording a transcription dot's offset only at odd indices, reads 1,775 lines.