Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial changes for compilation time tracking. #1111

Merged
merged 15 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
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 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 @@
}

state = .recompiling(currentTokens: [token])
compilationStartTime = compilationStartTime ?? CACurrentMediaTime()
lock.unlock()
return true
}
Expand Down Expand Up @@ -388,6 +386,15 @@
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
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
studiosutara marked this conversation as resolved.
Show resolved Hide resolved

// todo: need to change this to the updated format with time range and iteration
self.errorReporting?.fire(.contentBlockingCompilationTime, parameters: ["compilationTime": String(compilationTime)])
}

changes[task.rulesList.name] = diff
return rules
}
Expand All @@ -404,7 +411,6 @@
_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 @@

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 @@
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

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 @@ -53,6 +56,8 @@ extension ContentBlockerRulesManager {
return
}

self.compilationStartTime = CACurrentMediaTime()

guard !ignoreCache else {
Logger.contentBlocking.log("❗️ ignoring cache")
self.workQueue.async {
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
studiosutara marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since we have resultType, this comment is addressed?

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)
bwaresiak marked this conversation as resolved.
Show resolved Hide resolved
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
// typealias LookupResult = (compiledRulesList: WKContentRuleList, model: ContentBlockerRulesSourceModel)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed


private let sourceManagers: [ContentBlockerRulesSourceManager]

public private(set) var result: [LookupResult]?
// todo: how to get around naming this compilation result when in lookuptask?
public private(set) var result: [CompilationResult]?
studiosutara marked this conversation as resolved.
Show resolved Hide resolved

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
Loading