Skip to content

scanner: parallelize directory traversal with worker scheduler - #8

Closed
gaby wants to merge 1 commit into
mainfrom
refactor-walker.scan-for-parallel-directory-enumeration
Closed

gaby wants to merge 1 commit into
mainfrom
refactor-walker.scan-for-parallel-directory-enumeration

Conversation

@gaby

@gaby gaby commented Apr 27, 2026 •

Copy link
Copy Markdown
Owner

Motivation

  • Improve scan throughput by parallelizing directory enumeration while keeping behavior identical to the existing walker.
  • Preserve deterministic ignore handling (including negations) and existing error/warning reporting semantics.
  • Ensure cancellation (ctx.Done) and event emission remain correct under concurrent traversal.

Description

  • Introduce Config.Walkers (default defaultWalkWorkers) and implement walkWithScheduler, a bounded worker-pool scheduler that enqueues start paths and processes subdirectories concurrently with sorted DirNames enumeration.
  • Keep per-path ignore decisions using the existing matcher API (Matcher.Match, CanSkipDir) and perform those checks before deciding to enqueue child directories.
  • Replace the order-sensitive ignoredParent bookkeeping with a concurrency-safe emitIgnoredAncestors routine that deterministically emits required ignored-parent entries and records handled ancestors with a mutex-protected map.
  • Preserve cancellation and error semantics by checking ctx.Done() in enqueue/worker loops, propagating the first error and logging warnable errors as before, and closing toHashChan via defer once scanning completes.
  • Add/extend tests and helpers: TestWalkParallelParity, TestWalkParallelCancel, TestSkipIgnoredDirsWithNegation, and collectScanResults; add BenchmarkWalkSynthetic with wide/deep synthetic trees to validate throughput and memory characteristics.

Testing

  • Unit tests: ran targeted unit tests including TestWalk, TestWalkSub, TestRecurseInclude, TestSkipIgnoredDirsWithNegation, TestWalkParallelParity, TestWalkParallelCancel and TestStopWalk, all of which passed.
  • Benchmarks: ran BenchmarkWalkSynthetic variants; observed improvements for multi-worker runs versus single-worker: wide tree walkers-1 ≈ 243ms vs walkers-4 ≈ 164ms, deep tree walkers-1 ≈ 713ms vs walkers-4 ≈ 506ms; allocations remained in the same ballpark across worker counts.
  • All automated tests and the added benchmarks completed successfully in the scanner package during validation.

@gaby gaby closed this Jul 8, 2026
@gaby
gaby deleted the refactor-walker.scan-for-parallel-directory-enumeration branch July 8, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant