Skip to content

Commit 5b36757

Browse files
Perf: Avoid unnecessary ProjectMetadataInstance allocations (#12599)
1 parent f4fbb16 commit 5b36757

2 files changed

Lines changed: 21 additions & 28 deletions

File tree

src/Build/Instance/ProjectItemDefinitionInstance.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,15 @@ string IMetadataTable.GetEscapedValue(string specifiedItemType, string name)
183183
/// <returns>The metadata value, or an null if none exists.</returns>
184184
string IMetadataTable.GetEscapedValueIfPresent(string specifiedItemType, string name)
185185
{
186-
if (specifiedItemType == null || String.Equals(_itemType, specifiedItemType, StringComparison.OrdinalIgnoreCase))
186+
if (_metadata == null)
187187
{
188-
ProjectMetadataInstance metadatum = GetMetadata(name);
189-
if (metadatum != null)
190-
{
191-
return metadatum.EvaluatedValueEscaped;
192-
}
188+
return null;
193189
}
194190

195-
return null;
191+
bool matchesItemType = specifiedItemType == null || String.Equals(_itemType, specifiedItemType, StringComparison.OrdinalIgnoreCase);
192+
return matchesItemType && _metadata.TryGetValue(name, out string value)
193+
? value
194+
: null;
196195
}
197196

198197
#endregion

src/Build/Instance/ProjectItemInstance.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,19 +1408,18 @@ public string GetMetadataEscaped(string metadataName)
14081408
return escapedValue;
14091409
}
14101410

1411-
ProjectMetadataInstance metadatum;
1412-
metadatum = GetItemDefinitionMetadata(metadataName);
1411+
escapedValue = GetItemDefinitionMetadataEscaped(metadataName);
14131412

1414-
if (metadatum != null && Expander<ProjectProperty, ProjectItem>.ExpressionMayContainExpandableExpressions(metadatum.EvaluatedValueEscaped))
1413+
if (escapedValue != null && Expander<ProjectProperty, ProjectItem>.ExpressionMayContainExpandableExpressions(escapedValue))
14151414
{
14161415
Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(null, null, new BuiltInMetadataTable(null, this), FileSystems.Default);
14171416

14181417
// We don't have a location to use, but this is very unlikely to error
1419-
return expander.ExpandIntoStringLeaveEscaped(metadatum.EvaluatedValueEscaped, ExpanderOptions.ExpandBuiltInMetadata, ElementLocation.EmptyLocation);
1418+
return expander.ExpandIntoStringLeaveEscaped(escapedValue, ExpanderOptions.ExpandBuiltInMetadata, ElementLocation.EmptyLocation);
14201419
}
1421-
else if (metadatum != null)
1420+
else if (escapedValue != null)
14221421
{
1423-
return metadatum.EvaluatedValueEscaped;
1422+
return escapedValue;
14241423
}
14251424

14261425
string value = GetBuiltInMetadataEscaped(metadataName);
@@ -1802,7 +1801,7 @@ public bool HasMetadata(string name)
18021801
{
18031802
if ((_directMetadata?.ContainsKey(name) == true) ||
18041803
FileUtilities.ItemSpecModifiers.IsItemSpecModifier(name) ||
1805-
GetItemDefinitionMetadata(name) != null)
1804+
GetItemDefinitionMetadataEscaped(name) != null)
18061805
{
18071806
return true;
18081807
}
@@ -1921,19 +1920,14 @@ internal void TranslateWithInterning(ITranslator translator, LookasideStringInte
19211920
/// </summary>
19221921
internal ProjectMetadataInstance GetMetadataObject(string name)
19231922
{
1924-
ProjectMetadataInstance value = null;
1925-
1926-
if (_directMetadata != null && _directMetadata.TryGetValue(name, out string escapedValue))
1927-
{
1928-
value = new ProjectMetadataInstance(name, escapedValue, allowItemSpecModifiers: true);
1929-
}
1930-
1931-
if (value == null)
1923+
if (_directMetadata == null || !_directMetadata.TryGetValue(name, out string escapedValue))
19321924
{
1933-
value = GetItemDefinitionMetadata(name);
1925+
escapedValue = GetItemDefinitionMetadataEscaped(name);
19341926
}
19351927

1936-
return value;
1928+
return escapedValue != null
1929+
? new ProjectMetadataInstance(name, escapedValue, allowItemSpecModifiers: true)
1930+
: null;
19371931
}
19381932

19391933
/// <summary>
@@ -2085,19 +2079,19 @@ private string GetBuiltInMetadataEscaped(string name)
20852079
/// Retrieves the named metadata from the item definition, if any.
20862080
/// If it is not present, returns null.
20872081
/// </summary>
2088-
private ProjectMetadataInstance GetItemDefinitionMetadata(string metadataName)
2082+
private string GetItemDefinitionMetadataEscaped(string metadataName)
20892083
{
20902084
// Check any inherited item definition metadata first. It's more like
20912085
// direct metadata, but we didn't want to copy the tables.
20922086
if (_itemDefinitions != null)
20932087
{
20942088
for (int i = 0; i < _itemDefinitions.Count; i++)
20952089
{
2096-
ProjectMetadataInstance metadataFromDefinition = _itemDefinitions[i].GetMetadata(metadataName);
2090+
string metadataValue = ((IMetadataTable)_itemDefinitions[i]).GetEscapedValueIfPresent(itemType: null, metadataName);
20972091

2098-
if (metadataFromDefinition != null)
2092+
if (metadataValue != null)
20992093
{
2100-
return metadataFromDefinition;
2094+
return metadataValue;
21012095
}
21022096
}
21032097
}

0 commit comments

Comments
 (0)