mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
* perf(mobile): answer a page's ready that arrived before its bridge host A page's ready can reach the shell before the screen has built its bridge host (the host waits on the client and the host profile read). It used to be dropped, so init waited for the page's next retry, up to 2 s later. Keep the latest ready per session and answer it once the host exists. * perf(mobile): keep one prewarmed page document while Home is shown While native Home is on screen, boot one page document for the most recently connected host with a cached build, parked invisibly in the window. Opening a page from that build adopts it instead of building a WebView, which removes the WebView start and script parse from the tap. The spare is dropped when Home is left without a claim (2 s grace), on background, memory pressure, its own failure and before the cache deletes its files. It is never kept beside a shown page, and only phones with more than 3 GiB of RAM (4 GB phones) get one. A parked page is told to stop asking for init; the adopting view answers the ready it sent. Builds on Jinwoo's spare pool and replay work in #32203 and #32204. Co-authored-by: Jinwoo-H <73622457+Jinwoo-H@users.noreply.github.com> * fix(mobile): unregister the shell's memory callbacks from the context that registered them --------- Co-authored-by: Jinwoo-H <73622457+Jinwoo-H@users.noreply.github.com>
151 lines
5.3 KiB
Swift
151 lines
5.3 KiB
Swift
import UIKit
|
||
import WebKit
|
||
|
||
/// More than 3 GiB can only come from a phone with 4 GB or more; those report about 3.5 to 3.9 GiB.
|
||
private let minimumPhysicalMemory: UInt64 = 3 << 30
|
||
|
||
/// A claim is adopted on the claiming screen's first commit; this only bounds a screen that never mounts.
|
||
private let claimExpirySeconds: TimeInterval = 10
|
||
|
||
/// A booted document waiting for a screen. It keeps what the page reported, and the page's ready, for
|
||
/// the view that adopts it: a parked page stops asking for init, so its ready is answered from here.
|
||
final class MobileWebShellSpare: MobileWebShellDocumentSink {
|
||
let document = MobileWebShellDocument()
|
||
let directory: String
|
||
let sessionId: String
|
||
private var loadStates: [[String: Any]] = []
|
||
private var ready: String?
|
||
|
||
init(directory: String, sessionId: String) {
|
||
self.directory = directory
|
||
self.sessionId = sessionId
|
||
document.sink = self
|
||
}
|
||
|
||
func documentLoadState(_ payload: [String: Any]) {
|
||
loadStates.append(payload)
|
||
if payload["state"] as? String == "failed" {
|
||
MobileWebShellSparePool.shared.discard(self)
|
||
}
|
||
}
|
||
|
||
/// Before init a page only sends ready, from its first document or one it reloaded.
|
||
func documentBridgeMessage(_ json: String) {
|
||
ready = json
|
||
document.postHostFrame(MobileWebShellBridge.parkedFrame)
|
||
}
|
||
|
||
/// A parked page has no screen to have been tapped on.
|
||
func documentExternalNavigation(_ url: String) {}
|
||
|
||
/// The adopting view sees what a fresh document would have reported, ready last.
|
||
func handOver(to sink: MobileWebShellDocumentSink) {
|
||
document.sink = sink
|
||
loadStates.forEach(sink.documentLoadState)
|
||
if let ready {
|
||
sink.documentBridgeMessage(ready)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// At most one document booted ahead of its screen, only while no page document is shown. Main thread only.
|
||
/// Parked in the key window at alpha 0: WebKit suspends an off-window view (0.5–1.3 s to paint after 30 s, vs 0.1 s).
|
||
final class MobileWebShellSparePool {
|
||
static let shared = MobileWebShellSparePool()
|
||
|
||
private var parked: MobileWebShellSpare?
|
||
private var claimed: MobileWebShellSpare?
|
||
private let shown = NSHashTable<MobileWebShellDocument>.weakObjects()
|
||
|
||
/// Whether a spare for `directory` is parked after the call; never on a small phone, inactive app or shown page.
|
||
func prepare(directory: String, sessionId: String) -> Bool {
|
||
guard
|
||
ProcessInfo.processInfo.physicalMemory > minimumPhysicalMemory,
|
||
UIApplication.shared.applicationState == .active,
|
||
shown.allObjects.isEmpty,
|
||
let window = keyWindow()
|
||
else { return false }
|
||
if let parked, parked.directory == directory, parked.document.isUsable {
|
||
return true
|
||
}
|
||
// A claim no view adopted was abandoned; it must not outlive the spare that replaces it.
|
||
discardAll()
|
||
let spare = MobileWebShellSpare(directory: directory, sessionId: sessionId)
|
||
let webView = spare.document.webView
|
||
webView.alpha = 0
|
||
webView.isUserInteractionEnabled = false
|
||
webView.accessibilityElementsHidden = true
|
||
webView.frame = window.bounds
|
||
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||
window.insertSubview(webView, at: 0)
|
||
parked = spare
|
||
spare.document.apply(generationDirectory: directory, sessionId: sessionId, bridgeEnabled: true)
|
||
// A load that failed at once has already discarded it.
|
||
return parked === spare
|
||
}
|
||
|
||
/// The parked spare's session id for a screen opening `directory`, handed out once.
|
||
func claim(directory: String) -> String? {
|
||
guard let spare = parked, spare.directory == directory, spare.document.isUsable else {
|
||
return nil
|
||
}
|
||
parked = nil
|
||
claimed = spare
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + claimExpirySeconds) { [weak self, weak spare] in
|
||
if let self, let spare, self.claimed === spare {
|
||
self.discard(spare)
|
||
}
|
||
}
|
||
return spare.sessionId
|
||
}
|
||
|
||
/// The claimed spare when `sessionId` names it, else a fresh document, which leaves no room for a spare.
|
||
func document(for sessionId: String, sink: MobileWebShellDocumentSink) -> MobileWebShellDocument {
|
||
let document: MobileWebShellDocument
|
||
if let spare = claimed, spare.sessionId == sessionId, spare.document.isUsable {
|
||
claimed = nil
|
||
document = spare.document
|
||
let webView = document.webView
|
||
webView.removeFromSuperview()
|
||
webView.alpha = 1
|
||
webView.isUserInteractionEnabled = true
|
||
webView.accessibilityElementsHidden = false
|
||
webView.autoresizingMask = []
|
||
spare.handOver(to: sink)
|
||
} else {
|
||
discardAll()
|
||
document = MobileWebShellDocument()
|
||
document.sink = sink
|
||
}
|
||
shown.add(document)
|
||
return document
|
||
}
|
||
|
||
/// Memory pressure, the app leaving the foreground, files going away, or Home without a claim.
|
||
func discardAll() {
|
||
for spare in [parked, claimed].compactMap({ $0 }) {
|
||
spare.document.destroy()
|
||
}
|
||
parked = nil
|
||
claimed = nil
|
||
}
|
||
|
||
fileprivate func discard(_ spare: MobileWebShellSpare) {
|
||
if parked === spare {
|
||
parked = nil
|
||
} else if claimed === spare {
|
||
claimed = nil
|
||
} else {
|
||
return
|
||
}
|
||
spare.document.destroy()
|
||
}
|
||
|
||
private func keyWindow() -> UIWindow? {
|
||
UIApplication.shared.connectedScenes
|
||
.compactMap { $0 as? UIWindowScene }
|
||
.flatMap(\.windows)
|
||
.first(where: \.isKeyWindow)
|
||
}
|
||
}
|