-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PrettyPrinterPerformance Optimized the PrettyPrinter for #894 #901
Conversation
Thank you, @macshome, that's a great improvement. |
Looks like this fails to build using Swift 5.10. Maybe the |
Unfortunately, It's probably fine here to just write the loop manually instead of using that API. If you simultaneously keep track of the latest |
Will do. This explains why it was failing on Godbolt when including |
Head branch was pushed to by a user without write access
557d19f
to
a0f588d
Compare
I added a quick fix that is Swift 5 compatible and isn't too bad a hit on the performance. let lines = text.lazy.filter({ $0 == "\n" }).count I thought I had found a good solution using: text.enumerateLines {line, stop in
column = line.count
lineNumber += 1
} but all the enumerate API options are escaping and this is a struct. I looked at the implementation of I can keep looking to shave off more instructions with one forward pass if you like. |
a0f588d
to
f540f02
Compare
Shaved a few more instructions by finding the last newline in reverse. let lastNewlineRange = text.range(of: "\n", options: .backwards) |
I find it interesting that the https://godbolt.org/z/v9xjnq4qb I guess that the benefit of reading the text corpus backwards makes up for it. |
When you're looking at the instruction counts, are you getting those by running swift-format on a Mac or a Linux machine? Godbolt.org only has Linux machines AFAIK (at least for Swift), so if you're looking at Godbolt output there, you could be comparing something vastly different than what you would see on macOS. And even on recent versions of macOS, they've been porting Foundation from the old Obj-C implementation to the new Swift core, so the code you're executing could even vary significantly between versions there, too. Ultimately, I think this is still doing unnecessary work (either approach); in my original comment, I suggested having a single loop that counts the number of |
Another optimization would be to iterate over the |
Good point. I'll check the assembly on my Mac as well when working on additional optimizations. Thanks! |
(Edit: I see that I do have a question that isn't about the optimization work: should we be counting newlines or lines? This string: let text = """
This is some text
it has a lot
of
line
breaks.
""" has 5 Is the code handling this conversion of newlines to actual lines somewhere else so that we don't need to worry about it here? |
I've pretty much got this down to one forward pass that equals the original instruction count now. Just adding in the last tweaks. |
…mance PrettyPrinterPerformance Optimized the PrettyPrinter for swiftlang#894 Worked to get the perfomance to be closer to where we were before the changes in swiftlang#883. This code should be only about 1.5% slower rather than 7% slower. Using a lazy filter as `count(where:_)` isn't avaliable < Swift 6.0. One forward loop and using the UTF8 view makes this faster than the original code pre-swiftlang#883.
f540f02
to
87467ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work for beating the original baseline! 🚀
Co-authored-by: Alex Hoppen <alex@alexhoppen.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks for fixing the regression!
Worked to get the perfomance to be closer to where we were before the changes in #883. This code should be only about 1.5% slower rather than 7% slower.
Pre-changes: Instructions executed: 67167400341
After #883: Instructions executed: 71260352299
This PR: Instructions executed: 68259258073
Fixes #894.