Skip to content

Commit d9fff6b

Browse files
committed
Fix a performance regression.
Add test.
1 parent 7c99b6f commit d9fff6b

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

NetDiff.Test/Test.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,66 @@ public void SpecifiedComparer()
556556
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(4).Status);
557557
}
558558

559+
[TestMethod]
560+
public void CaseReplace()
561+
{
562+
string str1 = "abbbc";
563+
string str2 = "adbbc";
564+
565+
var option = DiffOption<char>.Default;
566+
option.Order = DiffOrder.GreedyDeleteFirst;
567+
568+
var results = DiffUtil.Diff(str1, str2, option);
569+
570+
Assert.AreEqual(DiffStatus.Equal, results.ElementAt(0).Status);
571+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status);
572+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(2).Status);
573+
Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status);
574+
Assert.AreEqual(DiffStatus.Equal, results.ElementAt(4).Status);
575+
Assert.AreEqual(DiffStatus.Equal, results.ElementAt(5).Status);
576+
}
577+
578+
[TestMethod]
579+
public void GivenEmpty()
580+
{
581+
string str1 = "";
582+
string str2 = "";
583+
584+
var results = DiffUtil.Diff(str1, str2);
585+
586+
Assert.IsTrue(!results.Any());
587+
}
588+
589+
[TestMethod]
590+
public void GivenSeq1Empty()
591+
{
592+
string str1 = "";
593+
string str2 = "abcde";
594+
595+
var results = DiffUtil.Diff(str1, str2);
596+
597+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status);
598+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(1).Status);
599+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(2).Status);
600+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(3).Status);
601+
Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(4).Status);
602+
}
603+
604+
[TestMethod]
605+
public void GivenSeq2Empty()
606+
{
607+
string str1 = "abced";
608+
string str2 = "";
609+
610+
var results = DiffUtil.Diff(str1, str2);
611+
612+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(0).Status);
613+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status);
614+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(2).Status);
615+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(3).Status);
616+
Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(4).Status);
617+
}
618+
559619
[TestMethod]
560620
public void Performance()
561621
{
@@ -579,6 +639,23 @@ public void Performance()
579639
Assert.IsTrue(time2 < time1);
580640
}
581641

642+
[TestMethod]
643+
public void Performance2()
644+
{
645+
var str1 = Enumerable.Repeat("good dog", 1000000).SelectMany(c => c);
646+
var str2 = Enumerable.Repeat("Bad dog", 1000000).SelectMany(c => c);
647+
648+
var option = DiffOption<char>.Default;
649+
option.Limit = 100;
650+
var sw = new System.Diagnostics.Stopwatch();
651+
sw.Start();
652+
var result1 = DiffUtil.Diff(str1, str2, option);
653+
sw.Stop();
654+
var time1 = sw.Elapsed.TotalSeconds;
655+
656+
System.Console.WriteLine(sw.Elapsed.TotalSeconds);
657+
}
658+
582659
[TestMethod]
583660
public void CaseEmpty()
584661
{

NetDiff/EditGraph.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ public EditGraph(
9393

9494
public List<Point> CalculatePath(DiffOption<T> option)
9595
{
96+
if (!seq1.Any())
97+
return Enumerable.Range(0, seq2.Length + 1).Select(i => new Point(0, i)).ToList();
98+
99+
if (!seq2.Any())
100+
return Enumerable.Range(0, seq1.Length + 1).Select(i => new Point(i, 0)).ToList();
101+
96102
this.option = option;
97103

98104
BeginCalculatePath();
@@ -152,7 +158,7 @@ private void UpdateHeads()
152158
if (option.Limit > 0 && heads.Count > option.Limit)
153159
{
154160
var selectedNode = SelectNode(heads);
155-
Initialize();
161+
heads.Clear();
156162
heads.Add(selectedNode);
157163
}
158164

0 commit comments

Comments
 (0)