Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d58c770

Browse files
committed
ZipFile: Reduce allocations in IsDirEmpty
- Use Directory.EnumerateFileSystemEntries to avoid the extra allocations associated with the FileInfo/DirectoryInfo instance created by DirectoryInfo.EnumerateFileSystemInfos. - Use the default overload of EnumerateFileSystemEntries, which uses SearchOption.TopDirectoryOnly, as SearchOption.AllDirectories is not necessary here and AllDirectories currently has more allocations than TopDirectoryOnly on Windows. - Use the enumerator directly to avoid the unnecessary call to Current.
1 parent a73b4d7 commit d58c770

File tree

1 file changed

+3
-4
lines changed
  • src/System.IO.Compression.ZipFile/src/System/IO/Compression

1 file changed

+3
-4
lines changed

src/System.IO.Compression.ZipFile/src/System/IO/Compression/ZipFile.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Text;
67

@@ -569,10 +570,8 @@ private static string EntryFromPath(string entry, int offset, int length)
569570

570571
private static Boolean IsDirEmpty(DirectoryInfo possiblyEmptyDir)
571572
{
572-
foreach (FileSystemInfo fi in possiblyEmptyDir.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
573-
return false;
574-
575-
return true;
573+
using (IEnumerator<String> enumerator = Directory.EnumerateFileSystemEntries(possiblyEmptyDir.FullName).GetEnumerator())
574+
return !enumerator.MoveNext();
576575
}
577576
} // class ZipFile
578577
} // namespace

0 commit comments

Comments
 (0)