fix(speech): keep the engine's file reads off the main thread's hot paths

The engine lookup and the GPU failure marker are read once per process
instead of on every dictation start and mobile catalog request, the marker
is written asynchronously, and the child reads the hotwords file with
fs/promises. The baseline moves the existing engine presence checks from
stt-worker-paths to the shared package table and adds the once-per-process
marker reads.
This commit is contained in:
Kelvin Amoaba
2026-11-09 05:21:45 +00:00
parent 652e13c18e
commit 29cc22dc0a
4 changed files with 30 additions and 16 deletions
+2 -1
View File
@@ -497,8 +497,8 @@
2 src/main/speech/speech-model-download-cleanup.ts
1 src/main/speech/speech-model-download-transport.ts
2 src/main/speech/speech-model-http-download.ts
2 src/main/speech/stt-gpu-failure-marker.ts
1 src/main/speech/stt-worker-model-config.ts
2 src/main/speech/stt-worker-paths.ts
2 src/main/sqlite/harden-database-files.ts
1 src/main/sqlite/row-cache/keyed-json-row-cache.ts
2 src/main/sqlite/row-cache/legacy-json-source.ts
@@ -609,6 +609,7 @@
7 src/shared/serve-bundle-park.ts
1 src/shared/serve-user-data-path.ts
3 src/shared/source-scan/source-tree-scan.ts
3 src/shared/speech-recognizer-package.ts
5 src/shared/startup-command-staging.ts
1 src/shared/terminal/pty/native-process/pty-process-ceiling-state.ts
8 src/shared/windows-breakaway-launcher.ts
+11 -7
View File
@@ -1,4 +1,5 @@
import { readFileSync, writeFileSync } from 'node:fs'
import { readFileSync } from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { release } from 'node:os'
import { join } from 'node:path'
import type { SttGpuPolicy } from './stt-worker-cpu-fallback'
@@ -39,20 +40,23 @@ function readMarkerKey(file: string): string | null {
/** `gpuEnabled` is the user's Voice setting; the marker records a GPU that failed on this machine. */
export function createSttGpuPolicy(modelsDir: string, gpuEnabled: () => boolean): SttGpuPolicy {
const file = join(modelsDir, GPU_FAILURE_MARKER_FILE)
// Read once per process; this process's own failures update it in memory.
let failedKey: string | null | undefined
return {
allowGpu: () => {
if (!gpuEnabled()) {
return false
}
const marker = readMarkerKey(file)
return marker === null || marker !== gpuFailureKey()
if (failedKey === undefined) {
failedKey = readMarkerKey(file)
}
return failedKey !== gpuFailureKey()
},
markGpuFailed: () => {
try {
writeFileSync(file, JSON.stringify({ key: gpuFailureKey() }))
} catch (error) {
failedKey = gpuFailureKey()
writeFile(file, JSON.stringify({ key: failedKey })).catch((error: unknown) => {
console.warn('[speech] Could not record the GPU failure:', error)
}
})
}
}
}
@@ -1,4 +1,4 @@
import { existsSync, readFileSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { availableParallelism } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
@@ -17,11 +17,9 @@ function performanceCoreThreads(): number {
// The hotwords file is written in sherpa's format, one `term :score` per line. The host deletes it
// once dictation starts, so a CPU fallback's reload finds it gone and runs without.
function readVocabulary(path: string | undefined): string[] {
if (!path || !existsSync(path)) {
return []
}
return readFileSync(path, 'utf8')
async function readVocabulary(path: string | undefined): Promise<string[]> {
const text = path ? await readFile(path, 'utf8').catch(() => '') : ''
return text
.split('\n')
.map((line) => line.replace(/\s+:[\d.]+\s*$/, '').trim())
.filter(Boolean)
@@ -46,7 +44,7 @@ export async function createTranscribeCppRecognizer(
)
// The child runs one call at a time, so every stream can share one session.
const session = model.createSession({ nThreads: performanceCoreThreads() })
const vocabulary = readVocabulary(init.hotwordsFilePath)
const vocabulary = await readVocabulary(init.hotwordsFilePath)
const options: TranscribeCpp.TranscribeOptions = {
timestamps: 'none',
...(init.language && model.capabilities.languages.includes(init.language)
+12 -1
View File
@@ -36,9 +36,20 @@ function speechNodeModulesDirCandidates(): string[] {
return require.resolve.paths('transcribe-cpp') ?? []
}
// The app's engine packages don't change while it runs, so each engine is looked up once.
const speechEngineModulesDirs = new Map<SpeechEngine, string | null>()
/** The node_modules directory holding `engine`, or null on hosts without it. */
export function getSpeechEngineModulesDir(engine: SpeechEngine): string | null {
return speechNodeModulesDirCandidates().find((dir) => hasSpeechEnginePackage(dir, engine)) ?? null
let dir = speechEngineModulesDirs.get(engine)
if (dir === undefined) {
dir =
speechNodeModulesDirCandidates().find((candidate) =>
hasSpeechEnginePackage(candidate, engine)
) ?? null
speechEngineModulesDirs.set(engine, dir)
}
return dir
}
/** Whether this host ships the engine `model` runs on. */