rust-lang / rust-lang/rustfmt

RFC: Upgrade format output to a type more efficient than `String`

Open
#2,156 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-performance C-discussion
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Rustfmt spends a lot of time concatenating strings. This involves many allocations, memcpys and deallocations. Common operations on types like ropes have much better time complexity than standard strings. Unfortunately, rustfmt is fairly fixed on using these String values to represent formatted code. Ideally, the formatting code should be more flexible as to its output type. That way, it would be very easy to experiment with other types and observe their effects on performance. Below is a rough proposal on how this could be achieved gradually, without a huge overhaul or breakage.

  1. gradually replace the return type of formatting functions from Option<String> to Option<FmtString>, where FmtString is just an alias for String
  2. introduce a custom trait abstracting common string operations like to_owned, push_str and push and implement it for String
  3. replace method calls to String specific functions with calls to methods in the abstract trait in the
    relevant functions
  4. the format! macro is also highly specific to Strings. Replace these invocations by a custom macro call doing string-like concatenation using the new trait

After this, switching to a new output type should be as simple as implementing this custom trait for your favourite string representation and setting the FmtString alias to it.

This will still be a very big undertaking, but I believe that steps 1 through 4 can be done without breaking anything. Some steps can even be done partially. And it should be worth the effort, as there are many performance gains to be had.

Proof of concept

Contributor guide

Open the contributing guide

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.

Research direction

No source files or tests are named. Start by reviewing the linked proof of concept and locating rustfmt's formatting functions that return or manipulate String values. The issue considers the work done when those operations use an output abstraction while preserving existing behavior and allowing another string representation to be substituted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance, tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.