Skip to content

Commit 761f06e

Browse files
authored
Minor allocation optimizations / cleanup (#6983)
1 parent f8c114a commit 761f06e

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

src/Build/BackEnd/Components/SdkResolution/CachingSdkResolverService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public override SdkResult ResolveSdk(int submissionId, SdkReference sdk, Logging
4747
else
4848
{
4949
// Get the dictionary for the specified submission if one is already added otherwise create a new dictionary for the submission.
50-
ConcurrentDictionary<string, Lazy<SdkResult>> cached = _cache.GetOrAdd(submissionId, new ConcurrentDictionary<string, Lazy<SdkResult>>(MSBuildNameIgnoreCaseComparer.Default));
50+
ConcurrentDictionary<string, Lazy<SdkResult>> cached = _cache.GetOrAdd(
51+
submissionId,
52+
_ => new ConcurrentDictionary<string, Lazy<SdkResult>>(MSBuildNameIgnoreCaseComparer.Default));
5153

5254
/*
5355
* Get a Lazy<SdkResult> if available, otherwise create a Lazy<SdkResult> which will resolve the SDK with the SdkResolverService.Instance. If multiple projects are attempting to resolve

src/Build/BackEnd/Components/SdkResolution/SdkResolverService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ private void SetResolverState(int submissionId, SdkResolver resolver, object sta
247247
// Do not set state for resolution requests that are not associated with a valid build submission ID
248248
if (submissionId != BuildEventContext.InvalidSubmissionId)
249249
{
250-
ConcurrentDictionary<SdkResolver, object> resolverState = _resolverStateBySubmission.GetOrAdd(submissionId, new ConcurrentDictionary<SdkResolver, object>(NativeMethodsShared.GetLogicalCoreCount(), _resolvers.Count));
250+
ConcurrentDictionary<SdkResolver, object> resolverState = _resolverStateBySubmission.GetOrAdd(
251+
submissionId,
252+
_ => new ConcurrentDictionary<SdkResolver, object>(NativeMethodsShared.GetLogicalCoreCount(), _resolvers.Count));
251253

252254
resolverState.AddOrUpdate(resolver, state, (sdkResolver, obj) => state);
253255
}

src/Build/Utilities/EngineFileUtilities.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,13 @@ internal static Func<string, bool> GetFileSpecMatchTester(IList<string> filespec
236236
return file => matchers.Any(m => m.Value.IsMatch(file));
237237
}
238238

239-
internal class IOCache
239+
internal sealed class IOCache
240240
{
241241
private readonly Lazy<ConcurrentDictionary<string, bool>> existenceCache = new Lazy<ConcurrentDictionary<string, bool>>(() => new ConcurrentDictionary<string, bool>(), true);
242242

243-
public virtual bool DirectoryExists(string directory)
243+
public bool DirectoryExists(string directory)
244244
{
245-
return existenceCache.Value.GetOrAdd(directory, Directory.Exists);
246-
}
247-
248-
public virtual bool FileExists(string file)
249-
{
250-
return existenceCache.Value.GetOrAdd(file, File.Exists);
245+
return existenceCache.Value.GetOrAdd(directory, directory => Directory.Exists(directory));
251246
}
252247
}
253248
}

src/Shared/FileUtilities.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ internal static bool DirectoryExistsNoThrow(string fullPath, IFileSystem fileSys
903903
fileSystem ??= DefaultFileSystem;
904904

905905
return Traits.Instance.CacheFileExistence
906-
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.DirectoryExists)
906+
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.DirectoryExists(fullPath))
907907
: fileSystem.DirectoryExists(fullPath);
908908
}
909909
catch
@@ -927,7 +927,7 @@ internal static bool FileExistsNoThrow(string fullPath, IFileSystem fileSystem =
927927
fileSystem ??= DefaultFileSystem;
928928

929929
return Traits.Instance.CacheFileExistence
930-
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.FileExists)
930+
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.FileExists(fullPath))
931931
: fileSystem.FileExists(fullPath);
932932
}
933933
catch
@@ -951,7 +951,7 @@ internal static bool FileOrDirectoryExistsNoThrow(string fullPath, IFileSystem f
951951
fileSystem ??= DefaultFileSystem;
952952

953953
return Traits.Instance.CacheFileExistence
954-
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.FileOrDirectoryExists)
954+
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.FileOrDirectoryExists(fullPath))
955955
: fileSystem.FileOrDirectoryExists(fullPath);
956956
}
957957
catch

src/Tasks/AssemblyDependency/ResolveAssemblyReference.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,23 +3153,26 @@ public override bool Execute()
31533153
{
31543154
return Execute
31553155
(
3156-
new FileExists(p => FileUtilities.FileExistsNoThrow(p)),
3157-
new DirectoryExists(p => FileUtilities.DirectoryExistsNoThrow(p)),
3158-
new GetDirectories(Directory.GetDirectories),
3159-
new GetAssemblyName(AssemblyNameExtension.GetAssemblyNameEx),
3160-
new GetAssemblyMetadata(AssemblyInformation.GetAssemblyMetadata),
3156+
p => FileUtilities.FileExistsNoThrow(p),
3157+
p => FileUtilities.DirectoryExistsNoThrow(p),
3158+
(p, searchPattern) => Directory.GetDirectories(p, searchPattern),
3159+
p => AssemblyNameExtension.GetAssemblyNameEx(p),
3160+
(string path, ConcurrentDictionary<string, AssemblyMetadata> assemblyMetadataCache, out AssemblyNameExtension[] dependencies, out string[] scatterFiles, out FrameworkNameVersioning frameworkName)
3161+
=> AssemblyInformation.GetAssemblyMetadata(path, assemblyMetadataCache, out dependencies, out scatterFiles, out frameworkName),
31613162
#if FEATURE_WIN32_REGISTRY
3162-
new GetRegistrySubKeyNames(RegistryHelper.GetSubKeyNames),
3163-
new GetRegistrySubKeyDefaultValue(RegistryHelper.GetDefaultValue),
3163+
(baseKey, subkey) => RegistryHelper.GetSubKeyNames(baseKey, subkey),
3164+
(baseKey, subkey) => RegistryHelper.GetDefaultValue(baseKey, subkey),
31643165
#endif
3165-
new GetLastWriteTime(NativeMethodsShared.GetLastWriteFileUtcTime),
3166-
new GetAssemblyRuntimeVersion(AssemblyInformation.GetRuntimeVersion),
3166+
p => NativeMethodsShared.GetLastWriteFileUtcTime(p),
3167+
p => AssemblyInformation.GetRuntimeVersion(p),
31673168
#if FEATURE_WIN32_REGISTRY
3168-
new OpenBaseKey(RegistryHelper.OpenBaseKey),
3169+
(hive, view) => RegistryHelper.OpenBaseKey(hive, view),
31693170
#endif
3170-
new GetAssemblyPathInGac(GetAssemblyPathInGac),
3171-
new IsWinMDFile(AssemblyInformation.IsWinMDFile),
3172-
new ReadMachineTypeFromPEHeader(ReferenceTable.ReadMachineTypeFromPEHeader)
3171+
(assemblyName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, fullFusionName, specificVersion)
3172+
=> GetAssemblyPathInGac(assemblyName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, fullFusionName, specificVersion),
3173+
(string fullPath, GetAssemblyRuntimeVersion getAssemblyRuntimeVersion, FileExists fileExists, out string imageRuntimeVersion, out bool isManagedWinmd)
3174+
=> AssemblyInformation.IsWinMDFile(fullPath, getAssemblyRuntimeVersion, fileExists, out imageRuntimeVersion, out isManagedWinmd),
3175+
p => ReferenceTable.ReadMachineTypeFromPEHeader(p)
31733176
);
31743177
}
31753178

0 commit comments

Comments
 (0)