Skip to content

Commit

Permalink
Optimize jit-analyze (dotnet#306)
Browse files Browse the repository at this point in the history
* Use parallel

* Use .AsParallel()

* Just use .AsParallel()

* Convert showTextDiff to skipTextDiff

* Update src/jit-analyze/jit-analyze.cs

Co-authored-by: Andy Ayers <andya@microsoft.com>

Co-authored-by: Andy Ayers <andya@microsoft.com>
  • Loading branch information
kunalspathak and AndyAyersMS authored Dec 4, 2020
1 parent a9a332b commit bd80267
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/jit-analyze/jit-analyze.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Config
private string _note;
private string _filter;
private string _metric;
private bool _skipTextDiff = false;

public Config(string[] args)
{
Expand Down Expand Up @@ -81,6 +82,8 @@ public Config(string[] args)
"Dump analysis data to specified file in tab-separated format.");
syntax.DefineOption("filter", ref _filter,
"Only consider assembly files whose names match the filter");
syntax.DefineOption("skiptextdiff", ref _skipTextDiff,
"Skip analysis that checks for files that have textual diffs but no metric diffs.");
});

// Run validation code on parsed input to ensure we have a sensible scenario.
Expand Down Expand Up @@ -127,6 +130,7 @@ private void validate()
public string Filter { get { return _filter; } }

public string Metric { get { return _metric; } }
public bool SkipTextDiff { get { return _skipTextDiff; } }
}

public class FileInfo
Expand Down Expand Up @@ -505,7 +509,7 @@ public static IEnumerable<FileInfo> ExtractFileInfo(string path, string filter,
string fileNamePattern = filter ?? "*";
string searchPattern = fileNamePattern + fileExtension;
return Directory.EnumerateFiles(fullRootPath, searchPattern, searchOption)
.Select(p => new FileInfo
.AsParallel().Select(p => new FileInfo
{
path = p.Substring(fullRootPath.Length).TrimStart(Path.DirectorySeparatorChar),
methodList = ExtractMethodInfo(p)
Expand Down Expand Up @@ -650,7 +654,7 @@ public static IEnumerable<FileDelta> Comparator(IEnumerable<FileInfo> baseInfo,
// Top diffs by size across all files
// Top diffs by percentage size across all files
//
public static int Summarize(IEnumerable<FileDelta> fileDeltaList, Config config, Dictionary<string, int> diffCounts)
public static int Summarize(IEnumerable<FileDelta> fileDeltaList, Config config)
{
var totalDeltaMetrics = fileDeltaList.Sum(x => x.deltaMetrics);
var totalBaseMetrics = fileDeltaList.Sum(x => x.baseMetrics);
Expand Down Expand Up @@ -807,18 +811,24 @@ void DisplayMethodMetric(string headerText, string subtext, int methodCount, dyn
Console.WriteLine("\n{0} total methods with {1} differences ({2} improved, {3} regressed), {4} unchanged.",
sortedMethodCount, metricName, methodImprovementCount, methodRegressionCount, unchangedMethodCount);

// Show files with text diffs but no metric diffs.
// TODO: resolve diffs to particular methods in the files.
var zeroDiffFilesWithDiffs = fileDeltaList.Where(x => diffCounts.ContainsKey(x.diffPath) && (x.deltaMetrics.IsZero()))
.OrderByDescending(x => diffCounts[x.basePath]);

int zeroDiffFilesWithDiffCount = zeroDiffFilesWithDiffs.Count();
if (zeroDiffFilesWithDiffCount > 0)
if (!config.SkipTextDiff)
{
Console.WriteLine("\n{0} files had text diffs but no metric diffs.", zeroDiffFilesWithDiffCount);
foreach (var zerofile in zeroDiffFilesWithDiffs.Take(config.Count))
// Show files with text diffs but no metric diffs.

Dictionary<string, int> diffCounts = DiffInText(config.DiffPath, config.BasePath);

// TODO: resolve diffs to particular methods in the files.
var zeroDiffFilesWithDiffs = fileDeltaList.Where(x => diffCounts.ContainsKey(x.diffPath) && (x.deltaMetrics.IsZero()))
.OrderByDescending(x => diffCounts[x.basePath]);

int zeroDiffFilesWithDiffCount = zeroDiffFilesWithDiffs.Count();
if (zeroDiffFilesWithDiffCount > 0)
{
Console.WriteLine($"{zerofile.basePath} had {diffCounts[zerofile.basePath]} diffs");
Console.WriteLine("\n{0} files had text diffs but no metric diffs.", zeroDiffFilesWithDiffCount);
foreach (var zerofile in zeroDiffFilesWithDiffs.Take(config.Count))
{
Console.WriteLine($"{zerofile.basePath} had {diffCounts[zerofile.basePath]} diffs");
}
}
}

Expand Down Expand Up @@ -974,12 +984,11 @@ public static Dictionary<string, int> DiffInText(string diffPath, string basePat
commandArgs.Add(diffPath);

ProcessResult result = Utility.ExecuteProcess("git", commandArgs, true);
Dictionary<string, int> fileToTextDiffCount = null;
Dictionary<string, int> fileToTextDiffCount = new Dictionary<string, int>(); ;

if (result.ExitCode != 0)
{
// There are files with diffs. Build up a dictionary mapping base file name to net text diff count.
fileToTextDiffCount = new Dictionary<string, int>();

var rawLines = result.StdOut.Split(new[] { "\0", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (rawLines.Length % 3 != 0)
Expand Down Expand Up @@ -1047,15 +1056,6 @@ public static int Main(string[] args)
// Parse incoming arguments
Config config = new Config(args);

Dictionary<string, int> diffCounts = DiffInText(config.DiffPath, config.BasePath);

// Early out if no textual diffs found.
if (diffCounts == null)
{
Console.WriteLine("No diffs found.");
return 0;
}

try
{
// Extract method info from base and diff directory or file.
Expand Down Expand Up @@ -1086,8 +1086,7 @@ public static int Main(string[] args)
GenerateJson(compareList, config.JsonFileName);
}

return Summarize(compareList, config, diffCounts);

return Summarize(compareList, config);
}
catch (System.IO.DirectoryNotFoundException e)
{
Expand Down

0 comments on commit bd80267

Please sign in to comment.