-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf(core): Optimize file collection with UTF-8 fast path and promise pool #1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7dcdbae
perf(core): Add UTF-8 fast path to skip expensive jschardet encoding …
yamadashy e97691d
perf(core): Replace worker threads with promise pool for file collection
yamadashy 05f11f4
refactor(core): Remove unused fileCollect worker infrastructure
yamadashy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,82 +1,82 @@ | ||
| import path from 'node:path'; | ||
| import pc from 'picocolors'; | ||
| import type { RepomixConfigMerged } from '../../config/configSchema.js'; | ||
| import { logger } from '../../shared/logger.js'; | ||
| import { initTaskRunner } from '../../shared/processConcurrency.js'; | ||
| import type { RepomixProgressCallback } from '../../shared/types.js'; | ||
| import { readRawFile as defaultReadRawFile, type FileSkipReason } from './fileRead.js'; | ||
| import type { RawFile } from './fileTypes.js'; | ||
| import type { FileCollectResult, FileCollectTask, SkippedFileInfo } from './workers/fileCollectWorker.js'; | ||
|
|
||
| // Concurrency limit for parallel file reads on the main thread. | ||
| // 50 balances I/O throughput with FD/memory safety across different machines. | ||
| const FILE_COLLECT_CONCURRENCY = 50; | ||
|
|
||
| export interface SkippedFileInfo { | ||
| path: string; | ||
| reason: FileSkipReason; | ||
| } | ||
|
|
||
| export interface FileCollectResults { | ||
| rawFiles: RawFile[]; | ||
| skippedFiles: SkippedFileInfo[]; | ||
| } | ||
|
|
||
| // Re-export SkippedFileInfo for external use | ||
| export type { SkippedFileInfo } from './workers/fileCollectWorker.js'; | ||
| const promisePool = async <T, R>(items: T[], concurrency: number, fn: (item: T) => Promise<R>): Promise<R[]> => { | ||
| const results: R[] = Array.from({ length: items.length }); | ||
| let nextIndex = 0; | ||
|
|
||
| const worker = async () => { | ||
| while (nextIndex < items.length) { | ||
| const i = nextIndex++; | ||
| results[i] = await fn(items[i]); | ||
| } | ||
| }; | ||
|
|
||
| await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => worker())); | ||
|
|
||
| return results; | ||
| }; | ||
|
|
||
| export const collectFiles = async ( | ||
| filePaths: string[], | ||
| rootDir: string, | ||
| config: RepomixConfigMerged, | ||
| progressCallback: RepomixProgressCallback = () => {}, | ||
| deps = { | ||
| initTaskRunner, | ||
| readRawFile: defaultReadRawFile, | ||
| }, | ||
| ): Promise<FileCollectResults> => { | ||
| const taskRunner = deps.initTaskRunner<FileCollectTask, FileCollectResult>({ | ||
| numOfTasks: filePaths.length, | ||
| workerType: 'fileCollect', | ||
| runtime: 'worker_threads', | ||
| const startTime = process.hrtime.bigint(); | ||
| logger.trace(`Starting file collection for ${filePaths.length} files`); | ||
|
|
||
| let completedTasks = 0; | ||
| const totalTasks = filePaths.length; | ||
| const maxFileSize = config.input.maxFileSize; | ||
|
|
||
| const results = await promisePool(filePaths, FILE_COLLECT_CONCURRENCY, async (filePath) => { | ||
| const fullPath = path.resolve(rootDir, filePath); | ||
| const result = await deps.readRawFile(fullPath, maxFileSize); | ||
|
|
||
| completedTasks++; | ||
| progressCallback(`Collect file... (${completedTasks}/${totalTasks}) ${pc.dim(filePath)}`); | ||
| logger.trace(`Collect files... (${completedTasks}/${totalTasks}) ${filePath}`); | ||
|
|
||
| return { filePath, result }; | ||
| }); | ||
| const tasks = filePaths.map( | ||
| (filePath) => | ||
| ({ | ||
| filePath, | ||
| rootDir, | ||
| maxFileSize: config.input.maxFileSize, | ||
| }) satisfies FileCollectTask, | ||
| ); | ||
|
|
||
| try { | ||
| const startTime = process.hrtime.bigint(); | ||
| logger.trace(`Starting file collection for ${filePaths.length} files using worker pool`); | ||
|
|
||
| let completedTasks = 0; | ||
| const totalTasks = tasks.length; | ||
|
|
||
| const results = await Promise.all( | ||
| tasks.map((task) => | ||
| taskRunner.run(task).then((result) => { | ||
| completedTasks++; | ||
| progressCallback(`Collect file... (${completedTasks}/${totalTasks}) ${pc.dim(task.filePath)}`); | ||
| logger.trace(`Collect files... (${completedTasks}/${totalTasks}) ${task.filePath}`); | ||
| return result; | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const endTime = process.hrtime.bigint(); | ||
| const duration = Number(endTime - startTime) / 1e6; | ||
| logger.trace(`File collection completed in ${duration.toFixed(2)}ms`); | ||
|
|
||
| const rawFiles: RawFile[] = []; | ||
| const skippedFiles: SkippedFileInfo[] = []; | ||
|
|
||
| for (const result of results) { | ||
| if (result.rawFile) { | ||
| rawFiles.push(result.rawFile); | ||
| } | ||
| if (result.skippedFile) { | ||
| skippedFiles.push(result.skippedFile); | ||
| } | ||
| } | ||
|
|
||
| return { rawFiles, skippedFiles }; | ||
| } catch (error) { | ||
| logger.error('Error during file collection:', error); | ||
| throw error; | ||
| } finally { | ||
| // Always cleanup worker pool | ||
| await taskRunner.cleanup(); | ||
| const rawFiles: RawFile[] = []; | ||
| const skippedFiles: SkippedFileInfo[] = []; | ||
|
|
||
| for (const { filePath, result } of results) { | ||
| if (result.content !== null) { | ||
| rawFiles.push({ path: filePath, content: result.content }); | ||
| } else if (result.skippedReason) { | ||
| skippedFiles.push({ path: filePath, reason: result.skippedReason }); | ||
| } | ||
| } | ||
|
|
||
| const endTime = process.hrtime.bigint(); | ||
| const duration = Number(endTime - startTime) / 1e6; | ||
| logger.trace(`File collection completed in ${duration.toFixed(2)}ms`); | ||
|
|
||
| return { rawFiles, skippedFiles }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.