This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BraveCore `Bookmarkv2.byFrequency` runs on main thread, and is not performant in general. Takes about 200ms per query. This commit moves this operation to a background cancellable thread.
- Loading branch information
Showing
8 changed files
with
76 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import Shared | ||
import Storage | ||
import Data | ||
|
||
class FrecencyQuery { | ||
private static let queue = DispatchQueue(label: "frecency-query-queue") | ||
private static var cancellable: DispatchWorkItem? | ||
|
||
public static func sitesByFrecency(containing query: String? = nil, | ||
completion: @escaping (Set<Site>) -> Void) { | ||
cancellable?.cancel() | ||
cancellable = nil | ||
|
||
// CoreData is fast, can be fetched on main thread. This also prevents threading problems. | ||
let historySites = History.byFrecency(query: query) | ||
.map { Site(url: $0.url ?? "", title: $0.title ?? "") } | ||
cancellable = DispatchWorkItem { | ||
// brave-core fetch can be slow over 200ms per call, | ||
// a cancellable serial queue is used for it. | ||
let bookmarkSites = Bookmarkv2.byFrequency(query: query) | ||
.map { Site(url: $0.url ?? "", title: $0.title ?? "", bookmarked: true) } | ||
|
||
let result = Set<Site>(historySites + bookmarkSites) | ||
|
||
DispatchQueue.main.async { | ||
completion(result) | ||
} | ||
} | ||
|
||
if let task = cancellable { | ||
queue.async(execute: task) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters