Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Next Version

### Changed

- Speed up source inclusion checking for big projects [#1122](https://github.com/yonaskolb/XcodeGen/pull/1122) @PaulTaykalo

## 2.25.0

### Added
Expand Down
79 changes: 79 additions & 0 deletions Sources/XcodeGenCore/ArrayExtensions.swift
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? {
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// 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
}
}
29 changes: 19 additions & 10 deletions Sources/XcodeGenCore/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ public class Glob: Collection {
}

let patterns = behavior.supportsGlobstar ? expandGlobstar(pattern: adjustedPattern) : [adjustedPattern]

for pattern in patterns {
var gt = glob_t()
if executeGlob(pattern: pattern, gt: &gt) {
populateFiles(gt: gt, includeFiles: includeFiles)
}

globfree(&gt)
}

#if os(macOS)
paths = patterns.parallelMap { paths(usingPattern: $0, includeFiles: includeFiles) }.flatMap { $0 }
#else
// Parallel invocations of Glob on Linux seems to be causing unexpected crashes
paths = patterns.map { paths(usingPattern: $0, includeFiles: includeFiles) }.flatMap { $0 }
#endif

paths = Array(Set(paths)).sorted { lhs, rhs in
lhs.compare(rhs) != ComparisonResult.orderedDescending
Expand Down Expand Up @@ -209,7 +207,17 @@ public class Glob: Collection {
isDirectoryCache.removeAll()
}

private func populateFiles(gt: glob_t, includeFiles: Bool) {
private func paths(usingPattern pattern: String, includeFiles: Bool) -> [String] {
var gt = glob_t()
defer { globfree(&gt) }
if executeGlob(pattern: pattern, gt: &gt) {
return populateFiles(gt: gt, includeFiles: includeFiles)
}
return []
}

private func populateFiles(gt: glob_t, includeFiles: Bool) -> [String] {
var paths = [String]()
let includeDirectories = behavior.includesDirectoriesInResults

#if os(macOS)
Expand All @@ -229,6 +237,7 @@ public class Glob: Collection {
paths.append(path)
}
}
return paths
}
}

Expand Down
22 changes: 13 additions & 9 deletions Sources/XcodeGenKit/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,23 @@ class SourceGenerator {
}

/// Checks whether the path is not in any default or TargetSource excludes
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: Set<Path>) -> Bool {
!defaultExcludedFiles.contains(where: { path.lastComponent.contains($0) })
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) -> Bool {
return !defaultExcludedFiles.contains(where: { path.lastComponent == $0 })
&& !(path.extension.map(defaultExcludedExtensions.contains) ?? false)
&& !excludePaths.contains(path)
// If includes is empty, it's included. If it's not empty, the path either needs to match exactly, or it needs to be a direct parent of an included path.
&& (includePaths.isEmpty || includePaths.contains(where: { includedFile in
if path == includedFile { return true }
return includedFile.description.contains(path.description)
}))
&& (includePaths.value.isEmpty || _isIncludedPathSorted(path, sortedPaths: includePaths))
}

private func _isIncludedPathSorted(_ path: Path, sortedPaths: SortedArray<Path>) -> Bool {
guard let idx = sortedPaths.firstIndex(where: { $0 >= path }) else { return false }
let foundPath = sortedPaths.value[idx]
return foundPath.description.hasPrefix(path.description)
}


/// Gets all the children paths that aren't excluded
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: Set<Path>) throws -> [Path] {
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) throws -> [Path] {
try dirPath.children()
.filter {
if $0.isDirectory {
Expand Down Expand Up @@ -408,7 +412,7 @@ class SourceGenerator {
isBaseGroup: Bool,
hasCustomParent: Bool,
excludePaths: Set<Path>,
includePaths: Set<Path>,
includePaths: SortedArray<Path>,
buildPhases: [Path: BuildPhaseSpec]
) throws -> (sourceFiles: [SourceFile], groups: [PBXGroup]) {

Expand Down Expand Up @@ -634,7 +638,7 @@ class SourceGenerator {
isBaseGroup: true,
hasCustomParent: hasCustomParent,
excludePaths: excludePaths,
includePaths: includePaths,
includePaths: SortedArray(includePaths),
buildPhases: buildPhases
)

Expand Down
40 changes: 40 additions & 0 deletions Tests/XcodeGenCoreTests/ArrayExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import XCTest
@testable import XcodeGenCore

class ArrayExtensionsTests: XCTestCase {
Copy link
Owner

Choose a reason for hiding this comment

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

Another thing the SortedArray does is sort the elements it's initialised with if we could test that too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


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)
}

}