frontend: _SPACE_BEFORE_BLOCK takes quadratic time on a long run of whitespace

Open Beginner friendly
#408 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
performance

Research direction

Start at scriptconv/frontend.py line 114, where _SPACE_BEFORE_BLOCK = \s\W*$ is defined, and line 1140, where the block join searches it across the whole preceding text. Run the repro snippet to confirm the quadratic growth, then pin down what the pattern must still match (trailing whitespace plus non-word characters) so the replacement keeps that meaning — e.g. strip the tail and test the last character, or search an end-anchored pattern on the reversed tail. Done means the 20,000-space case is fast and the existing frontend tests pass.

Written by the indexing model from the issue text.

Description

🤖 Auto-generated by Claude Opus 5.5 (claude-opus-5-5) via Claude Code — NOT human-reviewed. Verify before acting.

Verified against source: the lines and outputs below were read and run on scriptconv origin/dev 03b4c08.

Observed

_SPACE_BEFORE_BLOCK in scriptconv/frontend.py (line 114) is \s\W*$, and the block join searches it over the whole text before a block (line 1140). On a long run of whitespace that a letter follows, the search time grows with the square of the run length:

Text searched Time
a, 5,000 spaces, b 1.40 s
a, 10,000 spaces, b 3.04 s
a, 20,000 spaces, b 7.92 s

The timings come from a loaded machine, so the absolute values are high. The growth from 10,000 to 20,000 is the point.

Expected

The check reads only the end of the text, so its time does not depend on the length of the whitespace before the end. For example, strip the end of the text and test the last character, or search a pattern anchored at the end on the reversed tail.

Reproduce

import time
from scriptconv.frontend import _SPACE_BEFORE_BLOCK

text = "a" + " " * 20000 + "b"
start = time.perf_counter()
_SPACE_BEFORE_BLOCK.search(text)
print(time.perf_counter() - start)

Where

scriptconv/frontend.py line 114 (the pattern) and line 1140 (the search in the block join). The review of #353 found the case.

Dominant language
Python
Stars
4
Forks
1
Avg merge
8h 57m
Merged PRs (30d)
118

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TigreGotico/scriptconv

All issues in TigreGotico/scriptconv

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.