Skip to content

Optimize getSubjectFromPath to avoid concurrent stat calls on large globs#352

Closed
bdehamer with Copilot wants to merge 5 commits into
bdehamer/esmfrom
copilot/sub-pr-347
Closed

Optimize getSubjectFromPath to avoid concurrent stat calls on large globs#352
bdehamer with Copilot wants to merge 5 commits into
bdehamer/esmfrom
copilot/sub-pr-347

Conversation

Copilot AI commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Addresses review feedback on #347 regarding performance issues when globs expand to large file lists.

Changes

  • Sequential stat with early exit: Replaced Promise.all(paths.map(stat)) with sequential iteration that terminates immediately when MAX_SUBJECT_COUNT is reached
  • Fixed off-by-one: Moved limit check before array push to correctly enforce 1024 maximum (was allowing 1025)

Before:

const stats = await Promise.all(paths.map(async p => fs.stat(p)))
const files = paths.filter((_, i) => stats[i].isFile())

if (files.length > MAX_SUBJECT_COUNT) {
  throw new Error(...)
}

After:

const files: string[] = []
for (const p of paths) {
  const stat = await fs.stat(p)
  if (stat.isFile()) {
    if (files.length >= MAX_SUBJECT_COUNT) {
      throw new Error(...)
    }
    files.push(p)
  }
}

Prevents issuing thousands of concurrent stat calls that could hit OS file descriptor limits or cause significant slowdown.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Update to address feedback from ESM conversion PR review Optimize getSubjectFromPath to avoid concurrent stat calls on large globs Feb 17, 2026
Copilot AI requested a review from bdehamer February 17, 2026 16:05
@bdehamer

Copy link
Copy Markdown
Collaborator

cherry-picked into #347

@bdehamer bdehamer closed this Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants