Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor allocation optimizations / cleanup #6983

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was unused.

{
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a more concise way of expressing this. My understanding is that if there is an out parameter then all parameters have to be listed complete with types.

=> 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