Skip to content

Commit

Permalink
Remove some duplicate dictionary lookups (#6168)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 4, 2024
1 parent b9b70a8 commit 589cf2f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
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

0 comments on commit 589cf2f

Please sign in to comment.