Skip to content

Commit 655bfe5

Browse files
johnpcbridgemill-ch
andcommitted
fix: remove O(n²) bottleneck in large file scans (#46)
Removed redundant findMatchingVideoFile call and per-file WebSocket broadcasts from the run:files_found handler. With 32k+ subtitle files, the synchronous disk I/O and DB queries blocked the event loop entirely. Video path matching now happens in processFile when the file is actually processed, and video_path is stored in the DB at that point. Co-authored-by: bridgemill-ch <bridgemill-ch@users.noreply.github.com>
1 parent 648e434 commit 655bfe5

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/coordinator.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ProcessingEngine } from './processingEngine';
22
import { StateManager } from './stateManager';
33
import { ScanConfig } from './config';
4-
import { findMatchingVideoFile } from './findMatchingVideoFile';
54
import { Run } from './database';
65
import { once } from 'events';
76

@@ -33,16 +32,15 @@ export class ProcessingCoordinator {
3332
this.engine.on('run:files_found', (files: string[]) => {
3433
this.currentRunId = this.stateManager.startRun(files.length, this.enabledEngines);
3534

36-
// Add all files to database as pending
37-
files.forEach((filePath) => {
38-
const videoPath = findMatchingVideoFile(filePath);
39-
this.stateManager.addFile(this.currentRunId!, filePath, videoPath);
40-
});
35+
// Add all files to database as pending (video matching happens during processing)
36+
for (const filePath of files) {
37+
this.stateManager.addFile(this.currentRunId!, filePath, null);
38+
}
4139
});
4240

43-
this.engine.on('file:started', ({ srtPath }: { srtPath: string }) => {
41+
this.engine.on('file:started', ({ srtPath, videoPath }: { srtPath: string; videoPath: string | null }) => {
4442
if (this.currentRunId) {
45-
this.stateManager.updateFileStatus(this.currentRunId, srtPath, 'processing', null);
43+
this.stateManager.updateFileStatus(this.currentRunId, srtPath, 'processing', null, videoPath);
4644
}
4745
});
4846

src/stateManager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ export class StateManager extends EventEmitter {
105105
// File management
106106
addFile(runId: string, filePath: string, videoPath: string | null): void {
107107
this.db.createFileResult(runId, filePath, videoPath);
108-
this.emitFileUpdate(runId, filePath);
109108
}
110109

111-
updateFileStatus(runId: string, filePath: string, status: FileResult['status'], currentEngine?: string | null): void {
110+
updateFileStatus(runId: string, filePath: string, status: FileResult['status'], currentEngine?: string | null, videoPath?: string | null): void {
112111
const updates: Partial<FileResult> = { status };
113112
if (currentEngine !== undefined) {
114113
updates.current_engine = currentEngine;
115114
}
115+
if (videoPath !== undefined) {
116+
updates.video_path = videoPath;
117+
}
116118

117119
this.db.updateFileResult(runId, filePath, updates);
118120
this.emitFileUpdate(runId, filePath);

0 commit comments

Comments
 (0)