Skip to content

Wrap syntax text directly#4179

Open
KRRT7 wants to merge 4 commits into
Textualize:mainfrom
KRRT7:perf/syntax-direct-word-wrap
Open

Wrap syntax text directly#4179
KRRT7 wants to merge 4 commits into
Textualize:mainfrom
KRRT7:perf/syntax-direct-word-wrap

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Optimizes Syntax(word_wrap=True, line_numbers=False) by wrapping the highlighted Text directly and yielding adjusted segments, instead of sending the whole renderable through Console.render_lines(...).

This stacks on #4178 and continues the syntax word-wrap performance work.

Performance framing

Observed:
The no-line-number word-wrap path still routes large syntax output through Console.render_lines(...), which re-enters the generic render pipeline and then splits/crops the rendered segments.

Target:
Wrap the highlighted syntax Text directly, then render and adjust each wrapped line for the configured width.

Hypothesis:
Avoiding the generic render_lines path should reduce overhead for large syntax blocks while preserving padding, styles, blank lines, trailing newlines, and line ranges.

Target workload

User-visible operation: Console.print(Syntax(...)) for large word-wrapped Python syntax output without line numbers.

Workload:

code = snippets.PYTHON_SNIPPET * 120
syntax = Syntax(code=code, lexer="python", word_wrap=True, line_numbers=False)
console = Console(file=StringIO(), color_system="truecolor", legacy_windows=False, width=30)
console.print(syntax)

Environment:

  • Darwin arm64, Darwin Kernel Version 25.5.0
  • Python 3.14.6
  • Pygments 2.20.0
  • pytest 9.0.3

Performance

Before, on #4178 tip before this change:

median: 0.756429s over 7 runs
profile: Console.render_lines cumulative 1.024s

After:

median: 0.716699s over 7 runs
profile: Console.render_lines no longer appears in the hot path

This is about a 5.3% speedup for the target workload.

Correctness

python3 -m pytest tests/test_syntax.py tests/test_console.py
126 passed

I also checked output preservation for plain wrapped text, trailing newlines, blank middle lines, and line_range, and smoke-tested the new ASV benchmark method:

SyntaxLargeWrappingSuite.setup()
SyntaxLargeWrappingSuite.time_text_thin_terminal_heavy_wrapping()

Tradeoffs

No cache or retained state. This duplicates the small part of Console.render_lines(...) needed here: style application, fixed-width adjustment, and newline emission after Text.wrap(...).

Stack

Order PR Branch Merge after
1 #4177 optimize-syntax-word-wrap main
2 #4178 perf/syntax-line-numbers-word-wrap #4177
3 #4179 perf/syntax-direct-word-wrap #4178

@KRRT7 KRRT7 force-pushed the perf/syntax-direct-word-wrap branch from 2bca925 to 6853707 Compare June 29, 2026 16:06
@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    #4179   +/-   ##
=======================================
  Coverage        ?   97.81%           
=======================================
  Files           ?       96           
  Lines           ?     8390           
  Branches        ?        0           
=======================================
  Hits            ?     8207           
  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