-
Notifications
You must be signed in to change notification settings - Fork 872
Speed up search by using parallel Glob and Binary Search for including files checks #1122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import Foundation | ||
|
|
||
| public extension Array { | ||
|
|
||
| func parallelMap<T>(transform: (Element) -> T) -> [T] { | ||
| var result = ContiguousArray<T?>(repeating: nil, count: count) | ||
| return result.withUnsafeMutableBufferPointer { buffer in | ||
| DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in | ||
| buffer[idx] = transform(self[idx]) | ||
| } | ||
| return buffer.map { $0! } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Holds a sorted array, created from specified sequence | ||
| /// This structure is needed for the cases, when some part of application requires array to be sorted, but don't trust any inputs :) | ||
| public struct SortedArray<T: Comparable> { | ||
| public let value: Array<T> | ||
| public init<S: Sequence>(_ value: S) where S.Element == T { | ||
| self.value = value.sorted() | ||
| } | ||
| } | ||
|
|
||
| public extension SortedArray { | ||
| /// 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 firstIndex(where predicate: (T) 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 = value.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(value[midIndex]) { | ||
| goodIndex = midIndex | ||
| } else { | ||
| badIndex = midIndex | ||
| } | ||
| midIndex = (badIndex + goodIndex) / 2 | ||
| } | ||
|
|
||
| // We're out of bounds, no good items in array | ||
| if midIndex == lastIndex || goodIndex == lastIndex { | ||
| return nil | ||
| } | ||
| return goodIndex | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import XCTest | ||
| @testable import XcodeGenCore | ||
|
|
||
| class ArrayExtensionsTests: XCTestCase { | ||
|
||
|
|
||
| func testSearchingForFirstIndex() { | ||
| let array = SortedArray([1, 2, 3, 4 ,5]) | ||
| XCTAssertEqual(array.firstIndex(where: { $0 > 2 }), 2) | ||
| } | ||
|
|
||
| func testIndexCannotBeFound() { | ||
| let array = SortedArray([1, 2, 3, 4, 5]) | ||
| XCTAssertEqual(array.firstIndex(where: { $0 > 10 }), nil) | ||
| } | ||
|
|
||
| func testEmptyArray() { | ||
| let array = SortedArray([Int]()) | ||
| XCTAssertEqual(array.firstIndex(where: { $0 > 0 }), nil) | ||
| } | ||
|
|
||
| func testSearchingReturnsFirstIndexWhenMultipleElementsHaveSameValue() { | ||
| let array = SortedArray([1, 2, 3, 3 ,3]) | ||
| XCTAssertEqual(array.firstIndex(where: { $0 == 3 }), 2) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| class SortedArrayTests: XCTestCase { | ||
|
|
||
| func testSortingOnInitialization() { | ||
| let array = [1, 5, 4, 2] | ||
| let sortedArray = SortedArray(array) | ||
| XCTAssertEqual([1, 2, 4, 5], sortedArray.value) | ||
| } | ||
|
|
||
| func testEmpty() { | ||
| XCTAssertEqual([Int](), SortedArray([Int]()).value) | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you please add some tests for this
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.
Done