Open marcusklaas opened 7 years ago
I think we could get some of the way by just using a more efficient String in the FileMap and continuing using Rust strings in the visitors. That would be much easier to implement and should show the scale of the speedup we can expect
cc #338
Doesn't FileMap
already use a string variant that is at least fairly efficient for its task, namely StringBuffer
?
It does, or at least tries, but it might in fact not be very efficient, or we might be able to do better. Not sure if the profiling tells us about that?
Rustfmt spends a lot of time concatenating strings. This involves many allocations,
memcpy
s and deallocations. Common operations on types like ropes have much better time complexity than standard strings. Unfortunately, rustfmt is fairly fixed on using theseString
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.Option<String>
toOption<FmtString>
, whereFmtString
is just an alias forString
to_owned
,push_str
andpush
and implement it forString
String
specific functions with calls to methods in the abstract trait in the relevant functionsformat!
macro is also highly specific toStrings
. Replace these invocations by a custom macro call doing string-like concatenation using the new traitAfter 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