RFC: Upgrade format output to a type more efficient than `String`
Nobody has claimed this yet.
- 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.
- gradually replace the return type of formatting functions from
Option<String>toOption<FmtString>, whereFmtStringis just an alias forString - introduce a custom trait abstracting common string operations like
to_owned,push_strandpushand implement it forString - replace method calls to
Stringspecific functions with calls to methods in the abstract trait in the
relevant functions - the
format!macro is also highly specific toStrings. 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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