Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/design/decisions/parse-cost.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<a scratch folder> 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; 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.
Loading