Skip to content
Draft
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
12 changes: 5 additions & 7 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,18 +1280,16 @@ private void EnsureParameterInitialized(TaskPropertyInfo parameter, Lookup looku

parameter.Initialized = true;

// PERF: Be careful to avoid unnecessary string allocations. Appending '_taskName + "_" + parameter.Name' happens in both paths,
// but we don't want to allocate the string if we don't need to.
string key = "DisableLogTaskParameter_" + _taskName + "_" + parameter.Name;

if (string.Equals(lookup.GetProperty(key)?.EvaluatedValue, "true", StringComparison.OrdinalIgnoreCase))
// Use the precomputed lookup keys from the TaskFactoryWrapper to avoid allocations on the hot path
string key = _taskFactoryWrapper.GetDisableLogTaskParameterKey(parameter.Name);
if (key != null && string.Equals(lookup.GetProperty(key)?.EvaluatedValue, "true", StringComparison.OrdinalIgnoreCase))
{
parameter.Log = false;
}
else
{
string metadataKey = "DisableLogTaskParameterItemMetadata_" + _taskName + "_" + parameter.Name;
if (string.Equals(lookup.GetProperty(metadataKey)?.EvaluatedValue, "true", StringComparison.OrdinalIgnoreCase))
string metadataKey = _taskFactoryWrapper.GetDisableLogTaskParameterItemMetadataKey(parameter.Name);
if (metadataKey != null && string.Equals(lookup.GetProperty(metadataKey)?.EvaluatedValue, "true", StringComparison.OrdinalIgnoreCase))
{
parameter.LogItemMetadata = false;
}
Expand Down
57 changes: 55 additions & 2 deletions src/Build/Instance/TaskFactoryWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,32 @@ private struct PropertyData
/// </summary>
public readonly IReadOnlyDictionary<string, TaskPropertyInfo> PropertyInfoCache;

/// <summary>
/// Cache of precomputed DisableLogTaskParameter property keys for each parameter.
/// Maps parameter name to "DisableLogTaskParameter_{taskName}_{parameterName}"
/// </summary>
public readonly IReadOnlyDictionary<string, string> DisableLogTaskParameterKeys;

/// <summary>
/// Cache of precomputed DisableLogTaskParameterItemMetadata property keys for each parameter.
/// Maps parameter name to "DisableLogTaskParameterItemMetadata_{taskName}_{parameterName}"
/// </summary>
public readonly IReadOnlyDictionary<string, string> DisableLogTaskParameterItemMetadataKeys;

public PropertyData(
IReadOnlyDictionary<string, string> namesOfPropertiesWithRequiredAttribute,
IReadOnlyDictionary<string, string> namesOfPropertiesWithOutputAttribute,
IReadOnlyDictionary<string, string> namesOfPropertiesWithAmbiguousMatches,
IReadOnlyDictionary<string, TaskPropertyInfo> propertyInfoCache)
IReadOnlyDictionary<string, TaskPropertyInfo> propertyInfoCache,
IReadOnlyDictionary<string, string> disableLogTaskParameterKeys,
IReadOnlyDictionary<string, string> disableLogTaskParameterItemMetadataKeys)
{
NamesOfPropertiesWithRequiredAttribute = namesOfPropertiesWithRequiredAttribute;
NamesOfPropertiesWithOutputAttribute = namesOfPropertiesWithOutputAttribute;
NamesOfPropertiesWithAmbiguousMatches = namesOfPropertiesWithAmbiguousMatches;
PropertyInfoCache = propertyInfoCache;
DisableLogTaskParameterKeys = disableLogTaskParameterKeys;
DisableLogTaskParameterItemMetadataKeys = disableLogTaskParameterItemMetadataKeys;
}
}

Expand Down Expand Up @@ -168,6 +184,26 @@ public string Name
/// </summary>
public TaskHostParameters FactoryIdentityParameters => _factoryIdentityParameters;

/// <summary>
/// Gets the precomputed DisableLogTaskParameter property key for a parameter.
/// Returns null if the parameter name is not found.
/// </summary>
internal string? GetDisableLogTaskParameterKey(string parameterName)
{
_propertyData.Value.DisableLogTaskParameterKeys.TryGetValue(parameterName, out string? key);
return key;
}

/// <summary>
/// Gets the precomputed DisableLogTaskParameterItemMetadata property key for a parameter.
/// Returns null if the parameter name is not found.
/// </summary>
internal string? GetDisableLogTaskParameterItemMetadataKey(string parameterName)
{
_propertyData.Value.DisableLogTaskParameterItemMetadataKeys.TryGetValue(parameterName, out string? key);
return key;
}

#endregion

#region Methods.
Expand Down Expand Up @@ -265,6 +301,8 @@ private PropertyData PopulatePropertyInfo()
Dictionary<string, string>? namesOfPropertiesWithRequiredAttribute = null;
Dictionary<string, string>? namesOfPropertiesWithOutputAttribute = null;
Dictionary<string, string>? namesOfPropertiesWithAmbiguousMatches = null;
Dictionary<string, string>? disableLogTaskParameterKeys = null;
Dictionary<string, string>? disableLogTaskParameterItemMetadataKeys = null;

bool taskTypeImplementsIGeneratedTask = typeof(IGeneratedTask).IsAssignableFrom(_taskFactory.TaskType);
TaskPropertyInfo[] propertyInfos = _taskFactory.GetTaskParameters();
Expand Down Expand Up @@ -325,13 +363,28 @@ private PropertyData PopulatePropertyInfo()
// we have a output attribute defined, keep a record of that
namesOfPropertiesWithOutputAttribute[propertyInfo.Name] = String.Empty;
}

// Precompute the DisableLogTaskParameter lookup keys to avoid allocations in the hot path
if (disableLogTaskParameterKeys == null)
{
disableLogTaskParameterKeys = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
disableLogTaskParameterKeys[propertyInfo.Name] = "DisableLogTaskParameter_" + _taskName + "_" + propertyInfo.Name;

if (disableLogTaskParameterItemMetadataKeys == null)
{
disableLogTaskParameterItemMetadataKeys = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
disableLogTaskParameterItemMetadataKeys[propertyInfo.Name] = "DisableLogTaskParameterItemMetadata_" + _taskName + "_" + propertyInfo.Name;
}

return new PropertyData(
(IReadOnlyDictionary<string, string>?)namesOfPropertiesWithRequiredAttribute ?? ReadOnlyEmptyDictionary<string, string>.Instance,
(IReadOnlyDictionary<string, string>?)namesOfPropertiesWithOutputAttribute ?? ReadOnlyEmptyDictionary<string, string>.Instance,
(IReadOnlyDictionary<string, string>?)namesOfPropertiesWithAmbiguousMatches ?? ReadOnlyEmptyDictionary<string, string>.Instance,
(IReadOnlyDictionary<string, TaskPropertyInfo>?)propertyInfoCache ?? ReadOnlyEmptyDictionary<string, TaskPropertyInfo>.Instance);
(IReadOnlyDictionary<string, TaskPropertyInfo>?)propertyInfoCache ?? ReadOnlyEmptyDictionary<string, TaskPropertyInfo>.Instance,
(IReadOnlyDictionary<string, string>?)disableLogTaskParameterKeys ?? ReadOnlyEmptyDictionary<string, string>.Instance,
(IReadOnlyDictionary<string, string>?)disableLogTaskParameterItemMetadataKeys ?? ReadOnlyEmptyDictionary<string, string>.Instance);
}
#endregion
}
Expand Down
Loading