Skip to content

Commit

Permalink
Minor allocation optimizations / cleanup (#6983)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladipro committed Nov 1, 2021
1 parent f8c114a commit 761f06e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
Expand Up @@ -47,7 +47,9 @@ public override SdkResult ResolveSdk(int submissionId, SdkReference sdk, Logging
else
{
// Get the dictionary for the specified submission if one is already added otherwise create a new dictionary for the submission.
ConcurrentDictionary<string, Lazy<SdkResult>> cached = _cache.GetOrAdd(submissionId, new ConcurrentDictionary<string, Lazy<SdkResult>>(MSBuildNameIgnoreCaseComparer.Default));
ConcurrentDictionary<string, Lazy<SdkResult>> cached = _cache.GetOrAdd(
submissionId,
_ => new ConcurrentDictionary<string, Lazy<SdkResult>>(MSBuildNameIgnoreCaseComparer.Default));

/*
* 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
Expand Down
Expand Up @@ -247,7 +247,9 @@ private void SetResolverState(int submissionId, SdkResolver resolver, object sta
// Do not set state for resolution requests that are not associated with a valid build submission ID
if (submissionId != BuildEventContext.InvalidSubmissionId)
{
ConcurrentDictionary<SdkResolver, object> resolverState = _resolverStateBySubmission.GetOrAdd(submissionId, new ConcurrentDictionary<SdkResolver, object>(NativeMethodsShared.GetLogicalCoreCount(), _resolvers.Count));
ConcurrentDictionary<SdkResolver, object> resolverState = _resolverStateBySubmission.GetOrAdd(
submissionId,
_ => new ConcurrentDictionary<SdkResolver, object>(NativeMethodsShared.GetLogicalCoreCount(), _resolvers.Count));

resolverState.AddOrUpdate(resolver, state, (sdkResolver, obj) => state);
}
Expand Down
11 changes: 3 additions & 8 deletions src/Build/Utilities/EngineFileUtilities.cs
Expand Up @@ -236,18 +236,13 @@ private static bool MatchesLazyWildcard(string fileSpec)
return file => matchers.Any(m => m.Value.IsMatch(file));
}

internal class IOCache
internal sealed class IOCache
{
private readonly Lazy<ConcurrentDictionary<string, bool>> existenceCache = new Lazy<ConcurrentDictionary<string, bool>>(() => new ConcurrentDictionary<string, bool>(), true);

public virtual bool DirectoryExists(string directory)
public bool DirectoryExists(string directory)
{
return existenceCache.Value.GetOrAdd(directory, Directory.Exists);
}

public virtual bool FileExists(string file)
{
return existenceCache.Value.GetOrAdd(file, File.Exists);
return existenceCache.Value.GetOrAdd(directory, directory => Directory.Exists(directory));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/FileUtilities.cs
Expand Up @@ -903,7 +903,7 @@ internal static bool DirectoryExistsNoThrow(string fullPath, IFileSystem fileSys
fileSystem ??= DefaultFileSystem;

return Traits.Instance.CacheFileExistence
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.DirectoryExists)
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.DirectoryExists(fullPath))
: fileSystem.DirectoryExists(fullPath);
}
catch
Expand All @@ -927,7 +927,7 @@ internal static bool FileExistsNoThrow(string fullPath, IFileSystem fileSystem =
fileSystem ??= DefaultFileSystem;

return Traits.Instance.CacheFileExistence
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.FileExists)
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.FileExists(fullPath))
: fileSystem.FileExists(fullPath);
}
catch
Expand All @@ -951,7 +951,7 @@ internal static bool FileOrDirectoryExistsNoThrow(string fullPath, IFileSystem f
fileSystem ??= DefaultFileSystem;

return Traits.Instance.CacheFileExistence
? FileExistenceCache.GetOrAdd(fullPath, fileSystem.FileOrDirectoryExists)
? FileExistenceCache.GetOrAdd(fullPath, fullPath => fileSystem.FileOrDirectoryExists(fullPath))
: fileSystem.FileOrDirectoryExists(fullPath);
}
catch
Expand Down
29 changes: 16 additions & 13 deletions src/Tasks/AssemblyDependency/ResolveAssemblyReference.cs
Expand Up @@ -3153,23 +3153,26 @@ public override bool Execute()
{
return Execute
(
new FileExists(p => FileUtilities.FileExistsNoThrow(p)),
new DirectoryExists(p => FileUtilities.DirectoryExistsNoThrow(p)),
new GetDirectories(Directory.GetDirectories),
new GetAssemblyName(AssemblyNameExtension.GetAssemblyNameEx),
new GetAssemblyMetadata(AssemblyInformation.GetAssemblyMetadata),
p => FileUtilities.FileExistsNoThrow(p),
p => FileUtilities.DirectoryExistsNoThrow(p),
(p, searchPattern) => Directory.GetDirectories(p, searchPattern),
p => AssemblyNameExtension.GetAssemblyNameEx(p),
(string path, ConcurrentDictionary<string, AssemblyMetadata> assemblyMetadataCache, out AssemblyNameExtension[] dependencies, out string[] scatterFiles, out FrameworkNameVersioning frameworkName)
=> AssemblyInformation.GetAssemblyMetadata(path, assemblyMetadataCache, out dependencies, out scatterFiles, out frameworkName),
#if FEATURE_WIN32_REGISTRY
new GetRegistrySubKeyNames(RegistryHelper.GetSubKeyNames),
new GetRegistrySubKeyDefaultValue(RegistryHelper.GetDefaultValue),
(baseKey, subkey) => RegistryHelper.GetSubKeyNames(baseKey, subkey),
(baseKey, subkey) => RegistryHelper.GetDefaultValue(baseKey, subkey),
#endif
new GetLastWriteTime(NativeMethodsShared.GetLastWriteFileUtcTime),
new GetAssemblyRuntimeVersion(AssemblyInformation.GetRuntimeVersion),
p => NativeMethodsShared.GetLastWriteFileUtcTime(p),
p => AssemblyInformation.GetRuntimeVersion(p),
#if FEATURE_WIN32_REGISTRY
new OpenBaseKey(RegistryHelper.OpenBaseKey),
(hive, view) => RegistryHelper.OpenBaseKey(hive, view),
#endif
new GetAssemblyPathInGac(GetAssemblyPathInGac),
new IsWinMDFile(AssemblyInformation.IsWinMDFile),
new ReadMachineTypeFromPEHeader(ReferenceTable.ReadMachineTypeFromPEHeader)
(assemblyName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, fullFusionName, specificVersion)
=> GetAssemblyPathInGac(assemblyName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, fullFusionName, specificVersion),
(string fullPath, GetAssemblyRuntimeVersion getAssemblyRuntimeVersion, FileExists fileExists, out string imageRuntimeVersion, out bool isManagedWinmd)
=> AssemblyInformation.IsWinMDFile(fullPath, getAssemblyRuntimeVersion, fileExists, out imageRuntimeVersion, out isManagedWinmd),
p => ReferenceTable.ReadMachineTypeFromPEHeader(p)
);
}

Expand Down

0 comments on commit 761f06e

Please sign in to comment.