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

Commit c5bb21f

Browse files
committed
Merge pull request #2270 from stephentoub/directory_iterate_list
Remove List<T> allocation from directory enumeration
2 parents 620e103 + 7b5006b commit c5bb21f

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/Common/src/System/Collections/Generic/EnumerableHelpers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ namespace System.Collections.Generic
66
/// <summary>Internal helper functions for working with enumerables.</summary>
77
internal static class EnumerableHelpers
88
{
9+
/// <summary>Converts an enumerable to an array using the same logic as does List{T}.</summary>
10+
internal static T[] ToArray<T>(IEnumerable<T> source)
11+
{
12+
int count;
13+
T[] results = ToArray(source, out count);
14+
Array.Resize(ref results, count);
15+
return results;
16+
}
17+
918
/// <summary>Converts an enumerable to an array using the same logic as does List{T}.</summary>
1019
/// <param name="length">The number of items stored in the resulting array, 0-indexed.</param>
1120
/// <returns>

src/System.IO.FileSystem/src/System.IO.FileSystem.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
33
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
44
<PropertyGroup>
@@ -46,6 +46,9 @@
4646
<Compile Include="System\IO\ReadLinesIterator.cs" />
4747
<Compile Include="System\IO\SearchOption.cs" />
4848
<Compile Include="System\IO\SearchTarget.cs" />
49+
<Compile Include="$(CommonPath)\System\Collections\Generic\EnumerableHelpers.cs">
50+
<Link>Common\System\Collections\Generic\EnumerableHelpers.cs</Link>
51+
</Compile>
4952
<Compile Include="$(CommonPath)\System\IO\StringBuilderCache.cs">
5053
<Link>Common\System\IO\StringBuilderCache.cs</Link>
5154
</Compile>

src/System.IO.FileSystem/src/System/IO/Directory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ internal static String[] InternalGetFileDirectoryNames(String path, String userP
351351

352352
IEnumerable<String> enumerable = FileSystem.Current.EnumeratePaths(path, searchPattern, searchOption,
353353
(includeFiles ? SearchTarget.Files : 0) | (includeDirs ? SearchTarget.Directories : 0));
354-
List<String> fileList = new List<String>(enumerable);
355-
return fileList.ToArray();
354+
return EnumerableHelpers.ToArray(enumerable);
356355
}
357356

358357
public static IEnumerable<String> EnumerateDirectories(String path)

src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ private FileInfo[] InternalGetFiles(String searchPattern, SearchOption searchOpt
179179
Contract.Requires(searchPattern != null);
180180
Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
181181

182-
IEnumerable<FileInfo> enble = (IEnumerable<FileInfo>)FileSystem.Current.EnumerateFileSystemInfos(FullPath, searchPattern, searchOption, SearchTarget.Files);
183-
List<FileInfo> fileList = new List<FileInfo>(enble);
184-
return fileList.ToArray();
182+
IEnumerable<FileInfo> enumerable = (IEnumerable<FileInfo>)FileSystem.Current.EnumerateFileSystemInfos(FullPath, searchPattern, searchOption, SearchTarget.Files);
183+
return EnumerableHelpers.ToArray(enumerable);
185184
}
186185

187186
// Returns an array of Files in the DirectoryInfo specified by path
@@ -228,8 +227,7 @@ private FileSystemInfo[] InternalGetFileSystemInfos(String searchPattern, Search
228227
Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
229228

230229
IEnumerable<FileSystemInfo> enumerable = FileSystem.Current.EnumerateFileSystemInfos(FullPath, searchPattern, searchOption, SearchTarget.Both);
231-
List<FileSystemInfo> fileList = new List<FileSystemInfo>(enumerable);
232-
return fileList.ToArray();
230+
return EnumerableHelpers.ToArray(enumerable);
233231
}
234232

235233
// Returns an array of strongly typed FileSystemInfo entries which will contain a listing
@@ -274,8 +272,7 @@ private DirectoryInfo[] InternalGetDirectories(String searchPattern, SearchOptio
274272
Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
275273

276274
IEnumerable<DirectoryInfo> enumerable = (IEnumerable<DirectoryInfo>)FileSystem.Current.EnumerateFileSystemInfos(FullPath, searchPattern, searchOption, SearchTarget.Directories);
277-
List<DirectoryInfo> fileList = new List<DirectoryInfo>(enumerable);
278-
return fileList.ToArray();
275+
return EnumerableHelpers.ToArray(enumerable);
279276
}
280277

281278
public IEnumerable<DirectoryInfo> EnumerateDirectories()

0 commit comments

Comments
 (0)