swift 3.0 syntax updates for preprocessor and performance tools

This commit is contained in:
Jamie Pinkham 2016-06-21 08:45:56 -04:00
parent a5f19de922
commit bc86d47a3f
3 changed files with 20 additions and 19 deletions

View File

@ -16,7 +16,7 @@ if Process.argc != 3 {
let sourceFilesRoot = Process.arguments[1]
let derivedData = Process.arguments[2]
let fileManager = NSFileManager()
let fileManager = FileManager()
func escape(value: String) -> String {
let escapedString = value.replacingOccurrences(of: "\n", with: "\\n")
@ -27,10 +27,11 @@ func escape(value: String) -> String {
}
func processFile(path: String, outputPath: String) -> String {
let rawContent = NSData(contentsOfFile: path)!
let content = NSString(data: rawContent, encoding: NSUTF8StringEncoding)! as String
let url = URL(fileURLWithPath: path)
let rawContent = try! Data(contentsOf: url)
let content = String(data: rawContent, encoding: String.Encoding.utf8)
let components = content.components(separatedBy: "<%")
guard let components = content?.components(separatedBy: "<%") else { return "" }
var functionContentComponents: [String] = []
functionContentComponents.append("var components: [String] = [\"// This file is autogenerated. Take a look at `Preprocessor` target in RxSwift project \\n\"]\n")
@ -46,7 +47,7 @@ func processFile(path: String, outputPath: String) -> String {
let suffix = codePlusSuffixSeparated[1]
if code.hasPrefix("=") {
functionContentComponents.append("components.append(String(\(code.substring(from: code.startIndex.successor()))))\n")
functionContentComponents.append("components.append(String(\(code.substring(from: code.index(after: code.startIndex)))))\n")
}
else {
functionContentComponents.append("\(code)\n")
@ -55,15 +56,15 @@ func processFile(path: String, outputPath: String) -> String {
functionContentComponents.append("components.append(\(escape(value: suffix)));\n")
}
functionContentComponents.append("try! components.joinWithSeparator(\"\").writeToFile(\"\(outputPath)\", atomically: false, encoding: NSUTF8StringEncoding)")
functionContentComponents.append("try! components.join(withSeparator:\"\").write(toFile:\"\(outputPath)\", atomically: false, encoding: NSUTF8StringEncoding)")
return functionContentComponents.joined(separator: "")
}
func runCommand(path: String) {
_ = NSProcessInfo().processIdentifier
_ = ProcessInfo().processIdentifier
let task = NSTask()
let task = Task()
task.launchPath = "/bin/bash"
task.arguments = ["-c", "xcrun swift \"\(path)\""]
@ -71,7 +72,7 @@ func runCommand(path: String) {
task.waitUntilExit()
if task.terminationReason != NSTaskTerminationReason.exit {
if task.terminationReason != Task.TerminationReason.exit {
exit(-1)
}
}
@ -88,8 +89,8 @@ for file in files {
print(file)
let path = (sourceFilesRoot as NSString).appendingPathComponent(file as String)
let outputPath = path.substring(to: path.endIndex.predecessor().predecessor().predecessor()) + ".swift"
let endIndex = path.index(before: path.index(before: path.index(before: path.endIndex)))
let outputPath = path.substring(to: endIndex) + ".swift"
generateAllFiles.append("_ = { () -> Void in\n\(processFile(path: path, outputPath: outputPath))\n}()\n")
}
@ -98,7 +99,7 @@ let script = generateAllFiles.joined(separator: "")
let scriptPath = (derivedData as NSString).appendingPathComponent("_preprocessor.sh")
do {
try script.write(toFile: scriptPath, atomically: true, encoding: NSUTF8StringEncoding)
try script.write(toFile: scriptPath, atomically: true, encoding: String.Encoding.utf8)
} catch _ {
}
runCommand(path: scriptPath)
runCommand(path: scriptPath)

View File

@ -132,7 +132,7 @@ func approxValuePerIteration(_ total: UInt64) -> UInt64 {
return UInt64(round(Double(total) / Double(NumberOfIterations)))
}
func measureTime(@noescape _ work: () -> ()) -> UInt64 {
func measureTime( _ work: @noescape() -> ()) -> UInt64 {
var timebaseInfo: mach_timebase_info = mach_timebase_info()
let res = mach_timebase_info(&timebaseInfo)
@ -147,7 +147,7 @@ func measureTime(@noescape _ work: () -> ()) -> UInt64 {
return approxValuePerIteration(timeInNano) / 1000
}
func measureMemoryUsage(@noescape _ work: () -> ()) -> (bytesAllocated: UInt64, allocations: UInt64) {
func measureMemoryUsage( _ work: @noescape() -> ()) -> (bytesAllocated: UInt64, allocations: UInt64) {
let (bytes, allocations) = getMemoryInfo()
for _ in 0 ..< NumberOfIterations {
work()
@ -159,7 +159,7 @@ func measureMemoryUsage(@noescape _ work: () -> ()) -> (bytesAllocated: UInt64,
var fragmentedMemory = false
func compareTwoImplementations(_ benchmarkTime: Bool, benchmarkMemory: Bool, @noescape first: () -> (), @noescape second: () -> ()) {
func compareTwoImplementations(_ benchmarkTime: Bool, benchmarkMemory: Bool, first: @noescape() -> (), second: @noescape() -> ()) {
if !fragmentedMemory {
print("Fragmenting memory ...")
fragmentMemory()

View File

@ -19,7 +19,7 @@ func allocation() {
}
repeat {
compareTwoImplementations(benchmarkTime: true, benchmarkMemory: false, first: {
compareTwoImplementations(true, benchmarkMemory: false, first: {
let publishSubject = PublishSubject<Int>()
//let a = Observable.just(1)
@ -55,10 +55,10 @@ compareTwoImplementations(benchmarkTime: true, benchmarkMemory: false, first: {
for i in 0..<100 {
publishSubject.on(.Next(i))
publishSubject.on(.next(i))
}
}, second: {
})
} while true
} while true