Agents In Use
What’s new todayLast update
1005 additions

How to use GitHub Copilot to optimize file searches

Copilot added safe Windows directory-search prefilters while keeping .NET's managed matching, with wildcard and cross-platform coverage before approval.

FROM THE SOURCE

Original prompt

Shared by the source author. See source notes for context.

Optimize common cases by passing "safe" patterns to `NtQueryDirectoryFile` as a pre-filter hint,

What happened

Copilot added safe Windows directory-search prefilters while keeping .NET's managed matching, with wildcard and cross-platform coverage before approval; the pull request merged into dotnet/runtime main.

Reported by Copilot (on behalf of stephentoub) on GitHub

How the agent did it

  1. Threaded search expression to Windows enumerator.
  2. Identified wildcard patterns safe for OS filtering.
  3. Retained managed filter for correctness.

Where a person stepped in

Human brainstormed approach with Copilot chat and reviewed/merged.

Stephen Toub used Copilot to explore a long-standing performance problem in Windows directory searches. .NET's enumeration path passed no filename filter to NtQueryDirectoryFile, then matched names in managed code. A selective pattern in a large directory therefore still required broad enumeration.

The chosen approach used the operating system as a prefilter while retaining the existing managed matcher. Copilot threaded the search expression into the Windows enumerator and added a check for patterns whose OS interpretation was safe. Simple literal-and-star patterns could take the optimized path; question marks, special DOS wildcard cases and invalid filename characters remained on the established path.

The managed filter continued checking every returned result, preserving .NET behavior even where OS matching could return extra names. The PR documents casing and wildcard coverage, including tests designed for Windows and Unix differences. Maintainers reviewed the implementation, and Toub merged it on January 26, 2026.

The source supports a reviewed optimization with explicit correctness boundaries; it does not establish a universal speedup for all patterns or filesystems.

Prompt
Original prompt

Source: GitHub · Copilot (on behalf of stephentoub)

Frequently asked questions

Why retain managed matching?

The OS filter is only a prefilter. The existing .NET matcher still validates returned names to preserve expected behavior.

Which patterns avoid the new fast path?

The PR excludes question marks, special DOS wildcard cases such as *.* and trailing *., and invalid filename characters.

Was this a cross-platform rewrite?

The optimization targets Windows enumeration. The PR documents Windows and Unix coverage to protect shared matching behavior.

More details

Remaining steps

  1. Added casing/DOS-wildcard/cross-platform tests.

About this account

GitHub Copilot investigated or implemented the scoped repository task and produced a pull request, with work and checks recorded for maintainer review.

Primary pull request and public API timeline opened and checked on 2026-10-14. PR creation date is the original publication date; merge dates are separate result evidence. Windows optimization; unsafe patterns retain original path. Test outcome reported by PR. Outcomes and test claims are attributed to the source and were not independently reproduced. Original prompt excerpt: reproduced verbatim from the public PR prompt; the full task contains additional context.

Credit: Copilot (on behalf of stephentoub) on GitHub

Source published 6 Jan 2026

Source checked 14 Oct 2026

Tools and topics

GitHub Copilot · .NET · NtQueryDirectoryFile · GitHub

Windows performance · Directory search · Wildcard semantics

Editorial completeness: 100/100. Measures documented fields, not agent performance or verified outcomes.