spb/prisme Public MIT
Navigateur iOS intelligent — chaque page comprise localement avant d'être affichée. SwiftUI · WebKit · Foundation Models, 100% on-device.
Swift 96.7%
JavaScript 3.3%
1//2// InferenceQueue.swift3// Prisme4//5// Author: Simon-Pierre Boucher <contact@spboucher.ai>6//78import Foundation910/// Priority of an inference request. User gestures preempt page enrichment,11/// which preempts background work (CLAUDE.md §8).12enum InferencePriority: Int, Comparable, Sendable {13 case background = 014 case activePage = 115 case userGesture = 21617 static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue }18}1920/// Caps in-flight model requests and wakes waiters by priority. The Neural21/// Engine serializes inference anyway — an unbounded fan-out only hides the22/// real queue and wastes energy.23actor InferenceQueue {24 private let maxConcurrent: Int25 private var inFlight = 026 private var waiters: [(priority: InferencePriority, order: Int, continuation: CheckedContinuation<Void, Never>)] = []27 private var counter = 02829 init(maxConcurrent: Int) {30 self.maxConcurrent = maxConcurrent31 }3233 func run<T: Sendable>(34 _ priority: InferencePriority,35 operation: @Sendable () async throws -> T36 ) async rethrows -> T {37 await acquire(priority)38 defer { release() }39 return try await operation()40 }4142 private func acquire(_ priority: InferencePriority) async {43 if inFlight < maxConcurrent {44 inFlight += 145 return46 }47 counter += 148 let order = counter49 await withCheckedContinuation { continuation in50 waiters.append((priority, order, continuation))51 }52 }5354 private func release() {55 if let index = waiters.indices.max(by: { lhs, rhs in56 (waiters[lhs].priority, -waiters[lhs].order) < (waiters[rhs].priority, -waiters[rhs].order)57 }) {58 let next = waiters.remove(at: index)59 next.continuation.resume()60 } else {61 inFlight -= 162 }63 }64}65