Skip to content

Skip hidden line-range syntax prefix text#4182

Open
KRRT7 wants to merge 6 commits into
Textualize:mainfrom
KRRT7:perf/syntax-line-range-skip-prefix
Open

Skip hidden line-range syntax prefix text#4182
KRRT7 wants to merge 6 commits into
Textualize:mainfrom
KRRT7:perf/syntax-line-range-skip-prefix

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Workload

Render a large Python Syntax object with a late line_range and no wrapping or line numbers:

PYTHONPATH=. uvx --with pytest-benchmark pytest /tmp/rich_pytest_bench_syntax.py --benchmark-only --benchmark-min-rounds=15 --benchmark-max-time=1 --benchmark-cprofile=cumtime --benchmark-cprofile-top=15 --benchmark-columns="min,median,mean,rounds,iterations"

Input: snippets.PYTHON_SNIPPET * 120, lexer="python", word_wrap=False, line_numbers=False, line_range=(3000, 3060), width 80.

This matters for callers that render only a small late slice of a large highlighted source file.

Results

Before, at 6d54cb8d:

  • test_highlight_line_range_3000: 65.1610 ms median over 15 rounds
  • test_render_line_range_3000: 71.1404 ms median over 15 rounds

After, at 06406dd2:

  • test_highlight_line_range_3000: 62.9820 ms median over 15 rounds
  • test_render_line_range_3000: 68.3597 ms median over 15 rounds

The pytest-benchmark cProfile output shows the previous path spent extra time in Text.append_tokens appending skipped prefix tokens before the requested range. The new path still tokenizes the prefix so lexer state is preserved, but emits only newline placeholders for skipped lines.

Correctness

  • python3 -m pytest tests/test_syntax.py tests/test_console.py
  • uvx black --check rich/syntax.py benchmarks/benchmarks.py tests/test_syntax.py

Tradeoffs

No behavior change intended. Late line ranges still tokenize from the beginning of the source to keep Pygments lexer state correct. This only avoids storing the skipped source text in the intermediate Text.

Stack

Order PR Branch Depends on
1 #4177 optimize-syntax-word-wrap main
2 #4178 perf/syntax-line-numbers-word-wrap #4177
3 #4179 perf/syntax-direct-word-wrap #4178
4 #4181 perf/syntax-direct-no-wrap #4179
5 This PR perf/syntax-line-range-skip-prefix #4181

@codecov-commenter

codecov-commenter commented Jun 29, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@9d8f9a3). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4182   +/-   ##
=======================================
  Coverage        ?   97.81%           
=======================================
  Files           ?       96           
  Lines           ?     8392           
  Branches        ?        0           
=======================================
  Hits            ?     8209           
  Misses          ?      183           
  Partials        ?        0           
Flag Coverage Δ
unittests 97.81% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants