Skip to content

Commit 064e9c6

Browse files
committed
improve performance
1 parent 1ef55ae commit 064e9c6

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

BuildTimeAnalyzer.xcodeproj/xcshareddata/xcschemes/BuildTimeAnalyzer.xcscheme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "1020"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -59,6 +59,7 @@
5959
useCustomWorkingDirectory = "NO"
6060
ignoresPersistentStateOnLaunch = "NO"
6161
debugDocumentVersioning = "YES"
62+
stopOnEveryMainThreadCheckerIssue = "YES"
6263
debugServiceExtension = "internal"
6364
allowLocationSimulation = "YES">
6465
<BuildableProductRunnable

BuildTimeAnalyzer/LogProcessor.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension LogProcessorProtocol {
3737
let text = text as NSString
3838
let characterSet = CharacterSet(charactersIn:"\r\"")
3939
var remainingRange = NSMakeRange(0, text.length)
40-
40+
4141
rawMeasures.removeAll()
4242

4343
processingDidStart()
@@ -49,22 +49,19 @@ extension LogProcessorProtocol {
4949
let beginIdx = remainingRange.lowerBound
5050
let endIdx = nextRange.upperBound
5151
let textCount = endIdx - beginIdx
52-
let text = text.substring(with: NSMakeRange(beginIdx, textCount))
5352

5453
defer {
5554
remainingRange = NSMakeRange(endIdx, remainingRange.upperBound - endIdx)
5655
}
5756

58-
let range = NSMakeRange(0, textCount)
59-
guard let match = regex.firstMatch(in: text, options: [], range: range) else { continue }
60-
61-
let timeString = text[..<text.index(text.startIndex, offsetBy: match.range.length - 4)]
57+
let range = NSMakeRange(beginIdx, textCount)
58+
guard let match = regex.firstMatch(in: text as String, options: [], range: range) else { continue }
59+
let timeString = text.substring(with: NSMakeRange(beginIdx, match.range.length - 4))
6260
if let time = Double(timeString) {
63-
let value = String(text[text.index(text.startIndex, offsetBy: match.range.length - 1)...])
64-
if var rawMeasure = rawMeasures[value] {
61+
let value = text.substring(with: NSMakeRange(match.range.upperBound - 1, endIdx - match.range.upperBound - 1)) as String
62+
if let rawMeasure = rawMeasures[value] {
6563
rawMeasure.time += time
6664
rawMeasure.references += 1
67-
rawMeasures[value] = rawMeasure
6865
} else {
6966
rawMeasures[value] = RawMeasure(time: time, text: value)
7067
}
@@ -75,16 +72,23 @@ extension LogProcessorProtocol {
7572
}
7673

7774
fileprivate func updateResults(didComplete completed: Bool, didCancel: Bool) {
78-
var filteredResults = rawMeasures.values.filter{ $0.time > 10 }
79-
if filteredResults.count < 20 {
80-
filteredResults = rawMeasures.values.filter{ $0.time > 0.1 }
81-
}
82-
83-
let sortedResults = filteredResults.sorted(by: { $0.time > $1.time })
84-
updateHandler?(processResult(sortedResults), completed, didCancel)
85-
86-
if completed {
87-
rawMeasures.removeAll()
75+
DispatchQueue.global(qos: .userInteractive).async {
76+
let measures = self.rawMeasures.values
77+
var filteredResults = measures.filter{ $0.time > 10 }
78+
if filteredResults.count < 20 {
79+
filteredResults = measures.filter{ $0.time > 0.1 }
80+
}
81+
82+
let sortedResults = filteredResults.sorted(by: { $0.time > $1.time })
83+
let result = self.processResult(sortedResults)
84+
85+
if completed {
86+
self.rawMeasures.removeAll()
87+
}
88+
89+
DispatchQueue.main.async {
90+
self.updateHandler?(result, completed, didCancel)
91+
}
8892
}
8993
}
9094

BuildTimeAnalyzer/RawMeasure.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Foundation
77

8-
struct RawMeasure {
8+
class RawMeasure {
99
var time: Double
1010
var text: String
1111
var references: Int

0 commit comments

Comments
 (0)