Assorted perf improvements

Renames and restore perf test
This commit is contained in:
Paulo Janotti
2018-08-12 09:42:01 -07:00
parent 6f44ca53fc
commit b9d5e98bd5
5 changed files with 46 additions and 17 deletions
+3 -1
View File
@@ -161,6 +161,8 @@ namespace Coverlet.Core
continue;
}
List<Document> documents = result.Documents.Values.ToList();
using (var fs = new FileStream(result.HitsFilePath, FileMode.Open))
using (var sr = new StreamReader(fs))
{
@@ -173,7 +175,7 @@ namespace Coverlet.Core
continue;
bool isBranch = info[0] == "B";
var document = result.Documents.ElementAt(int.Parse(info[1])).Value;
var document = documents[int.Parse(info[1])];
int start = int.Parse(info[2]);
int hits = int.Parse(info[4]);
@@ -175,17 +175,12 @@ namespace Coverlet.Core.Instrumentation
private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor processor, Instruction instruction, SequencePoint sequencePoint)
{
int documentIndex = 0;
if (!_result.Documents.TryGetValue(sequencePoint.Document.Url, out var document))
{
document = new Document { Path = sequencePoint.Document.Url };
documentIndex = _result.Documents.Count;
document.Index = _result.Documents.Count;
_result.Documents.Add(document.Path, document);
}
else
{
documentIndex = _result.Documents.Keys.ToList().IndexOf(document.Path);
}
for (int i = sequencePoint.StartLine; i <= sequencePoint.EndLine; i++)
{
@@ -193,7 +188,7 @@ namespace Coverlet.Core.Instrumentation
document.Lines.Add(i, new Line { Number = i, Class = method.DeclaringType.FullName, Method = method.FullName });
}
string marker = $"L,{documentIndex},{sequencePoint.StartLine},{sequencePoint.EndLine}";
string marker = $"L,{document.Index},{sequencePoint.StartLine},{sequencePoint.EndLine}";
var pathInstr = Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath);
var markInstr = Instruction.Create(OpCodes.Ldstr, marker);
@@ -208,17 +203,12 @@ namespace Coverlet.Core.Instrumentation
private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor processor, Instruction instruction, BranchPoint branchPoint)
{
int documentIndex = 0;
if (!_result.Documents.TryGetValue(branchPoint.Document, out var document))
{
document = new Document { Path = branchPoint.Document };
documentIndex = _result.Documents.Count;
document.Index = _result.Documents.Count;
_result.Documents.Add(document.Path, document);
}
else
{
documentIndex = _result.Documents.Keys.ToList().IndexOf(document.Path);
}
var key = (branchPoint.StartLine, (int)branchPoint.Ordinal);
if (!document.Branches.ContainsKey(key))
@@ -235,7 +225,7 @@ namespace Coverlet.Core.Instrumentation
}
);
string marker = $"B,{documentIndex},{branchPoint.StartLine},{branchPoint.Ordinal}";
string marker = $"B,{document.Index},{branchPoint.StartLine},{branchPoint.Ordinal}";
var pathInstr = Instruction.Create(OpCodes.Ldstr, _result.HitsFilePath);
var markInstr = Instruction.Create(OpCodes.Ldstr, marker);
@@ -27,6 +27,7 @@ namespace Coverlet.Core.Instrumentation
}
public string Path;
public int Index;
public Dictionary<int, Line> Lines { get; private set; }
public Dictionary<(int Line, int Ordinal), Branch> Branches { get; private set; }
+3 -2
View File
@@ -8,6 +8,7 @@ namespace Coverlet.Tracker
public static class CoverageTracker
{
private static List<Dictionary<string, Dictionary<string, int>>> _events;
private static readonly StringHashSuffixComparer _stringHashSuffixComparer = new StringHashSuffixComparer();
[ThreadStatic]
private static Dictionary<string, Dictionary<string, int>> t_events;
@@ -25,7 +26,7 @@ namespace Coverlet.Tracker
{
if (t_events == null)
{
t_events = new Dictionary<string, Dictionary<string, int>>();
t_events = new Dictionary<string, Dictionary<string, int>>(_stringHashSuffixComparer);
lock (_events)
{
_events.Add(t_events);
@@ -38,7 +39,7 @@ namespace Coverlet.Tracker
{
if (!t_events.TryGetValue(file, out var fileEvents))
{
fileEvents = new Dictionary<string, int>();
fileEvents = new Dictionary<string, int>(_stringHashSuffixComparer);
t_events.Add(file, fileEvents);
}
@@ -0,0 +1,35 @@
using System.Collections.Generic;
namespace Coverlet.Tracker
{
internal class StringHashSuffixComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y) => string.Equals(x, y);
public int GetHashCode(string s)
{
if (s == null || s.Length == 0)
return 0;
// Hash calculation based on the old implementation of NameTable used in System.Xml
const int SuffixLength = 8;
const int Seed = 1031880390;
int hashCode;
unchecked
{
hashCode = s.Length + Seed;
int i = s.Length > SuffixLength ? s.Length - SuffixLength : 0;
for (; i<s.Length; ++i)
{
hashCode += (hashCode << 7) ^ s[i];
}
hashCode -= hashCode >> 17;
hashCode -= hashCode >> 11;
hashCode -= hashCode >> 5;
}
return hashCode;
}
}
}