Skip to content

fix: stop the title cleanup patterns backtracking on long names - #91

Open
a1d4r wants to merge 1 commit into
AIOStreams:mainfrom
a1d4r:fix/title-cleanup-backtracking
Open

a1d4r wants to merge 1 commit into
AIOStreams:mainfrom
a1d4r:fix/title-cleanup-backtracking

Conversation

@a1d4r

@a1d4r a1d4r commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Two patterns in cleanTitle backtrack through every split of the rest of
the name from every start that cannot match, so the time grows with the cube
of the length. A long name without the character the pattern has to end at
takes seconds:

Title Before After
'Фильм ' + 'а'.repeat(1000) 1.2 s 9 ms
'Фильм ' + 'а'.repeat(3000) 30 s 80 ms
'а('.repeat(512) 0.3 s 9 ms
'а('.repeat(1500) 7.4 s 77 ms
'a/('.repeat(1000) 1.5 s 5 ms
  • altTitlesRegex: a name with Cyrillic or CJK letters and no / or |.
    The first alternative is [^/|(]*[…][^/|]*[/|]; with no separator ahead
    each start tries every position of the letter and every length of the tail.
  • russianCastRegex: brackets that do not close the name. Both alternatives
    end with \)$.

Fix: a lookahead in each alternative for the place it has to end at anyway —
(?=[^/|]*[/|]), (?=[^)]*\)$), (?=.*\)$). A start that cannot end there
fails at once. No match changes: every match of the old alternative satisfies
the lookahead, and the alternatives are tried in the same order.

Checked: the old and the new pattern give the same matches (index and groups)
on the titles of the test corpus and on 800k random strings over the
characters the patterns tell apart; parseTorrentTitle returns the same JSON
for all 1,156 titles of bench/corpus.cjs and 3,976 tracker titles.

Tests: src/tests/longTitle.test.ts, 1349 tests pass. The remaining
patterns in cleanTitle are quadratic at worst (about 10 ms for a name of
1024 characters).

🤖 Generated with Claude Code

Two patterns tried every split of the rest of the name from every start
that could not match, which is cubic in the length:

- altTitlesRegex on a name with Cyrillic or CJK letters and no "/" or "|":
  1000 letters took 1.4 s, 3000 about 30 s;
- russianCastRegex on brackets that do not close the name: "а(" repeated
  to 1024 characters took 0.3 s, to 3000 about 9 s.

A lookahead for the place each alternative has to end at anyway fails
those starts at once and changes no match.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant