Skip to content

Use a concrete buffer for ordinary writes - #29819

Merged
charliermarsh merged 4 commits into
mainfrom
crmarsh/formatter-concrete-writer
Oct 30, 2026
Merged

charliermarsh merged 4 commits into
mainfrom
crmarsh/formatter-concrete-writer

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Formatter currently writes every element through &mut dyn Buffer, even when the destination is a vector. We now construct a formatter with direct references to the vector and formatting state when entering through VecBuffer::write_fmt:

Workload Main This PR Time change
NumPy globals 29.72 Β΅s 26.44 Β΅s βˆ’11.05%
Unicode pypinyin 107.40 Β΅s 103.66 Β΅s βˆ’3.48%
Pydantic types 755.86 Β΅s 739.81 Β΅s βˆ’2.12%
NumPy ctypeslib 406.22 Β΅s 387.68 Β΅s βˆ’4.57%
Large dataset 1,662.46 Β΅s 1,597.33 Β΅s βˆ’3.92%

This experiments with the dispatch part of Oxc #27559.

@charliermarsh charliermarsh added formatter Related to the Ruff formatter performance Potential performance improvement labels Oct 29, 2026 — with ChatGPT Codex Connector
@astral-automations-bot

astral-automations-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Formatter (stable)

βœ… ecosystem check detected no format changes.

Formatter (preview)

βœ… ecosystem check detected no format changes.

Formatter (stable) Β· Formatter (preview)


Results for 580f594 Β· Workflow run

@codspeed

codspeed Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 4.77%

⚑ 2 improved benchmarks
βœ… 58 untouched benchmarks
⏩ 238 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚑ CPU Simulation formatter[large/dataset.py] 16.3 ms 15.5 ms +4.93%
⚑ CPU Simulation formatter[pydantic/types.py] 6.5 ms 6.2 ms +4.6%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing crmarsh/formatter-concrete-writer (580f594) with main (52cca7e)

Open in CodSpeed

Footnotes

  1. 238 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@charliermarsh
charliermarsh marked this pull request as ready for review October 29, 2026 19:33
@MichaReiser MichaReiser changed the title [formatter] Use a concrete buffer for ordinary writes Use a concrete buffer for ordinary writes Oct 30, 2026

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I suggest using Vec(&mut VecBuffer) over destructuring its fields. It keeps the intended isolation and avoids repeating logic.

Comment thread crates/ruff_formatter/src/formatter.rs Resolved
Comment thread crates/ruff_formatter/src/formatter.rs Resolved
}

impl<'src> DocstringLinePrinter<'_, '_, '_, 'src> {
impl<'src> DocstringLinePrinter<'_, '_, '_, '_, 'src> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful πŸ˜†

@charliermarsh
charliermarsh merged commit 86b7fd1 into main Oct 30, 2026
55 checks passed
@charliermarsh
charliermarsh deleted the crmarsh/formatter-concrete-writer branch October 30, 2026 20:16

This branch was previously deployed

1 inactive deployment
automations β€” 580f5942 Deployed Oct 30, 2026 by charliermarsh via security-review / security review #96360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the Ruff formatter performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants