Skip to content
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

Optimize pretty printing performance #913

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

bnbarham
Copy link
Contributor

@bnbarham bnbarham commented Jan 10, 2025

#883 fixed outputting incorrect line numbers, but introduced a performance regression. #901 improved this back to around the original, but had to be reverted as it introduced a an issue due to counting codepoints rather than characters.

Introduce a similar optimization again, but only for the first portion of the string (prior to the last newline).

Fixes #894 again.

@bnbarham
Copy link
Contributor Author

CC @macshome

@bnbarham
Copy link
Contributor Author

bnbarham commented Jan 10, 2025

Some instruction counts (for formatting swift-syntax 509.0.0):

Still worse than it was, but we're doing a fair bit more work so that seems inevitable.

@rintaro
Copy link
Member

rintaro commented Jan 10, 2025

I would do:

    if let lastNewlineIndex = text.utf8.lastIndex(of: UInt8(ascii: "\n")) {
      let lastLineStart = text.utf8.index(after: lastNewlineIndex)
      lineNumber += text[..<lastLineStart].utf8.count(where: {$0 == UInt8(ascii: "\n")})
      column = text[lastLineStart...].count
    } else {
      column += text.count
    }

swift-format#883 fixed outputting incorrect line numbers, but introduced
a performance regression. swift-format#901 improved this back to around
the original, but had to be reverted as it introduced a an issue due to
counting codepoints rather than characters.

Introduce a similar optimization again, but only for the first portion
of the string (prior to the last newline).

Fixes swift-format#894 again.
@bnbarham
Copy link
Contributor Author

Ah, thanks @rintaro. We're now down to 68032409793 👍

@bnbarham bnbarham merged commit 6e64b26 into swiftlang:main Jan 10, 2025
19 checks passed
@bnbarham bnbarham deleted the pretty-print-perf branch January 10, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#883 regressed performance by ~7%
3 participants