Skip to content

Commit 258a5e0

Browse files
committed
Remove more nested appends
Nested appends generally lead to unnecessary allocation and copying. Unwind them in diffCompute. Replace them in DiffCleanupSemantics with splice. DiffHalfMatch-8 107µs ± 0% 108µs ± 1% +0.70% (p=0.000 n=10+10) DiffCleanupSemantic-8 1.00ms ± 2% 0.97ms ± 0% -2.71% (p=0.000 n=10+8) DiffMain-8 1.01s ± 0% 1.01s ± 0% ~ (p=0.236 n=8+9) DiffMainLarge-8 110ms ± 1% 110ms ± 1% ~ (p=1.000 n=9+8) DiffMainRunesLargeLines-8 692µs ± 1% 693µs ± 1% ~ (p=0.762 n=10+8) name old alloc/op new alloc/op delta DiffHalfMatch-8 106kB ± 0% 106kB ± 0% ~ (all equal) DiffCleanupSemantic-8 255kB ± 0% 177kB ± 0% -30.76% (p=0.000 n=9+10) DiffMain-8 16.4MB ± 0% 16.4MB ± 0% ~ (all equal) DiffMainLarge-8 4.84MB ± 0% 4.81MB ± 0% -0.57% (p=0.000 n=10+10) DiffMainRunesLargeLines-8 175kB ± 0% 174kB ± 0% -0.34% (p=0.000 n=10+10) name old allocs/op new allocs/op delta DiffHalfMatch-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) DiffCleanupSemantic-8 3.13k ± 0% 3.12k ± 0% -0.06% (p=0.000 n=10+10) DiffMain-8 83.0 ± 0% 83.0 ± 0% ~ (all equal) DiffMainLarge-8 46.5k ± 0% 46.3k ± 0% -0.41% (p=0.000 n=10+10) DiffMainRunesLargeLines-8 1.09k ± 0% 1.08k ± 0% -0.83% (p=0.000 n=9+10)
1 parent 6991d24 commit 258a5e0

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

diffmatchpatch/diff.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, dea
178178
diffsA := dmp.diffMainRunes(text1A, text2A, checklines, deadline)
179179
diffsB := dmp.diffMainRunes(text1B, text2B, checklines, deadline)
180180
// Merge the results.
181-
return append(diffsA, append([]Diff{Diff{DiffEqual, string(midCommon)}}, diffsB...)...)
181+
diffs := diffsA
182+
diffs = append(diffs, Diff{DiffEqual, string(midCommon)})
183+
diffs = append(diffs, diffsB...)
184+
return diffs
182185
} else if checklines && len(text1) > 100 && len(text2) > 100 {
183186
return dmp.diffLineMode(text1, text2, deadline)
184187
}
@@ -685,9 +688,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
685688
(len(lastequality) <= difference2) {
686689
// Duplicate record.
687690
insPoint := equalities.data
688-
diffs = append(
689-
diffs[:insPoint],
690-
append([]Diff{Diff{DiffDelete, lastequality}}, diffs[insPoint:]...)...)
691+
diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality})
691692

692693
// Change second copy to insert.
693694
diffs[insPoint+1].Type = DiffInsert
@@ -738,10 +739,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
738739
float64(overlapLength1) >= float64(len(insertion))/2 {
739740

740741
// Overlap found. Insert an equality and trim the surrounding edits.
741-
diffs = append(
742-
diffs[:pointer],
743-
append([]Diff{Diff{DiffEqual, insertion[:overlapLength1]}}, diffs[pointer:]...)...)
744-
742+
diffs = splice(diffs, pointer, 0, Diff{DiffEqual, insertion[:overlapLength1]})
745743
diffs[pointer-1].Text =
746744
deletion[0 : len(deletion)-overlapLength1]
747745
diffs[pointer+1].Text = insertion[overlapLength1:]
@@ -752,10 +750,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
752750
float64(overlapLength2) >= float64(len(insertion))/2 {
753751
// Reverse overlap found. Insert an equality and swap and trim the surrounding edits.
754752
overlap := Diff{DiffEqual, deletion[:overlapLength2]}
755-
diffs = append(
756-
diffs[:pointer],
757-
append([]Diff{overlap}, diffs[pointer:]...)...)
758-
753+
diffs = splice(diffs, pointer, 0, overlap)
759754
diffs[pointer-1].Type = DiffInsert
760755
diffs[pointer-1].Text = insertion[0 : len(insertion)-overlapLength2]
761756
diffs[pointer+1].Type = DiffDelete
@@ -968,8 +963,7 @@ func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff {
968963
insPoint := equalities.data
969964

970965
// Duplicate record.
971-
diffs = append(diffs[:insPoint],
972-
append([]Diff{Diff{DiffDelete, lastequality}}, diffs[insPoint:]...)...)
966+
diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality})
973967

974968
// Change second copy to insert.
975969
diffs[insPoint+1].Type = DiffInsert

0 commit comments

Comments
 (0)