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

Optimize bottlenecks #72

Closed
wants to merge 12 commits into from
Closed
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: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ let package = Package(
.library(name: "PathKit", targets: ["PathKit"]),
],
dependencies: [
.package(url:"https://github.com/kylef/Spectre.git", .upToNextMinor(from:"0.9.0"))
.package(url:"https://github.com/kylef/Spectre.git", .upToNextMinor(from:"0.9.0")),
.package(url:"https://github.com/michaeleisel/PathKitCExt.git", .branch("master"))
],
targets: [
.target(name: "PathKit", dependencies: [], path: "Sources"),
.target(name: "PathKit", dependencies: ["PathKitCExt"], path: "Sources"),
.testTarget(name: "PathKitTests", dependencies: ["PathKit", "Spectre"], path:"Tests/PathKitTests")
]
)
1 change: 1 addition & 0 deletions PathKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Pod::Spec.new do |spec|
spec.social_media_url = 'http://twitter.com/kylefuller'
spec.source = { :git => 'https://github.com/kylef/PathKit.git', :tag => spec.version }
spec.source_files = 'Sources/PathKit.swift'
spec.dependency 'PathKitCExt', '1.0.2'
spec.ios.deployment_target = '8.0'
spec.osx.deployment_target = '10.9'
spec.tvos.deployment_target = '9.0'
Expand Down
43 changes: 37 additions & 6 deletions Sources/PathKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let system_glob = Darwin.glob
#endif

import Foundation
import PathKitCExt


/// Represents a filesystem path.
Expand Down Expand Up @@ -227,7 +228,11 @@ extension Path {
/// - Returns: the last path component
///
public var lastComponent: String {
return NSString(string: path).lastPathComponent
var component = NSString(string: path).lastPathComponent
// .lastPathComponent leaves us with a bridged NSString, which can be slow to perform operations like == on later.
// So, make it a swifty contiguous UTF-8 string here
component.makeContiguousUTF8()
Copy link
Author

Choose a reason for hiding this comment

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

it's important to call makeContiguousUTF8 if it's going to be used much later, e.g. for string comparison. it allows swift to use fast paths in the string code if possible

return component
}

/// The last path component without file extension
Expand All @@ -246,7 +251,7 @@ extension Path {
/// - Returns: all path components
///
public var components: [String] {
return NSString(string: path).pathComponents
return getComponents(string)
}

/// The file extension behind the last dot of the last component.
Expand Down Expand Up @@ -565,8 +570,18 @@ extension Path {
/// - Returns: paths to all files, directories and symbolic links contained in the directory
///
public func children() throws -> [Path] {
return try Path.fileManager.contentsOfDirectory(atPath: path).map {
self + Path($0)
return path.withCString { buffer in
var count = 0
var temp: UnsafeMutableRawPointer?
let contents = PATContentsAt(buffer, &count, &temp)!
var array = ContiguousArray<Path>()
for i in 0..<count {
let entity = String(cString: contents[i]!)
let path = self + Path(entity)
array.append(path)
}
PATFreePathComponents(contents, temp)
return Array(array)
}
}

Expand Down Expand Up @@ -755,8 +770,8 @@ internal func +(lhs: String, rhs: String) -> Path {
// Absolute paths replace relative paths
return Path(rhs)
} else {
var lSlice = NSString(string: lhs).pathComponents.fullSlice
var rSlice = NSString(string: rhs).pathComponents.fullSlice
var lSlice = getComponents(lhs).fullSlice
var rSlice = getComponents(rhs).fullSlice

// Get rid of trailing "/" at the left side
if lSlice.count > 1 && lSlice.last == Path.separator {
Expand Down Expand Up @@ -791,6 +806,22 @@ internal func +(lhs: String, rhs: String) -> Path {
}
}

func getComponents(_ string: String) -> [String] {
return string.withCString { (path) -> [String] in
var count = 0
var temp: UnsafeMutableRawPointer? = nil
let strings = PATPathComponents(path, &count, &temp)!
var components = ContiguousArray<String>()
components.reserveCapacity(count)
for i in 0..<count {
let string = String(cString: strings[i]!)
components.append(string)
}
PATFreePathComponents(strings, temp)
return Array(components)
}
}

extension Array {
var fullSlice: ArraySlice<Element> {
return self[self.indices.suffix(from: 0)]
Expand Down