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

Faster tokens resolving in syntaxmap #2916

Merged
merged 7 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
[PaulTaykalo](https://github.com/PaulTaykalo)
[#2918](https://github.com/realm/SwiftLint/issues/2918)

* Speed up syntax token lookups, which can improve performance when linting
large files.
[PaulTaykalo](https://github.com/PaulTaykalo)
[#2916](https://github.com/realm/SwiftLint/issues/2916)

* Add GitHub Actions Logging reporter (`github-actions-logging`).
[Norio Nomura](https://github.com/norio-nomura)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extension RandomAccessCollection where Index == Int {
/// Returns the first index in which an element of the collection satisfies the given predicate.
/// The collection assumed to be sorted. If collection is not have sorted values the result is undefined.
///
/// The idea is to get first index of a function for which the given predicate evaluates to true.
///
/// let values = [1,2,3,4,5]
/// let idx = values.firstIndexAssumingSorted(where: { $0 > 3 })
///
/// // false, false, false, true, true
/// // ^
/// // therefore idx == 3
///
/// - Parameter predicate: A closure that takes an element as its argument
/// and returns a Boolean value that indicates whether the passed element
/// represents a match.
///
/// - Returns: The index of the first element for which `predicate` returns
/// `true`. If no elements in the collection satisfy the given predicate,
/// returns `nil`.
///
/// - Complexity: O(log(*n*)), where *n* is the length of the collection.
@inlinable
func firstIndexAssumingSorted(where predicate: (Self.Element) throws -> Bool) rethrows -> Int? {
// Predicate should divide a collection to two pairs of values
// "bad" values for which predicate returns `false``
// "good" values for which predicate return `true`

// false false false false false true true true
// ^
// The idea is to get _first_ index which for which the predicate returns `true`

let lastIndex = count

// The index that represents where bad values start
var badIndex = -1

// The index that represents where good values start
var goodIndex = lastIndex
var midIndex = (badIndex + goodIndex) / 2

while badIndex + 1 < goodIndex {
if try predicate(self[midIndex]) {
goodIndex = midIndex
} else {
badIndex = midIndex
}
midIndex = (badIndex + goodIndex) / 2
}

// We're out of bounds, no good items in array
if midIndex == lastIndex {
return nil
}
return goodIndex
}
}
16 changes: 13 additions & 3 deletions Source/SwiftLintFramework/Extensions/SyntaxMap+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ extension SyntaxMap {
.intersects(byteRange)
}

guard let startIndex = tokens.firstIndex(where: intersect) else {
func intersectsOrAfter(_ token: SyntaxToken) -> Bool {
return token.offset + token.length > byteRange.location
}

guard let startIndex = tokens.firstIndexAssumingSorted(where: intersectsOrAfter) else {
return []
}
let tokensBeginningIntersect = tokens.lazy.suffix(from: startIndex)
return Array(tokensBeginningIntersect.filter(intersect))

let tokensAfterFirstIntersection = tokens
.lazy
.suffix(from: startIndex)
.prefix(while: { $0.offset < byteRange.upperBound })
.filter(intersect)

return Array(tokensAfterFirstIntersection)
}

internal func kinds(inByteRange byteRange: NSRange) -> [SyntaxKind] {
Expand Down
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
D93DA3D11E699E6300809827 /* NestingConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D93DA3CF1E699E4E00809827 /* NestingConfiguration.swift */; };
DAD3BE4A1D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD3BE491D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift */; };
E315B83C1DFA4BC500621B44 /* DynamicInlineRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E315B83B1DFA4BC500621B44 /* DynamicInlineRule.swift */; };
E4A6CF752363CBFB00DD5B18 /* RandomAccessCollection+Swiftlint.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A6CF742363CBFB00DD5B18 /* RandomAccessCollection+Swiftlint.swift */; };
E57B23C11B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */; };
E802ED001C56A56000A35AE1 /* Benchmark.swift in Sources */ = {isa = PBXBuildFile; fileRef = E802ECFF1C56A56000A35AE1 /* Benchmark.swift */; };
E80746F61ECB722F00548D31 /* CacheDescriptionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = E80746F51ECB722F00548D31 /* CacheDescriptionProvider.swift */; };
Expand Down Expand Up @@ -866,6 +867,7 @@
D93DA3CF1E699E4E00809827 /* NestingConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestingConfiguration.swift; sourceTree = "<group>"; };
DAD3BE491D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivateOutletRuleConfiguration.swift; sourceTree = "<group>"; };
E315B83B1DFA4BC500621B44 /* DynamicInlineRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicInlineRule.swift; sourceTree = "<group>"; };
E4A6CF742363CBFB00DD5B18 /* RandomAccessCollection+Swiftlint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "RandomAccessCollection+Swiftlint.swift"; sourceTree = "<group>"; };
E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReturnArrowWhitespaceRule.swift; sourceTree = "<group>"; };
E5A167C81B25A0B000CF2D03 /* OperatorFunctionWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperatorFunctionWhitespaceRule.swift; sourceTree = "<group>"; };
E802ECFF1C56A56000A35AE1 /* Benchmark.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Benchmark.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1654,6 +1656,7 @@
3BA79C9A1C4767910057E705 /* NSRange+SwiftLint.swift */,
3BB47D841C51D80000AE6A10 /* NSRegularExpression+SwiftLint.swift */,
E81619521BFC162C00946723 /* QueuedPrint.swift */,
E4A6CF742363CBFB00DD5B18 /* RandomAccessCollection+Swiftlint.swift */,
6C1D763121A4E69600DEF783 /* Request+DisableSourceKit.swift */,
E88DEA721B0984C400A66CB0 /* String+SwiftLint.swift */,
B39353F28BCCA39247B316BD /* String+XML.swift */,
Expand Down Expand Up @@ -2216,6 +2219,7 @@
6C15818D237026AC00F582A2 /* GitHubActionsLoggingReporter.swift in Sources */,
62FE5D32200CABDD00F68793 /* DiscouragedOptionalCollectionExamples.swift in Sources */,
D49896F12026B36C00814A83 /* RedundantSetAccessControlRule.swift in Sources */,
E4A6CF752363CBFB00DD5B18 /* RandomAccessCollection+Swiftlint.swift in Sources */,
29FFC37A1F15764D007E4825 /* FileLengthRuleConfiguration.swift in Sources */,
ED641C3820AA07B400212C62 /* NoFallthroughOnlyRule.swift in Sources */,
D4B31ADC22A0581B000300F1 /* DuplicateEnumCasesRule.swift in Sources */,
Expand Down