Skip to content

Many leading titles plus many surname particles slow parsing quadratically #559

Description

@derek73

Found by the _pipeline/ sweep for #553.

Symptom. A name with many leading titles and many surname particles takes time proportional to the product of the two counts. Ratio for 4× the input (py3.11, 2026-09-28, best of 5), where 4× is linear:

shape 2.2.0 2.3.0 master
"Dr. " * n + "Jan " + "van Berg " * n 10.0× 10.1× 10.5×

This has been present since at least 2.2.0.

Mechanism. In _pipeline/_group.py, chain() inside _group_segment evaluates all(is_leading_title(pieces[x], ...) for x in range(k)) at every ambiguous-particle chain site k, inside the while k < len(pieces) loop. The all() short-circuits only at the first non-title, so the cost is (leading titles) × (chain sites). At n=400, cProfile shows about 160,800 generator steps and 162k is_leading_title calls. da Silva behaves the same way.

Fix. Compute the length of the leading-title run once, before the loop, and compare k against it. Check that merge() cannot change that run while the loop is running, or recompute it only where it can. Add a guard row: a _PREFIXED_SHAPES row (#557) or a frame-ratio test, with a recorded negative control.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions