From ceaff318d601d67c36facf8b01cb8b412d97fb56 Mon Sep 17 00:00:00 2001 From: Samuel Sainz Date: Tue, 24 May 2022 11:09:18 -0300 Subject: [PATCH] Fixed SwiftLint issues and CR improvements --- .../Dependencies/DependenciesReader.swift | 54 ++---------- .../DependenciesReaderPerformanceTest.swift | 83 ++----------------- .../dependencies.d | 0 3 files changed, 17 insertions(+), 120 deletions(-) rename Tests/XCRemoteCacheTests/TestData/Dependencies/{DependenciesReaderTests => DependenciesReaderPerformanceTest}/dependencies.d (100%) diff --git a/Sources/XCRemoteCache/Dependencies/DependenciesReader.swift b/Sources/XCRemoteCache/Dependencies/DependenciesReader.swift index 26c4afc..6e18c85 100644 --- a/Sources/XCRemoteCache/Dependencies/DependenciesReader.swift +++ b/Sources/XCRemoteCache/Dependencies/DependenciesReader.swift @@ -108,8 +108,9 @@ public class FileDependenciesReader: DependenciesReader { return yaml } - /// Parses the String to get the list of files. - /// It iterates over the String using its UTF8View since it is more performant (String type operates in a higher abstraction level and includes features that impact the performance) + /// Parses the String to get the list of files + /// It iterates over the String using its UTF8View since it is more performant (String type operates + /// in a higher abstraction level and supports features that have a negative impact in the performance) /// It supports escaping whitespace charaters, prefixed with "\\" /// - Parameter string: string of whitespace charaters separated file paths /// - Returns: Array of all file paths @@ -117,8 +118,9 @@ public class FileDependenciesReader: DependenciesReader { var result: [String] = [] var prevChar: UTF8.CodeUnit? - // These index are used to move over the UTF8View of the string. - // The goal is to optimize the memory used, since UTF8View uses the same memory as the original String without copying it. + // These index are used to move over the UTF8View of the string + // The goal is to optimize the memory used, since UTF8View uses + // the same memory as the original String without copying it var startIndex = string.utf8.startIndex var endIndex = startIndex @@ -151,7 +153,8 @@ public class FileDependenciesReader: DependenciesReader { startIndex = string.utf8.index(after: endIndex) endIndex = startIndex default: - // As long as it is possible the indexes are used to track the range of the string that will be included in the file path (until it ends or until a backslash is found) + // As long as it is possible the indexes are used to track the range of the string that + // will be included in the file path (until it ends or until a backslash is found) endIndex = string.utf8.index(after: endIndex) // The char is assigned as the previous char prevChar = c @@ -165,45 +168,4 @@ public class FileDependenciesReader: DependenciesReader { return result } - - - /// Splits space or new line separated files into a set of files - /// It supports escaping whitespace charaters, prefixed with "\\" - /// - Parameter string: string of whitespace charaters separated file paths - /// - Returns: Array of all file paths - @available(*, deprecated, message: "Deprecated in favor of parseDependencyFileList which is more performant") - func splitDependencyFileList(_ string: String) -> [String] { - struct ParseState { - var buffer: String = "" - var prevChar: Character? - var result: [String] = [] - func with(buffer: String? = nil, prevChar: Character? = nil, result: [String]? = nil) -> ParseState { - var new = self - new.buffer = buffer ?? new.buffer - new.prevChar = prevChar ?? new.prevChar - new.result = result ?? new.result - return new - } - } - let parseResult = string.reduce(ParseState()) { total, char in - switch char { - case "\n" where total.prevChar == "\\": - return total - case " " where total.buffer.isEmpty: - return total - case " " where total.prevChar == "\\": - return total.with(buffer: "\(total.buffer) ") - case " ": - return total.with(buffer: "", prevChar: nil, result: total.result + [total.buffer]) - case "\\": - return total.with(prevChar: "\\") - default: - return total.with(buffer: "\(total.buffer)\(char)", prevChar: char, result: total.result) - } - } - if !parseResult.buffer.isEmpty { - return parseResult.result + [parseResult.buffer] - } - return parseResult.result - } } diff --git a/Tests/XCRemoteCacheTests/Dependencies/DependenciesReaderPerformanceTest.swift b/Tests/XCRemoteCacheTests/Dependencies/DependenciesReaderPerformanceTest.swift index dda320c..1849115 100644 --- a/Tests/XCRemoteCacheTests/Dependencies/DependenciesReaderPerformanceTest.swift +++ b/Tests/XCRemoteCacheTests/Dependencies/DependenciesReaderPerformanceTest.swift @@ -23,7 +23,7 @@ import XCTest class DependenciesReaderPerformanceTest: XCTestCase { - private static let resourcesSubdirectory = "TestData/Dependencies/DependenciesReaderTests" + private static let resourcesSubdirectory = "TestData/Dependencies/DependenciesReaderPerformanceTest" private func pathForTestData(name: String) throws -> URL { return try XCTUnwrap(Bundle.module.url( @@ -101,21 +101,6 @@ class DependenciesReaderPerformanceTest: XCTestCase { } } - func testDeprecatedParseDependenciesFilesList() throws { - let file = try pathForTestData(name: "dependencies") - let reader = FileDependenciesReader(file, accessor: FileManager.default) - let fileData = try reader.getFileData() - let fileString = try reader.getFileStringFromData(fileData: fileData) - let yaml = try reader.getYaml(fileString: fileString) - - guard let dependencies = yaml["dependencies"] else { XCTAssertTrue(false); return } - - self.measure { // 0.039 - let deps = reader.splitDependencyFileList(dependencies) - XCTAssertTrue(deps.count == 1000) - } - } - func testParseDependencyFileListUsingUTF8View() throws { let file = try pathForTestData(name: "dependencies") let reader = FileDependenciesReader(file, accessor: FileManager.default) @@ -123,7 +108,10 @@ class DependenciesReaderPerformanceTest: XCTestCase { let fileString = try reader.getFileStringFromData(fileData: fileData) let yaml = try reader.getYaml(fileString: fileString) - guard let dependencies = yaml["dependencies"] else { XCTAssertTrue(false); return } + guard let dependencies = yaml["dependencies"] else { + XCTAssertTrue(false) + return + } self.measure { // 0.004 let deps = reader.parseDependencyFileList(dependencies) @@ -131,42 +119,6 @@ class DependenciesReaderPerformanceTest: XCTestCase { } } - func testResultsFromBothParsersAreEqual() throws { - let file = try pathForTestData(name: "dependencies") - let reader = FileDependenciesReader(file, accessor: FileManager.default) - let fileData = try reader.getFileData() - let fileString = try reader.getFileStringFromData(fileData: fileData) - let yaml = try reader.getYaml(fileString: fileString) - - guard let dependencies = yaml["dependencies"] else { XCTAssertTrue(false); return } - - let deps1 = reader.splitDependencyFileList(dependencies) - let deps2 = reader.parseDependencyFileList(dependencies) - - XCTAssertEqual(deps1.count, deps2.count) - for i in 0..