Skip to content

Commit

Permalink
Merge pull request #821 from stephentoub/lowhangingalloc
Browse files Browse the repository at this point in the history
Remove some low-hanging fruit allocation
  • Loading branch information
KirillOsenkov authored Sep 17, 2024
2 parents 9719f61 + 25d6f70 commit 53fb2ee
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 112 deletions.
10 changes: 5 additions & 5 deletions src/BinlogTool/ListTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private string GetSourceCommitId(Build build)
return null;
}

var sourceCommitId = environment.FindChild<Property>(p => p.Name == "SOURCECOMMITID");
var sourceCommitId = environment.FindChild<Property>(static p => p.Name == "SOURCECOMMITID");
if (sourceCommitId != null)
{
return sourceCommitId.Value;
Expand Down Expand Up @@ -83,15 +83,15 @@ private string GetToolInfo(Task task)

if (task.Name == "Exec")
{
var args = task.FindChild<Property>(p => p.Name == "CommandLineArguments");
var args = task.FindChild<Property>(static p => p.Name == "CommandLineArguments");
string arguments = null;

if (args == null)
{
var parameters = task.FindChild<Folder>("Parameters");
if (parameters != null)
{
var command = parameters.FindChild<Property>(p => p.Name == "Command");
var command = parameters.FindChild<Property>(static p => p.Name == "Command");
if (command != null)
{
arguments = command.Value;
Expand Down Expand Up @@ -124,7 +124,7 @@ private string GetToolInfo(Task task)
return null;
}

var assembly = task.FindChild<Property>(p => p.Name == "Assembly");
var assembly = task.FindChild<Property>(static p => p.Name == "Assembly");
if (assembly == null)
{
return null;
Expand All @@ -141,7 +141,7 @@ private string GetToolInfo(Task task)
return null;
}

var versionMessage = task.FindChild<Message>(m => m.Text is string message &&
var versionMessage = task.FindChild<Message>(static m => m.Text is string message &&
message.Length < 200 &&
!message.Contains("\n") &&
!message.Contains("Leaving it untouched") &&
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredLogViewer/Controls/BuildControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,7 @@ public void DisplayStats()
return;
}

var statsRoot = Build.FindChild<Folder>(f => f.Name.StartsWith(Strings.Statistics));
var statsRoot = Build.FindChild<Folder>(static f => f.Name.StartsWith(Strings.Statistics));
if (statsRoot != null)
{
return;
Expand Down Expand Up @@ -2778,9 +2778,9 @@ private UIElement GetHistogram(List<int> values)

private void DisplayTreeStats(Folder statsRoot, BuildStatistics treeStats, BinlogStats recordStats)
{
var buildMessageNode = statsRoot.FindChild<Folder>(n => n.Name.StartsWith("BuildMessage", StringComparison.Ordinal));
var taskInputsNode = buildMessageNode.FindChild<Folder>(n => n.Name.StartsWith("Task Input", StringComparison.Ordinal));
var taskOutputsNode = buildMessageNode.FindChild<Folder>(n => n.Name.StartsWith("Task Output", StringComparison.Ordinal));
var buildMessageNode = statsRoot.FindChild<Folder>(static n => n.Name.StartsWith("BuildMessage", StringComparison.Ordinal));
var taskInputsNode = buildMessageNode.FindChild<Folder>(static n => n.Name.StartsWith("Task Input", StringComparison.Ordinal));
var taskOutputsNode = buildMessageNode.FindChild<Folder>(static n => n.Name.StartsWith("Task Output", StringComparison.Ordinal));

AddTopTasks(treeStats.TaskParameterMessagesByTask, taskInputsNode);
AddTopTasks(treeStats.OutputItemMessagesByTask, taskOutputsNode);
Expand Down
12 changes: 6 additions & 6 deletions src/StructuredLogger/Analyzers/FileCopyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private void TryExplainSingleFileCopy(Project project, string filePath, IList<Se
var task = target.FindChild<MSBuildTask>();
if (task != null)
{
var outputItems = task.FindLastChild<Folder>(f => f.Name == "OutputItems");
var outputItems = task.FindLastChild<Folder>(static f => f.Name == "OutputItems");
if (outputItems != null)
{
var addItem = outputItems.FindChild<AddItem>();
Expand All @@ -265,13 +265,13 @@ private void TryExplainSingleFileCopy(Project project, string filePath, IList<Se
var item = addItem.FindChild<Item>(filePath);
if (item != null)
{
var metadata = item.FindChild<Metadata>(m => m.Name == "MSBuildSourceProjectFile");
var metadata = item.FindChild<Metadata>(static m => m.Name == "MSBuildSourceProjectFile");
if (metadata != null)
{
var metadataValue = metadata.Value;
resultSet.Add(new SearchResult(metadata));

var referencedProject = task.FindChild<Project>(p => p.Name.Equals(Path.GetFileName(metadataValue), StringComparison.OrdinalIgnoreCase));
var referencedProject = task.FindChild<Project, string>(static (p, metadataValue) => p.Name.Equals(Path.GetFileName(metadataValue), StringComparison.OrdinalIgnoreCase), metadataValue);
if (referencedProject != null)
{
var getCopyToOutputDirectoryItems = referencedProject.FindTarget("GetCopyToOutputDirectoryItems");
Expand Down Expand Up @@ -299,16 +299,16 @@ private void TryExplainSingleFileCopy(Project project, string filePath, IList<Se
var task = target.FindChild<ResolveAssemblyReferenceTask>();
if (task != null)
{
var outputItems = task.FindLastChild<Folder>(f => f.Name == "OutputItems");
var outputItems = task.FindLastChild<Folder>(static f => f.Name == "OutputItems");
if (outputItems != null)
{
var addItem = outputItems.FindChild<AddItem>(a => a.Name == "ReferenceCopyLocalPaths");
var addItem = outputItems.FindChild<AddItem>(static a => a.Name == "ReferenceCopyLocalPaths");
if (addItem != null)
{
var item = addItem.FindChild<Item>(filePath);
if (item != null)
{
var metadata = item.FindChild<Metadata>(m => m.Name == "MSBuildSourceProjectFile");
var metadata = item.FindChild<Metadata>(static m => m.Name == "MSBuildSourceProjectFile");
if (metadata != null)
{
var metadataValue = metadata.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public void AnalyzeResolveAssemblyReference(Task rar)

currentUsedLocations.Clear();

var results = rar.FindChild<Folder>(c => c.Name == Strings.Results);
var parameters = rar.FindChild<Folder>(c => c.Name == Strings.Parameters);
var results = rar.FindChild<Folder>(static c => c.Name == Strings.Results);
var parameters = rar.FindChild<Folder>(static c => c.Name == Strings.Parameters);

TotalRARDuration += rar.Duration;

IList<string> searchPaths = null;
if (parameters != null)
{
var searchPathsNode = parameters.FindChild<NamedNode>(c => c.Name == Strings.SearchPaths);
var searchPathsNode = parameters.FindChild<NamedNode>(static c => c.Name == Strings.SearchPaths);
if (searchPathsNode != null)
{
searchPaths = searchPathsNode.Children.Select(c => c.ToString()).ToArray();
Expand All @@ -46,15 +46,15 @@ public void AnalyzeResolveAssemblyReference(Task rar)
{
const string ResolvedFilePathIs = "Resolved file path is \"";
string resolvedFilePath = null;
var resolvedFilePathNode = reference.FindChild<Item>(i => i.ToString().StartsWith(ResolvedFilePathIs, StringComparison.Ordinal));
var resolvedFilePathNode = reference.FindChild<Item>(static i => i.ToString().StartsWith(ResolvedFilePathIs, StringComparison.Ordinal));
if (resolvedFilePathNode != null)
{
var text = resolvedFilePathNode.ToString();
resolvedFilePath = text.Substring(ResolvedFilePathIs.Length, text.Length - ResolvedFilePathIs.Length - 2);
}

const string ReferenceFoundAt = "Reference found at search path location \"";
var foundAtLocation = reference.FindChild<Item>(i => i.ToString().StartsWith(ReferenceFoundAt, StringComparison.Ordinal));
var foundAtLocation = reference.FindChild<Item>(static i => i.ToString().StartsWith(ReferenceFoundAt, StringComparison.Ordinal));
if (foundAtLocation != null)
{
var text = foundAtLocation.ToString();
Expand Down
21 changes: 18 additions & 3 deletions src/StructuredLogger/BinaryLogger/BuildEventArgsReader.Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ private struct NameValueRecord
public IDictionary<string, string> Dictionary;
}

private IDictionary<string, string> CreateDictionary(List<(int keyIndex, int valueIndex)> list)
private ArrayDictionary<string, string> CreateDictionary(List<(int keyIndex, int valueIndex)> list)
{
var dictionary = new ArrayDictionary<string, string>(list.Count);
for (int i = 0; i < list.Count; i++)
{
string key = GetStringFromRecord(list[i].keyIndex);
string value = GetStringFromRecord(list[i].valueIndex);
if (key != null)
{
string value = GetStringFromRecord(list[i].valueIndex);
dictionary.Add(key, value);
}
}
Expand Down Expand Up @@ -193,9 +193,24 @@ private string GetTaskFinishedMessage(bool succeeded, string taskName)
return FormatResourceStringIgnoreCodeAndKeyword(succeeded ? "Done executing task \"{0}\"." : "Done executing task \"{0}\" -- FAILED.", taskName);
}

internal static string FormatResourceStringIgnoreCodeAndKeyword(string resource, string arg0)
{
return string.Format(resource, arg0);
}

internal static string FormatResourceStringIgnoreCodeAndKeyword(string resource, string arg0, string arg1)
{
return string.Format(resource, arg0, arg1);
}

internal static string FormatResourceStringIgnoreCodeAndKeyword(string resource, string arg0, string arg1, string arg2)
{
return string.Format(resource, arg0, arg1, arg2);
}

internal static string FormatResourceStringIgnoreCodeAndKeyword(string resource, params string[] arguments)
{
return string.Format(resource, arguments);
}
}
}
}
23 changes: 14 additions & 9 deletions src/StructuredLogger/BinaryLogger/BuildEventArgsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ e is FormatException ||
{
hasError = true;

int localSerializedEventLength = serializedEventLength;
Exception localException = e;
string ErrorFactory() =>
string.Format("BuildEvent record number {0} (serialized size: {1}) attempted to perform disallowed reads (details: {2}: {3}).",
_recordNumber, serializedEventLength, e.GetType(), e.Message) + (_skipUnknownEvents
_recordNumber, localSerializedEventLength, localException.GetType(), localException.Message) + (_skipUnknownEvents
? " Skipping the record."
: string.Empty);

Expand All @@ -263,9 +265,11 @@ string ErrorFactory() =>

if (result == null && !hasError)
{
int localSerializedEventLength = serializedEventLength;
BinaryLogRecordKind localRecordKind = recordKind;
string ErrorFactory() =>
string.Format("BuildEvent record number {0} (serialized size: {1}) is of unsupported type: {2}.",
_recordNumber, serializedEventLength, recordKind) + (_skipUnknownEvents
_recordNumber, localSerializedEventLength, localRecordKind) + (_skipUnknownEvents
? " Skipping the record."
: string.Empty);

Expand All @@ -274,9 +278,10 @@ string ErrorFactory() =>

if (_readStream.BytesCountAllowedToReadRemaining > 0)
{
int localSerializedEventLength = serializedEventLength;
string ErrorFactory() => string.Format(
"BuildEvent record number {0} was expected to read exactly {1} bytes from the stream, but read {2} instead.", _recordNumber, serializedEventLength,
serializedEventLength - _readStream.BytesCountAllowedToReadRemaining);
"BuildEvent record number {0} was expected to read exactly {1} bytes from the stream, but read {2} instead.", _recordNumber, localSerializedEventLength,
localSerializedEventLength - _readStream.BytesCountAllowedToReadRemaining);

HandleError(ErrorFactory, _skipUnknownEventParts, ReaderErrorType.UnknownEventData, recordKind);
}
Expand Down Expand Up @@ -1616,7 +1621,7 @@ private void SetCommonFields(BuildEventArgs buildEventArgs, BuildEventArgsFields
}
}

private IEnumerable ReadPropertyList()
private IDictionary<string, string> ReadPropertyList()
{
var properties = ReadStringDictionary();
return properties;
Expand Down Expand Up @@ -1666,7 +1671,7 @@ private IDictionary<string, string> ReadStringDictionary()
return record;
}

private IDictionary<string, string> ReadLegacyStringDictionary()
private Dictionary<string, string> ReadLegacyStringDictionary()
{
int count = ReadInt32();
if (count == 0)
Expand Down Expand Up @@ -1695,7 +1700,7 @@ private ITaskItem ReadTaskItem()
return taskItem;
}

private IEnumerable ReadProjectItems()
private IList<DictionaryEntry> ReadProjectItems()
{
IList<DictionaryEntry> list;

Expand Down Expand Up @@ -1777,7 +1782,7 @@ private IEnumerable ReadProjectItems()
return list;
}

private IEnumerable<string> ReadStringList()
private string[] ReadStringList()
{
var count = ReadInt32();

Expand All @@ -1790,7 +1795,7 @@ private IEnumerable<string> ReadStringList()
return list;
}

private IEnumerable ReadTaskItemList()
private ITaskItem[] ReadTaskItemList()
{
int count = ReadInt32();
if (count == 0)
Expand Down
8 changes: 4 additions & 4 deletions src/StructuredLogger/Construction/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void TargetSkipped(TargetSkippedEventArgs args)
var project = GetProject(args.BuildEventContext.ProjectContextId);
if (project != null && args.BuildEventContext.TargetId != BuildEventContext.InvalidTargetId)
{
target = project.FindLastChild<Target>(t => t.Id == args.BuildEventContext.TargetId);
target = project.FindLastChild<Target, TargetSkippedEventArgs>(static (t, args) => t.Id == args.BuildEventContext.TargetId, args);
}

if (target == null)
Expand Down Expand Up @@ -523,7 +523,7 @@ public void StatusEventRaised(object sender, BuildStatusEventArgs e)
projectEvaluation.ProjectFile = projectFilePath;

projectEvaluation.Id = evaluationId;
projectEvaluation.EvaluationText = Intern("id:" + evaluationId);
projectEvaluation.EvaluationText = Intern($"id:{evaluationId}");
projectEvaluation.NodeId = e.BuildEventContext.NodeId;
projectEvaluation.StartTime = e.Timestamp;
projectEvaluation.EndTime = e.Timestamp;
Expand All @@ -534,7 +534,7 @@ public void StatusEventRaised(object sender, BuildStatusEventArgs e)
var projectFilePath = Intern(projectEvaluationFinished.ProjectFile);
var projectName = Intern(Path.GetFileName(projectFilePath));
var nodeName = Intern(GetEvaluationProjectName(evaluationId, projectName));
var projectEvaluation = EvaluationFolder.FindLastChild<ProjectEvaluation>(e => e.Id == evaluationId);
var projectEvaluation = EvaluationFolder.FindLastChild<ProjectEvaluation, int>(static (e, evaluationId) => e.Id == evaluationId, evaluationId);
if (projectEvaluation == null)
{
// no matching ProjectEvaluationStarted
Expand Down Expand Up @@ -728,7 +728,7 @@ private TreeNode FindParent(BuildEventContext buildEventContext)

result = EvaluationFolder;

var projectEvaluation = result.FindChild<ProjectEvaluation>(p => p.Id == evaluationId);
var projectEvaluation = result.FindChild<ProjectEvaluation, int>(static (p, evaluationId) => p.Id == evaluationId, evaluationId);
if (projectEvaluation != null)
{
result = projectEvaluation;
Expand Down
2 changes: 1 addition & 1 deletion src/StructuredLogger/ObjectModel/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public ProjectEvaluation FindEvaluation(int id)
}

// the evaluation we want is likely to be at the end (recently added)
projectEvaluation = evaluation.FindLastChild<ProjectEvaluation>(e => e.Id == id);
projectEvaluation = evaluation.FindLastChild<ProjectEvaluation, int>(static (e, id) => e.Id == id, id);
if (projectEvaluation != null)
{
evaluationById[id] = projectEvaluation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CompilationWrites(

internal static CompilationWrites? TryParse(Task task)
{
var parameters = task.FindChild<Folder>(c => c.Name == Strings.Parameters);
var parameters = task.FindChild<Folder>(static c => c.Name == Strings.Parameters);
if (parameters == null)
{
// Probably localized MSBuild that we don't yet support
Expand Down
Loading

0 comments on commit 53fb2ee

Please sign in to comment.