Skip to content

Commit 20ed6b5

Browse files
committed
replace FindFirstFileW with FindFirstFileExW and apply optimization
1 parent a065b8e commit 20ed6b5

File tree

1 file changed

+25
-4
lines changed
  • std/src/sys/pal/windows

1 file changed

+25
-4
lines changed

std/src/sys/pal/windows/fs.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,22 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
10471047
let path = maybe_verbatim(&star)?;
10481048

10491049
unsafe {
1050-
let mut wfd = mem::zeroed();
1051-
let find_handle = c::FindFirstFileW(path.as_ptr(), &mut wfd);
1050+
let mut wfd: c::WIN32_FIND_DATAW = mem::zeroed();
1051+
// this is like FindFirstFileW (see https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfileexw),
1052+
// but with FindExInfoBasic it should skip filling WIN32_FIND_DATAW.cAlternateFileName
1053+
// (see https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw)
1054+
// (which will be always null string value and currently unused) and should be faster.
1055+
//
1056+
// We can pass FIND_FIRST_EX_LARGE_FETCH to dwAdditionalFlags to speed up things more,
1057+
// but as we don't know user's use profile of this function, lets be conservative.
1058+
let find_handle = c::FindFirstFileExW(
1059+
path.as_ptr(),
1060+
c::FindExInfoBasic,
1061+
&mut wfd as *mut _ as _,
1062+
c::FindExSearchNameMatch,
1063+
ptr::null(),
1064+
0,
1065+
);
10521066

10531067
if find_handle != c::INVALID_HANDLE_VALUE {
10541068
Ok(ReadDir {
@@ -1242,8 +1256,15 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
12421256
// `ERROR_SHARING_VIOLATION` means the file exists (but is locked)
12431257
// therefore it's safe to assume the file name given does not
12441258
// include wildcards.
1245-
let mut wfd = mem::zeroed();
1246-
let handle = c::FindFirstFileW(path.as_ptr(), &mut wfd);
1259+
let mut wfd: c::WIN32_FIND_DATAW = mem::zeroed();
1260+
let handle = c::FindFirstFileExW(
1261+
path.as_ptr(),
1262+
c::FindExInfoBasic,
1263+
&mut wfd as *mut _ as _,
1264+
c::FindExSearchNameMatch,
1265+
ptr::null(),
1266+
0,
1267+
);
12471268

12481269
if handle == c::INVALID_HANDLE_VALUE {
12491270
// This can fail if the user does not have read access to the

0 commit comments

Comments
 (0)