Skip to content

Commit

Permalink
Initial changes for compilation time tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
studiosutara committed Dec 3, 2024
1 parent 59f8fb2 commit 80ced96
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
self.identifier = identifier
}

internal init(compilationResult: (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel)) {
internal init(compilationResult: CompilationResult) {
let surrogateTDS = ContentBlockerRulesManager.extractSurrogates(from: compilationResult.model.tds)
let encodedData = try? JSONEncoder().encode(surrogateTDS)
let encodedTrackerData = String(data: encodedData!, encoding: .utf8)!
Expand Down Expand Up @@ -130,7 +130,6 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
public var sourceManagers = [String: ContentBlockerRulesSourceManager]()

private var currentTasks = [CompilationTask]()
private var compilationStartTime: TimeInterval?

private let workQueue = DispatchQueue(label: "ContentBlockerManagerQueue", qos: .userInitiated)

Expand Down Expand Up @@ -229,7 +228,6 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
}

state = .recompiling(currentTokens: [token])
compilationStartTime = compilationStartTime ?? CACurrentMediaTime()
lock.unlock()
return true
}
Expand Down Expand Up @@ -388,6 +386,15 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
unprotectedSitesHash: nil))
}

if let compilationTime = result.compilationTime {

Check failure on line 390 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
//todo: map broken sources to iteration count

Check failure on line 391 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)
let iteration = task.sourceManager.brokenSources

Check warning on line 392 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run unit tests (iOS)

initialization of immutable value 'iteration' was never used; consider replacing with assignment to '_' or removing it

Check failure on line 393 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
//todo: need to change this to the updated format with time range and iteration

Check failure on line 394 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockerRulesManager.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)
self.errorReporting?.fire(.contentBlockingCompilationTime, parameters: ["compilationTime": String(compilationTime)])
}

changes[task.rulesList.name] = diff
return rules
}
Expand All @@ -404,7 +411,6 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
_currentRules = rules

let completionTokens: [CompletionToken]
let compilationTime = compilationStartTime.map { start in CACurrentMediaTime() - start }
switch state {
case .recompilingAndScheduled(let currentTokens, let pendingTokens):
// New work has been scheduled - prepare for execution.
Expand All @@ -414,12 +420,10 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {

completionTokens = currentTokens
state = .recompiling(currentTokens: pendingTokens)
compilationStartTime = CACurrentMediaTime()

case .recompiling(let currentTokens):
completionTokens = currentTokens
state = .idle
compilationStartTime = nil

case .idle:
assertionFailure("Unexpected state")
Expand All @@ -432,10 +436,6 @@ public class ContentBlockerRulesManager: CompiledRuleListsSource {
updatesSubject.send(UpdateEvent(rules: rules, changes: changes, completionTokens: completionTokens))

DispatchQueue.main.async {
if let compilationTime = compilationTime {
self.errorReporting?.fire(.contentBlockingCompilationTime, parameters: ["compilationTime": String(compilationTime)])
}

self.cleanup(currentIdentifiers: currentIdentifiers)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ import TrackerRadarKit
import os.log

extension ContentBlockerRulesManager {
typealias CompilationResult = (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel, compilationTime: TimeInterval?)

/**
Encapsulates compilation steps for a single Task
*/
internal class CompilationTask {
typealias Completion = (_ task: CompilationTask, _ success: Bool) -> Void

Check failure on line 33 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
let workQueue: DispatchQueue
let rulesList: ContentBlockerRulesList
let sourceManager: ContentBlockerRulesSourceManager
var isCompleted: Bool { result != nil || compilationImpossible }
private(set) var compilationImpossible = false
private(set) var result: (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel)?
private(set) var result: CompilationResult?
private(set) var compilationStartTime: TimeInterval?

init(workQueue: DispatchQueue,
rulesList: ContentBlockerRulesList,
Expand All @@ -52,6 +55,8 @@ extension ContentBlockerRulesManager {
completionHandler(self, false)
return
}

Check failure on line 58 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
self.compilationStartTime = CACurrentMediaTime()

guard !ignoreCache else {
Logger.contentBlocking.log("❗️ ignoring cache")
Expand All @@ -65,6 +70,7 @@ extension ContentBlockerRulesManager {
DispatchQueue.main.async {
let identifier = model.rulesIdentifier.stringValue
Logger.contentBlocking.debug("Lookup CBR with \(identifier, privacy: .public)")
//Todo: how do we exclude this case from compilation time where the result is returned from cache

Check failure on line 73 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)
WKContentRuleListStore.default()?.lookUpContentRuleList(forIdentifier: identifier) { ruleList, _ in
if let ruleList = ruleList {
Logger.contentBlocking.log("🟢 CBR loaded from cache: \(self.rulesList.name, privacy: .public)")
Expand All @@ -83,7 +89,9 @@ extension ContentBlockerRulesManager {
model: ContentBlockerRulesSourceModel,
completionHandler: @escaping Completion) {
workQueue.async {
self.result = (compiledRulesList, model)
let compilationTime = self.compilationStartTime.map { start in CACurrentMediaTime() - start }

self.result = (compiledRulesList, model, compilationTime)
completionHandler(self, true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ extension ContentBlockerRulesManager {

final class LookupRulesTask {

typealias LookupResult = (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel)
//This type has been overloaded multiple times, I'm collapsing them all into CompilationResult type

Check failure on line 27 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesLookupTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)
//typealias LookupResult = (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel)

Check failure on line 28 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesLookupTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)

private let sourceManagers: [ContentBlockerRulesSourceManager]

public private(set) var result: [LookupResult]?
////todo: how to get around calling this compilation result when in lookuptask?

Check failure on line 32 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesLookupTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)
public private(set) var result: [CompilationResult]?

init(sourceManagers: [ContentBlockerRulesSourceManager]) {
self.sourceManagers = sourceManagers
}

func lookupCachedRulesLists() async throws {

var result = [LookupResult]()
var result = [CompilationResult]()
for sourceManager in sourceManagers {
guard let model = sourceManager.makeModel() else {
throw WKError(.contentRuleListStoreLookUpFailed)
Expand All @@ -49,7 +51,7 @@ extension ContentBlockerRulesManager {
throw WKError(.contentRuleListStoreLookUpFailed)
}

result.append((ruleList, model))
result.append((ruleList, model, nil))
}
self.result = result
}
Expand Down

0 comments on commit 80ced96

Please sign in to comment.