Skip to content

perf: Optimize rendering - #437

Merged
Muscraft merged 7 commits into
rust-lang:mainfrom
epage:perf
Jul 7, 2026
Merged

perf: Optimize rendering#437
Muscraft merged 7 commits into
rust-lang:mainfrom
epage:perf

Conversation

@epage

@epage epage commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This is a port of astral-sh/ruff#24146

On my system, this took the simple benchmark from 7.041us to 5.166us. Having a benchmark longer than the us ranger would be a help for getting better numbers through these changes. Some were harder to tell if they made improvements with the amount of jitter I saw. That is partly why I left off a short circuit for puts on empty strings. I was unsure about the correctness of skipping adding a blank line and I saw almost no change in this benchmark.

Hoisting the line resizing would take this to 4.582us but there are some boundary conditions to examine as the raw change causes extra whitespace.

Comment thread src/renderer/render.rs Fixed
Comment thread src/renderer/render.rs Fixed
Comment thread src/renderer/render.rs Fixed
Comment thread src/renderer/styled_buffer.rs Fixed
Comment thread src/renderer/styled_buffer.rs Fixed
Comment thread src/renderer/styled_buffer.rs Fixed
@Muscraft
Muscraft merged commit bd50480 into rust-lang:main Jul 7, 2026
16 checks passed
@epage
epage deleted the perf branch July 7, 2026 21:05
epage added a commit to astral-sh/ruff that referenced this pull request Jul 28, 2026
## Summary

This is a first step towards #20411. After this, we would work to either
upstream changes or adjust things within Ruff.

Performance improvements have already been upstreamed
(rust-lang/annotate-snippets-rs#437,
rust-lang/annotate-snippets-rs#440)

Re-implemented the following from the fork:
- Hidden snippets
(rust-lang/annotate-snippets-rs#448)
- Renderer-level disabling of hyperlinks
- Jupyter cell indices
(rust-lang/annotate-snippets-rs#449)
- Fixable mark
(rust-lang/annotate-snippets-rs#450)
- `lineno_offset` (maybe not needed if #20648 is taken care of)
- anonymized lines affects origin
(rust-lang/annotate-snippets-rs#451)
- cut indicator (maybe not needed if we can switch to unicode renderer)

Additional hacks to minimize behavior change include
- Adjust upstream's hidden severity behavior to match Ruff's
(rust-lang/annotate-snippets-rs#452)
- How newlines are annotated
(rust-lang/annotate-snippets-rs#454)
- To not indent for line numbers when none will be present
(rust-lang/annotate-snippets-rs#453)
- Enabling `simd` by default

All of the above represents work that needs to be resolved to address
#20411.

`lib.name` hack is used to simplify maintenance with the assumption that
we'll continue working towards #20411.

Behavior changes that were included
- Tweak when implicit padding lines are present
- Fix Origin's line/column is now derived from the primary annotation
- Make consistent the rendering of primary/context annotations
- Fix snippets to show up for empty source (but may degrade #24528)
- Fix handling of term width
- Fix rendering of annotations between newline and first character
(granted, maybe the caller is wrong)
- Tweak multiline annotation markers

New or upcoming features annotate-snippets features that we will be able
to leverage by moving this forward
- `AnnotationKind::Visible` to improve the quality of built-in
code-folding if Ruff were to switch to it
- `DecorStyle::Unicode`
- *(upcoming)* No-graphiscs mode for screen readers (may also be useful
for AIs)

Deferred:
- Moving related `Group`s into the same `Report`
- Using `secondary_title` for any `Group` after the first in a `Report`
- Using a `Snippet`, instead of titles, for "virtual code" like
#27033 (comment)
- Switching hidden snippets to `Origin`

## Test Plan

Within cpython, I ran
```console
$ hyperfine -i "../../ruff/target-baseline/release/ruff check" "../../ruff/target-update/release/ruff check" -N -m20 --output=null
Benchmark 1: ../../ruff/target-baseline/release/ruff check
  Time (mean ± σ):      27.5 ms ±   1.1 ms    [User: 33.0 ms, System: 108.4 ms]
  Range (min … max):    25.0 ms …  31.7 ms    94 runs

  Warning: Ignoring non-zero exit code.

Benchmark 2: ../../ruff/target-update/release/ruff check
  Time (mean ± σ):      28.1 ms ±   2.0 ms    [User: 33.4 ms, System: 105.7 ms]
  Range (min … max):    24.8 ms …  38.9 ms    102 runs

  Warning: Ignoring non-zero exit code.
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use t
he '--warmup' or '--prepare' options.

Summary
  ../../ruff/target-baseline/release/ruff check ran
    1.03 ± 0.08 times faster than ../../ruff/target-update/release/ruff check
```

Before:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/2d94dabf-b28d-48c1-b17c-52aac59fd57e"
/>

After:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/b388ada3-e3d7-4ba0-b90b-1330eb81f4be"
/>
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.

3 participants