2026-11-26 · 5 min read · #Rust #Win32 #Systems

Bypassing std::fs with Win32 FindFirstFileExW in Rust

How scannex replaced 20-minute C:\ drive walks with direct Win32 directory enumeration, lock-free MPMC queues, and zero-contention thread-local accumulators.

Standard library filesystem iterators are designed for portability, not throughput. Calling std::fs::read_dir across millions of NTFS entries allocates a fresh PathBuf and queries extra metadata per entry that a storage analyzer does not need. In scannex, dropping down to raw Win32 removed the bottleneck completely.

1. FindFirstFileExW & Large Fetches

Instead of FindFirstFileW, calling FindFirstFileExW with FindExInfoBasic skips 8.3 short-name generation, and FIND_FIRST_EX_LARGE_FETCH batches directory entries into larger kernel buffers.

  • Extended-length paths: Prefixing \\?\ in reusable UTF-16 scratch buffers avoids MAX_PATH truncation.
  • Reparse-point safety: Checking FILE_ATTRIBUTE_REPARSE_POINT prevents infinite loops on NTFS junctions and symlinks.
  • Actual vs. logical size: GetCompressedFileSizeW distinguishes sparse/compressed allocation from logical file length.

2. Zero-Contention Thread-Local Heaps

Workers pull directories from a lock-free crossbeam-channel MPMC queue and accumulate extension stats into thread-local FxHashMap tables and bounded Top-K heaps. Results are merged exactly once when the queue drains.

cargo build --release
rust_scanner.exe C:\ --mode detailed --ui