Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ struct VerticalParameterAlignmentRule: Rule {

private extension VerticalParameterAlignmentRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
// Computed once per file: `SourceLocationConverter.sourceLines` materializes every line of the
// file as a new `[String]` on each access, and `graphemeColumn(line:column:)` below reads it
// once per parameter, which is quadratic in the size of the file.
private lazy var sourceLines = locationConverter.sourceLines

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also have this cached as part of SwiftLintFile+Cache to make it available to all rules.


override func visitPost(_ node: FunctionDeclSyntax) {
violations.append(contentsOf: violations(for: node.signature.parameterClause.parameters))
}
Expand Down Expand Up @@ -52,7 +57,7 @@ private extension VerticalParameterAlignmentRule {
/// by multi-byte characters (e.g. a non-ASCII function name) are compared by their visible alignment
/// rather than by their byte offset.
private func graphemeColumn(line: Int, column: Int) -> Int {
guard let graphemeClusters = String(locationConverter.sourceLines[line - 1].utf8.prefix(column - 1)) else {
guard let graphemeClusters = String(sourceLines[line - 1].utf8.prefix(column - 1)) else {
return column
}
return graphemeClusters.count + 1
Expand Down