From 7fa100a033b2df1d32471701541ecaae69a7c4b4 Mon Sep 17 00:00:00 2001 From: Norio Nomura Date: Sun, 10 Jan 2016 20:39:31 +0900 Subject: [PATCH] Reduce calls to `String.characters.count` in `LineLengthRule.validateFile(_:)` By applying this, the duration of linting Carthage is reduced from: ``` swiftlint lint 20.07s user 1.10s system 84% cpu 25.023 total ``` to: ``` swiftlint lint 18.27s user 1.05s system 84% cpu 22.769 total ``` --- CHANGELOG.md | 3 +++ Source/SwiftLintFramework/Rules/LineLengthRule.swift | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f25bc6edb..305936f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ * Improve performance by reducing calls to SourceKit. [Norio Nomura](https://github.com/norio-nomura) +* Improve performance of `LineLengthRule`. + [Norio Nomura](https://github.com/norio-nomura) + ##### Bug Fixes * AutoCorrect for TrailingNewlineRule only removes at most one line. diff --git a/Source/SwiftLintFramework/Rules/LineLengthRule.swift b/Source/SwiftLintFramework/Rules/LineLengthRule.swift index 8cf222080..b1bd3a722 100644 --- a/Source/SwiftLintFramework/Rules/LineLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/LineLengthRule.swift @@ -30,12 +30,13 @@ public struct LineLengthRule: ParameterizedRule { public func validateFile(file: File) -> [StyleViolation] { return file.lines.flatMap { line in - for param in parameters.reverse() where line.content.characters.count > param.value { + let length = line.content.characters.count + for param in parameters.reverse() where length > param.value { return StyleViolation(ruleDescription: self.dynamicType.description, severity: param.severity, location: Location(file: file.path, line: line.index), reason: "Line should be \(parameters.first!.value) characters or less: " + - "currently \(line.content.characters.count) characters") + "currently \(length) characters") } return nil }