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

remove duplicate dictionary lookups #6168

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ public void ExtractMetadata(PackageBuilder builder, string assemblyPath)

if (!builder.Authors.Any())
{
if (assemblyMetadata.Properties.ContainsKey("authors"))
if (assemblyMetadata.Properties.TryGetValue("authors", out var authors))
{
builder.Authors.AddRange(assemblyMetadata.Properties["authors"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
builder.Authors.AddRange(authors.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
}
else if (!string.IsNullOrEmpty(assemblyMetadata.Company))
{
builder.Authors.Add(assemblyMetadata.Company);
}
}

if (assemblyMetadata.Properties.ContainsKey("owners"))
if (assemblyMetadata.Properties.TryGetValue("owners", out var owners))
{
builder.Owners.AddRange(assemblyMetadata.Properties["owners"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
builder.Owners.AddRange(owners.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
}

builder.Properties.AddRange(assemblyMetadata.Properties);
// Let the id be overriden by AssemblyMetadataAttribute
// This preserves the existing behavior if no id metadata
// This preserves the existing behavior if no id metadata
// is provided by the assembly.
if (builder.Properties.ContainsKey("id"))
if (builder.Properties.TryGetValue("id", out var id))
{
builder.Id = builder.Properties["id"];
builder.Id = id;
}
else
{
Expand Down Expand Up @@ -174,10 +174,10 @@ private static Dictionary<string, string> GetProperties(IList<CustomAttributeDat
var properties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// NOTE: we make this check only by attribute type fullname, and we try to duck
// type it, therefore enabling the same metadata extesibility behavior for other platforms
// that don't define the attribute already as part of the framework.
// A package author could simply declare this attribute in his own project, using
// the same namespace and members, and we'd pick it up automatically. This is consistent
// with what MS did in the past with the System.Runtime.CompilerServices.ExtensionAttribute
// that don't define the attribute already as part of the framework.
// A package author could simply declare this attribute in his own project, using
// the same namespace and members, and we'd pick it up automatically. This is consistent
// with what MS did in the past with the System.Runtime.CompilerServices.ExtensionAttribute
// which allowed Linq to be re-implemented for .NET 2.0 :).
var attributeName = typeof(AssemblyMetadataAttribute).FullName;
foreach (var attribute in attributes.Where(x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ public string InitializeProperties(Packaging.IPackageMetadata metadata)
_properties.Clear();

// Allow Id to be overriden by cmd line properties
if (ProjectProperties.ContainsKey("Id"))
if (ProjectProperties.TryGetValue("Id", out var id))
{
_properties.Add("Id", ProjectProperties["Id"]);
_properties.Add("Id", id);
}
else
{
Expand Down Expand Up @@ -776,9 +776,9 @@ private PackageDependency CreateDependencyFromProject(dynamic project, Dictionar
}

VersionRange versionRange = null;
if (dependencies.ContainsKey(builder.Id))
if (dependencies.TryGetValue(builder.Id, out PackageDependency dependency))
{
VersionRange nuspecVersion = dependencies[builder.Id].VersionRange;
VersionRange nuspecVersion = dependency.VersionRange;
if (nuspecVersion != null)
{
versionRange = nuspecVersion;
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public PackArgs GetPackArgs(IPackTaskRequest<IMSBuildItem> request)
if (request.NuspecProperties != null && request.NuspecProperties.Any())
{
packArgs.Properties.AddRange(ParsePropertiesAsDictionary(request.NuspecProperties));
if (packArgs.Properties.ContainsKey("version"))
if (packArgs.Properties.TryGetValue("version", out var version))
{
packArgs.Version = packArgs.Properties["version"];
packArgs.Version = version;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ internal static class DependencyGraphFinder
}
}

return dependencyNodes.ContainsKey(topLevelPackage)
? dependencyNodes[topLevelPackage]
: null;
return dependencyNodes.GetValueOrDefault(topLevelPackage);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public IReadOnlyList<string> GetConfiguredPackageSources(string term)
{
char c = term[i];

if (!currentNode.Children.ContainsKey(c))
if (!currentNode.Children.TryGetValue(c, out SearchNode? child))
{
break;
}

currentNode = currentNode.Children[c];
currentNode = child;

if (currentNode.IsGlobbing)
{
Expand Down Expand Up @@ -172,7 +172,7 @@ public IReadOnlyList<string> GetConfiguredPackageSources(string term)
{
char c = term[i];

if (!currentNode.Children.ContainsKey(c))
if (!currentNode.Children.TryGetValue(c, out SearchNode? child))
{
if (!currentNode.IsGlobbing)
{
Expand All @@ -181,7 +181,7 @@ public IReadOnlyList<string> GetConfiguredPackageSources(string term)
break;
}

currentNode = currentNode.Children[c];
currentNode = child;

sb.Append(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2964,15 +2964,13 @@ internal async Task<IEnumerable<ResolvedAction>> PreviewBuildIntegratedProjectsA
}
else
{
if (!nugetProjectActionsLookup.ContainsKey(buildIntegratedProject.MSBuildProjectPath))
if (!nugetProjectActionsLookup.TryGetValue(buildIntegratedProject.MSBuildProjectPath, out nuGetProjectActions))
{
throw new ArgumentException(
message: string.Format(CultureInfo.CurrentCulture, Strings.UnableToFindPathInLookupOrList, nameof(nugetProjectActionsLookup), buildIntegratedProject.MSBuildProjectPath, nameof(packageIdentity), nameof(primarySources)),
paramName: nameof(nugetProjectActionsLookup));
}

nuGetProjectActions = nugetProjectActionsLookup[buildIntegratedProject.MSBuildProjectPath];

if (nuGetProjectActions.Length == 0)
{
// Continue to next project if there are no actions for current project.
Expand Down Expand Up @@ -3477,7 +3475,7 @@ await BuildIntegratedRestoreUtility.ExecuteInitPs1ScriptsAsync(
if (dgSpecForParents.Restore.Count > 0)
{
// Restore and commit the lock file to disk regardless of the result
// This will restore all parents in a single restore
// This will restore all parents in a single restore
await DependencyGraphRestoreUtility.RestoreAsync(
dgSpecForParents,
referenceContext,
Expand Down
Loading