Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

perf: speed up dictation handoff

altic-devb51678ae876b
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swift
index 67dc8f09..7b1f671f 100644
--- a/Sources/Fluid/ContentView.swift
+++ b/Sources/Fluid/ContentView.swift
@@ -2019,7 +2019,9 @@ struct ContentView: View {
self.asr.finalText = finalText
DebugLogger.shared.info("Transcription finalized (chars: \(finalText.count))", source: "ContentView")
+ let finalTextReadyAt = ProcessInfo.processInfo.systemUptime
self.appBench("transcription_finalized chars=\(finalText.count)")
+ self.appBench("text_ready chars=\(finalText.count)")
AnalyticsService.shared.capture(
.transcriptionCompleted,
@@ -2105,9 +2107,13 @@ struct ContentView: View {
if typingTarget.shouldRestoreOriginalFocus {
await self.restoreFocusToRecordingTarget()
}
+ self.appBench(
+ "text_ready_to_type_request elapsedMs=\(Int(((ProcessInfo.processInfo.systemUptime - finalTextReadyAt) * 1000).rounded()))"
+ )
self.asr.typeTextToActiveField(
finalText,
- preferredTargetPID: typingTarget.pid
+ preferredTargetPID: typingTarget.pid,
+ textReadyAt: finalTextReadyAt
)
didTypeExternally = true
}
@@ -2577,6 +2583,7 @@ struct ContentView: View {
// Ensure normal dictation mode is set (command/rewrite modes set their own)
if !self.isRecordingForCommand, !self.isRecordingForRewrite {
self.menuBarManager.setOverlayMode(.dictation)
+ self.menuBarManager.showRecordingOverlayImmediately()
}
if !self.isRecordingForCommand, !self.isRecordingForRewrite {
@@ -2621,11 +2628,20 @@ struct ContentView: View {
}
/// Best-effort: re-activate the app that was focused when recording started.
- /// Adds a short delay after activation so macOS can deliver focus before typing begins.
+ /// Skips the AX restore work when the captured text element is already focused.
private func restoreFocusToRecordingTarget() async {
guard let pid = NotchContentState.shared.recordingTargetPID else { return }
let startedAt = ProcessInfo.processInfo.systemUptime
self.appBench("focus_restore_start targetPID=\(pid)")
+ if TypingService.isCapturedFocusStillActive(for: pid) {
+ self.appBench("focus_restore_result activated=false element=true elapsedMs=0 reason=already_focused")
+ DebugLogger.shared.debug(
+ "Restore focus skipped; captured element still focused, targetPID: \(pid)",
+ source: "ContentView"
+ )
+ self.appBench("focus_restore_settle_done delayMs=0")
+ return
+ }
let activated = TypingService.activateApp(pid: pid)
let focusedElementRestored = TypingService.restoreCapturedFocus(in: pid)
self.appBench(
@@ -2635,12 +2651,7 @@ struct ContentView: View {
"Restore focus -> appActivated: \(activated), elementFocusRestored: \(focusedElementRestored), targetPID: \(pid)",
source: "ContentView"
)
- if activated {
- // Small delay to allow focus to settle before typing events fire.
- let settleNanos: UInt64 = 10_000_000
- try? await Task.sleep(nanoseconds: settleNanos)
- self.appBench("focus_restore_settle_done delayMs=10")
- }
+ self.appBench("focus_restore_settle_done delayMs=0")
}
// MARK: - ASR Model Management
@@ -3143,6 +3154,7 @@ extension ContentView {
self.rewriteModeService.clearState()
self.appBench("overlay_mode_request mode=Dictation")
self.menuBarManager.setOverlayMode(.dictation)
+ self.menuBarManager.showRecordingOverlayImmediately()
self.appBench("overlay_mode_requested mode=Dictation")
self.prewarmPrivateAIDictationIfNeeded(for: slot)
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index 9bd68c48..a39ed84d 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -2772,21 +2772,22 @@ final class ASRService: ObservableObject {
private let typingService = TypingService() // Reuse instance to avoid conflicts
func typeTextToActiveField(_ text: String) {
- DebugLogger.shared.benchmark("TYPING_BENCH", message: "asr_type_request chars=\(text.count) preferredPID=nil", source: "TypingBenchmark")
- self.typingService.typeTextInstantly(text)
- DebugLogger.shared.benchmark("TYPING_BENCH", message: "asr_type_dispatched chars=\(text.count) preferredPID=nil", source: "TypingBenchmark")
+ self.typeTextToActiveField(text, preferredTargetPID: nil, textReadyAt: nil)
}
- func typeTextToActiveField(_ text: String, preferredTargetPID: pid_t?) {
+ func typeTextToActiveField(_ text: String, preferredTargetPID: pid_t?, textReadyAt: TimeInterval? = nil) {
+ let requestedAt = ProcessInfo.processInfo.systemUptime
+ let textReadyAge = textReadyAt.map { Int(((requestedAt - $0) * 1000).rounded()) }
DebugLogger.shared.benchmark(
"TYPING_BENCH",
- message: "asr_type_request chars=\(text.count) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil")",
+ message: "asr_type_request chars=\(text.count) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil") textReadyAgeMs=\(textReadyAge.map { String($0) } ?? "nil")",
source: "TypingBenchmark"
)
- self.typingService.typeTextInstantly(text, preferredTargetPID: preferredTargetPID)
+ self.typingService.typeTextInstantly(text, preferredTargetPID: preferredTargetPID, textReadyAt: textReadyAt)
+ let dispatchedAt = ProcessInfo.processInfo.systemUptime
DebugLogger.shared.benchmark(
"TYPING_BENCH",
- message: "asr_type_dispatched chars=\(text.count) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil")",
+ message: "asr_type_dispatched chars=\(text.count) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil") textReadyToDispatchMs=\(textReadyAt.map { String(Int(((dispatchedAt - $0) * 1000).rounded())) } ?? "nil")",
source: "TypingBenchmark"
)
}
diff --git a/Sources/Fluid/Services/MenuBarManager.swift b/Sources/Fluid/Services/MenuBarManager.swift
index 881cdb1a..37972076 100644
--- a/Sources/Fluid/Services/MenuBarManager.swift
+++ b/Sources/Fluid/Services/MenuBarManager.swift
@@ -45,7 +45,8 @@ final class MenuBarManager: NSObject, ObservableObject, NSMenuDelegate {
private var pendingShowOperation: DispatchWorkItem?
private var pendingHideOperation: DispatchWorkItem?
private var pendingProcessingShowOperation: DispatchWorkItem?
- private let processingVisualDelay: DispatchTimeInterval = .milliseconds(100)
+ private let processingVisualDelay: DispatchTimeInterval = .milliseconds(0)
+ private let processingHideDelay: DispatchTimeInterval = .milliseconds(0)
// Subscription for forwarding audio levels to expanded command notch
private var expandedModeAudioSubscription: AnyCancellable?
@@ -209,6 +210,46 @@ final class MenuBarManager: NSObject, ObservableObject, NSMenuDelegate {
}
}
+ func showRecordingOverlayImmediately() {
+ guard let asrService else {
+ self.overlayBench("instant_show_return reason=no_asr_service")
+ return
+ }
+
+ self.pendingHideOperation?.cancel()
+ self.pendingHideOperation = nil
+ self.pendingShowOperation?.cancel()
+ self.pendingShowOperation = nil
+
+ guard !self.overlayVisible else {
+ self.overlayBench("instant_show_return reason=already_visible")
+ return
+ }
+
+ self.overlayVisible = true
+ self.overlayBench("instant_show_request mode=\(self.currentOverlayMode.rawValue)")
+
+ if NotchOverlayManager.shared.isCommandOutputExpanded {
+ if self.currentOverlayMode == .command, NotchOverlayManager.shared.supportsCommandNotchUI {
+ NotchContentState.shared.setRecordingInExpandedMode(true)
+ self.expandedModeAudioSubscription = asrService.audioLevelPublisher
+ .receive(on: DispatchQueue.main)
+ .sink { level in
+ NotchContentState.shared.updateExpandedModeAudioLevel(level)
+ }
+ return
+ }
+ NotchOverlayManager.shared.hideExpandedCommandOutput()
+ }
+
+ self.overlayBench("show_workitem_execute mode=\(self.currentOverlayMode.rawValue)")
+ NotchOverlayManager.shared.show(
+ audioLevelPublisher: asrService.audioLevelPublisher,
+ mode: self.currentOverlayMode
+ )
+ self.overlayBench("show_workitem_return mode=\(self.currentOverlayMode.rawValue)")
+ }
+
// MARK: - Public API for overlay management
func updateOverlayTranscription(_ text: String) {
@@ -236,7 +277,7 @@ final class MenuBarManager: NSObject, ObservableObject, NSMenuDelegate {
let showItem = DispatchWorkItem { [weak self] in
guard let self = self, self.isProcessingActive else { return }
- self.overlayBench("processing_show_workitem_execute delayMs=100")
+ self.overlayBench("processing_show_workitem_execute delayMs=0")
NotchOverlayManager.shared.setProcessing(true)
self.overlayBench("processing_show_workitem_return")
self.pendingProcessingShowOperation = nil
@@ -266,15 +307,15 @@ final class MenuBarManager: NSObject, ObservableObject, NSMenuDelegate {
return
}
- self.overlayBench("processing_hide_workitem_execute delayMs=100")
+ self.overlayBench("processing_hide_workitem_execute delayMs=0")
NotchOverlayManager.shared.hide()
self.overlayBench("processing_hide_workitem_return")
self.pendingHideOperation = nil
}
self.pendingHideOperation = hideItem
- DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100), execute: hideItem)
+ DispatchQueue.main.asyncAfter(deadline: .now() + self.processingHideDelay, execute: hideItem)
NotchOverlayManager.shared.setProcessing(false)
- self.overlayBench("processing_forwarded processing=false hideDelayMs=100")
+ self.overlayBench("processing_forwarded processing=false hideDelayMs=0")
return
}
}
diff --git a/Sources/Fluid/Services/TypingService.swift b/Sources/Fluid/Services/TypingService.swift
index 03460d51..26e5b9ee 100644
--- a/Sources/Fluid/Services/TypingService.swift
+++ b/Sources/Fluid/Services/TypingService.swift
@@ -205,6 +205,18 @@ final class TypingService {
return isFocused
}
+ static func isCapturedFocusStillActive(for pid: pid_t) -> Bool {
+ guard AXIsProcessTrusted(),
+ let snapshot = loadFocusSnapshot(),
+ snapshot.pid == pid,
+ let element = snapshot.element
+ else {
+ return false
+ }
+
+ return Self.isCurrentlyFocusedElement(element, expectedPID: pid)
+ }
+
/// Best-effort: activates the app with the given PID, unless it's Fluid itself.
@discardableResult
static func activateApp(pid: pid_t) -> Bool {
@@ -225,16 +237,30 @@ final class TypingService {
// MARK: - Public API
func typeTextInstantly(_ text: String) {
- self.typeTextInstantly(text, preferredTargetPID: nil)
+ self.typeTextInstantly(text, preferredTargetPID: nil, textReadyAt: nil)
}
/// Types/inserts text, optionally preferring a specific target PID for CGEvent posting.
/// This helps when our overlay temporarily has focus; we can still target the original app.
func typeTextInstantly(_ text: String, preferredTargetPID: pid_t?) {
+ self.typeTextInstantly(text, preferredTargetPID: preferredTargetPID, textReadyAt: nil)
+ }
+
+ /// Types/inserts text, optionally preferring a specific target PID for CGEvent posting.
+ /// This helps when our overlay temporarily has focus; we can still target the original app.
+ func typeTextInstantly(_ text: String, preferredTargetPID: pid_t?, textReadyAt: TimeInterval?) {
let requestedAt = ProcessInfo.processInfo.systemUptime
let mode = self.textInsertionMode
- let settleDelayMs = mode == .reliablePaste ? 80 : 200
- self.bench("request chars=\(text.count) mode=\(mode.rawValue) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil")")
+ let settleDelayMs: Int = {
+ if mode == .reliablePaste {
+ return preferredTargetPID == nil ? 80 : 0
+ }
+ return preferredTargetPID == nil ? 200 : 0
+ }()
+ let textReadyAge = textReadyAt.map { Self.elapsedMs(from: $0, to: requestedAt) }
+ self.bench(
+ "request chars=\(text.count) mode=\(mode.rawValue) preferredPID=\(preferredTargetPID.map { String($0) } ?? "nil") textReadyAgeMs=\(textReadyAge.map { String($0) } ?? "nil")"
+ )
self.log("[TypingService] ENTRY: typeTextInstantly called with text length: \(text.count)")
self.log("[TypingService] Text preview: \"\(String(text.prefix(100)))\"")
@@ -267,18 +293,17 @@ final class TypingService {
self.bench("worker_start queueDelayMs=\(Self.elapsedMs(from: requestedAt, to: workerStartedAt))")
defer {
+ let completedAt = ProcessInfo.processInfo.systemUptime
self.isCurrentlyTyping = false
- self.bench("complete totalMs=\(Self.elapsedMs(since: requestedAt))")
+ self.bench(
+ "complete totalMs=\(Self.elapsedMs(from: requestedAt, to: completedAt)) textReadyToCompleteMs=\(textReadyAt.map { String(Self.elapsedMs(from: $0, to: completedAt)) } ?? "nil")"
+ )
self.log("[TypingService] Typing operation completed, isCurrentlyTyping set to false")
}
self.log("[TypingService] Starting async text insertion process")
- if mode == .reliablePaste {
- // Reliable Paste still needs a short settle window after focus restoration.
- usleep(80_000)
- } else {
- // Direct typing paths are more timing-sensitive after app activation.
- usleep(200_000)
+ if settleDelayMs > 0 {
+ usleep(useconds_t(settleDelayMs * 1000))
}
self.bench("settle_delay_done delayMs=\(settleDelayMs) elapsedMs=\(Self.elapsedMs(since: requestedAt))")
self.log("[TypingService] Delay completed, calling insertTextInstantly")
@@ -325,24 +350,6 @@ final class TypingService {
self.log("[TypingService] Preferred PID CGEvent insertion failed, continuing fallback pipeline")
}
- if text.utf16.count > Self.cgEventUnicodeLimit {
- self.log("[TypingService] Text exceeds CGEvent limit (\(text.utf16.count) UTF-16 units), using clipboard insertion")
- if self.insertTextViaClipboard(text) {
- self.log("[TypingService] SUCCESS: Clipboard insertion completed (long text path)")
- return
- }
- self.log("[TypingService] Clipboard failed for long text, trying character-by-character")
- for (index, char) in text.enumerated() {
- if index % 10 == 0 {
- self.log("[TypingService] Typing character \(index + 1)/\(text.count)")
- }
- self.typeCharacter(char)
- usleep(1000)
- }
- self.log("[TypingService] Character-by-character typing completed")
- return
- }
-
// Get frontmost app info
if let frontApp = NSWorkspace.shared.frontmostApplication {
self.log("[TypingService] Target app: \(frontApp.localizedName ?? "Unknown") (\(frontApp.bundleIdentifier ?? "Unknown"))")
@@ -435,7 +442,7 @@ final class TypingService {
return false
}
- private static let cgEventUnicodeLimit = 200
+ private static let cgEventUnicodeChunkSize = 200
private static func storeFocusSnapshot(_ snapshot: FocusSnapshot?) {
self.focusSnapshotQueue.sync {
@@ -636,7 +643,7 @@ final class TypingService {
}
private func insertTextBulkInstant(_ text: String, targetPID: pid_t) -> Bool {
- self.log("[TypingService] Starting INSTANT bulk CGEvent insertion (NO CLIPBOARD) to PID \(targetPID)")
+ self.log("[TypingService] Starting chunked bulk CGEvent insertion (NO CLIPBOARD) to PID \(targetPID)")
guard targetPID > 0 else {
self.log("[TypingService] ERROR: Invalid target PID \(targetPID)")
@@ -646,57 +653,83 @@ final class TypingService {
let utf16Array = Array(text.utf16)
self.log("[TypingService] Converting \(text.count) characters to CGEvents (UTF16 count \(utf16Array.count))")
- guard utf16Array.count <= Self.cgEventUnicodeLimit else {
- self.log("[TypingService] Text too long for single CGEvent (\(utf16Array.count) UTF-16 units > \(Self.cgEventUnicodeLimit)), falling back")
- return false
+ return self.postUnicodeChunks(utf16Array, destinationDescription: "PID \(targetPID)") { event in
+ event.postToPid(targetPID)
}
-
- guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: true),
- let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: false)
- else {
- self.log("[TypingService] ERROR: Failed to create bulk CGEvents")
- return false
- }
-
- keyDown.keyboardSetUnicodeString(stringLength: utf16Array.count, unicodeString: utf16Array)
- keyUp.keyboardSetUnicodeString(stringLength: utf16Array.count, unicodeString: utf16Array)
-
- keyDown.postToPid(targetPID)
- usleep(2000)
- keyUp.postToPid(targetPID)
-
- self.log("[TypingService] Posted bulk CGEvents to PID \(targetPID)")
- return true
}
private func insertTextBulkHIDInstant(_ text: String) -> Bool {
- self.log("[TypingService] Starting INSTANT bulk CGEvent insertion via HID (NO PID)")
+ self.log("[TypingService] Starting chunked bulk CGEvent insertion via HID (NO PID)")
let utf16Array = Array(text.utf16)
- guard utf16Array.count <= Self.cgEventUnicodeLimit else {
- self.log("[TypingService] Text too long for single HID CGEvent (\(utf16Array.count) > \(Self.cgEventUnicodeLimit)), falling back")
- return false
+ return self.postUnicodeChunks(utf16Array, destinationDescription: "HID tap") { event in
+ event.post(tap: .cghidEventTap)
}
+ }
- guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: true),
- let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: false)
- else {
- self.log("[TypingService] ERROR: Failed to create HID bulk CGEvents")
- return false
- }
+ private func postUnicodeChunks(
+ _ utf16Array: [UInt16],
+ destinationDescription: String,
+ post: (CGEvent) -> Void
+ ) -> Bool {
+ guard utf16Array.isEmpty == false else { return true }
+
+ let chunkCount: Int = utf16Array.withUnsafeBufferPointer { buffer in
+ guard let baseAddress = buffer.baseAddress else { return 0 }
+
+ var chunkStart = 0
+ var chunkCount = 0
+ while chunkStart < buffer.count {
+ let chunkEnd = Self.unicodeChunkEnd(in: utf16Array, start: chunkStart)
+ let chunkLength = chunkEnd - chunkStart
+
+ guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: true),
+ let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: 0, keyDown: false)
+ else {
+ self.log("[TypingService] ERROR: Failed to create unicode chunk CGEvents")
+ return -1
+ }
- keyDown.keyboardSetUnicodeString(stringLength: utf16Array.count, unicodeString: utf16Array)
- keyUp.keyboardSetUnicodeString(stringLength: utf16Array.count, unicodeString: utf16Array)
+ let chunkPointer = baseAddress.advanced(by: chunkStart)
+ keyDown.keyboardSetUnicodeString(stringLength: chunkLength, unicodeString: chunkPointer)
+ keyUp.keyboardSetUnicodeString(stringLength: chunkLength, unicodeString: chunkPointer)
- keyDown.post(tap: .cghidEventTap)
- usleep(2000)
- keyUp.post(tap: .cghidEventTap)
+ post(keyDown)
+ post(keyUp)
- self.log("[TypingService] Posted bulk CGEvents via HID tap")
+ chunkStart = chunkEnd
+ chunkCount += 1
+ }
+ return chunkCount
+ }
+
+ guard chunkCount >= 0 else { return false }
+
+ self.log("[TypingService] Posted \(chunkCount) unicode CGEvent chunk(s) to \(destinationDescription) with chunkSize=\(Self.cgEventUnicodeChunkSize) interChunkDelayMs=0")
return true
}
+ private static func unicodeChunkEnd(in utf16Array: [UInt16], start: Int) -> Int {
+ var end = min(start + Self.cgEventUnicodeChunkSize, utf16Array.count)
+ if end < utf16Array.count,
+ end > start,
+ Self.isHighSurrogate(utf16Array[end - 1]),
+ Self.isLowSurrogate(utf16Array[end])
+ {
+ end -= 1
+ }
+ return max(end, start + 1)
+ }
+
+ private static func isHighSurrogate(_ value: UInt16) -> Bool {
+ (0xd800...0xdbff).contains(value)
+ }
+
+ private static func isLowSurrogate(_ value: UInt16) -> Bool {
+ (0xdc00...0xdfff).contains(value)
+ }
+
/// Clipboard-based text insertion as fallback
/// More reliable but slightly slower - copies text to clipboard then pastes
private func insertTextViaClipboard(_ text: String) -> Bool {
diff --git a/Sources/Fluid/Views/BottomOverlayView.swift b/Sources/Fluid/Views/BottomOverlayView.swift
index 6d699c5c..cbf29d55 100644
--- a/Sources/Fluid/Views/BottomOverlayView.swift
+++ b/Sources/Fluid/Views/BottomOverlayView.swift
@@ -91,21 +91,11 @@ final class BottomOverlayWindowController {
self.targetScreen = OverlayScreenResolver.screenForCurrentPointer()
self.positionWindow()
- // Show with animation
- self.window?.alphaValue = 0
+ // Show immediately; ASR startup can delay AppKit animation completions.
+ self.window?.alphaValue = 1
self.window?.orderFrontRegardless()
Self.overlayBench("bottom_order_front elapsedMs=\(Self.elapsedMs(since: startedAt))")
-
- NSAnimationContext.runAnimationGroup { context in
- context.duration = 0.25
- context.timingFunction = CAMediaTimingFunction(name: .easeOut)
- self.window?.animator().alphaValue = 1
- } completionHandler: {
- let elapsedMs = Int(((ProcessInfo.processInfo.systemUptime - startedAt) * 1000).rounded())
- Task { @MainActor in
- Self.overlayBench("bottom_fade_complete elapsedMs=\(elapsedMs)")
- }
- }
+ Self.overlayBench("bottom_fade_complete elapsedMs=\(Self.elapsedMs(since: startedAt))")
}
func hide() {
@@ -140,21 +130,11 @@ final class BottomOverlayWindowController {
NotchContentState.shared.setBottomOverlayDismissOffsetY(28)
NotchContentState.shared.setBottomOverlayDismissing(true)
- NSAnimationContext.runAnimationGroup { context in
- context.duration = 0.2
- context.timingFunction = CAMediaTimingFunction(name: .easeIn)
- window.animator().alphaValue = 0
- } completionHandler: {
- window.orderOut(nil)
- let elapsedMs = Int(((ProcessInfo.processInfo.systemUptime - startedAt) * 1000).rounded())
- Task { @MainActor in
- Self.overlayBench("bottom_hide_complete elapsedMs=\(elapsedMs)")
- }
- Task { @MainActor in
- self.endReleaseTransition(flushDeferredUpdate: false)
- NotchContentState.shared.setBottomOverlayDismissing(false)
- }
- }
+ window.alphaValue = 0
+ window.orderOut(nil)
+ self.endReleaseTransition(flushDeferredUpdate: false)
+ NotchContentState.shared.setBottomOverlayDismissing(false)
+ Self.overlayBench("bottom_hide_complete elapsedMs=\(Self.elapsedMs(since: startedAt))")
}
func setProcessing(_ processing: Bool) {