Skip to content

Commit 1ef55ae

Browse files
committed
reduce string processing overhead
1 parent be3cc0b commit 1ef55ae

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

BuildTimeAnalyzer/CompileMeasure.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ import Foundation
3737
}
3838

3939
init?(time: Double, rawPath: String, code: String, references: Int) {
40-
let untrimmedFilename = rawPath.split(separator: "/").map(String.init).last
41-
42-
guard let filepath = rawPath.split(separator: ":").map(String.init).first,
43-
let filename = untrimmedFilename?.split(separator: ":").map(String.init).first else { return nil }
44-
45-
let locationString = String(rawPath[filepath.endIndex...].dropFirst())
46-
let locations = locationString.split(separator: ":").compactMap{ Int(String.init($0)) }
40+
let untrimmedFilename: Substring
41+
if let lastIdx = rawPath.lastIndex(of: "/") {
42+
untrimmedFilename = rawPath.suffix(from: rawPath.index(after: lastIdx))
43+
} else {
44+
untrimmedFilename = rawPath[...]
45+
}
46+
let filepath = rawPath.prefix(while: {$0 != ":"})
47+
let filename = untrimmedFilename.prefix(while: {$0 != ":"})
48+
let locations = untrimmedFilename.split(separator: ":").dropFirst().compactMap({Int(String($0))})
4749
guard locations.count == 2 else { return nil }
4850

4951
self.time = time
5052
self.code = code
51-
self.path = filepath
52-
self.filename = filename
53+
self.path = String(filepath)
54+
self.filename = String(filename)
5355
self.locationArray = locations
5456
self.references = references
5557
}

BuildTimeAnalyzer/LogProcessor.swift

100644100755
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Foundation
77

88
typealias CMUpdateClosure = (_ result: [CompileMeasure], _ didComplete: Bool, _ didCancel: Bool) -> ()
99

10+
fileprivate let regex = try! NSRegularExpression(pattern: "^\\d*\\.?\\d*ms\\t/", options: [])
11+
1012
protocol LogProcessorProtocol: class {
1113
var rawMeasures: [String: RawMeasure] { get set }
1214
var updateHandler: CMUpdateClosure? { get set }
@@ -32,25 +34,30 @@ extension LogProcessorProtocol {
3234
// MARK: Private methods
3335

3436
private func process(text: String) {
37+
let text = text as NSString
3538
let characterSet = CharacterSet(charactersIn:"\r\"")
36-
var remainingRange = text.startIndex..<text.endIndex
37-
let regex = try! NSRegularExpression(pattern: "^\\d*\\.?\\d*ms\\t/", options: [])
39+
var remainingRange = NSMakeRange(0, text.length)
3840

3941
rawMeasures.removeAll()
4042

4143
processingDidStart()
4244

43-
while let nextRange = text.rangeOfCharacter(from: characterSet, options: [], range: remainingRange) {
44-
let text = String(text[remainingRange.lowerBound..<nextRange.upperBound])
45-
45+
while true {
46+
let nextRange = text.rangeOfCharacter(from: characterSet, options: .literal, range: remainingRange)
47+
guard nextRange.location != NSNotFound else { break }
48+
49+
let beginIdx = remainingRange.lowerBound
50+
let endIdx = nextRange.upperBound
51+
let textCount = endIdx - beginIdx
52+
let text = text.substring(with: NSMakeRange(beginIdx, textCount))
53+
4654
defer {
47-
remainingRange = nextRange.upperBound..<remainingRange.upperBound
55+
remainingRange = NSMakeRange(endIdx, remainingRange.upperBound - endIdx)
4856
}
4957

50-
// From LuizZak: (text as NSString).length improves the performance by about 2x compared to text.characters.count
51-
let range = NSMakeRange(0, (text as NSString).length)
58+
let range = NSMakeRange(0, textCount)
5259
guard let match = regex.firstMatch(in: text, options: [], range: range) else { continue }
53-
60+
5461
let timeString = text[..<text.index(text.startIndex, offsetBy: match.range.length - 4)]
5562
if let time = Double(timeString) {
5663
let value = String(text[text.index(text.startIndex, offsetBy: match.range.length - 1)...])

0 commit comments

Comments
 (0)