Skip to content

Commit

Permalink
Use a slice instead of a linked list to track equalities
Browse files Browse the repository at this point in the history
This is easier to follow and more idiomatic.
It also offers a minor overall performance boost.

name                       old time/op    new time/op    delta
DiffHalfMatch-8               107µs ± 1%     108µs ± 0%   +0.91%  (p=0.000 n=9+9)
DiffCleanupSemantic-8         968µs ± 1%     921µs ± 1%   -4.87%  (p=0.000 n=9+10)
DiffMain-8                    1.01s ± 0%     1.01s ± 0%     ~     (p=0.353 n=10+10)
DiffMainLarge-8               102ms ± 2%     101ms ± 1%     ~     (p=0.842 n=10+9)
DiffMainRunesLargeLines-8     515µs ± 1%     515µs ± 1%     ~     (p=0.400 n=9+10)

name                       old alloc/op   new alloc/op   delta
DiffHalfMatch-8               106kB ± 0%     106kB ± 0%     ~     (all equal)
DiffCleanupSemantic-8         177kB ± 0%     163kB ± 0%   -7.81%  (p=0.000 n=10+10)
DiffMain-8                   16.4MB ± 0%    16.4MB ± 0%   +0.00%  (p=0.000 n=9+10)
DiffMainLarge-8              4.81MB ± 0%    4.81MB ± 0%     ~     (p=1.000 n=10+10)
DiffMainRunesLargeLines-8     174kB ± 0%     174kB ± 0%     ~     (p=0.810 n=10+10)

name                       old allocs/op  new allocs/op  delta
DiffHalfMatch-8                2.00 ± 0%      2.00 ± 0%     ~     (all equal)
DiffCleanupSemantic-8         3.12k ± 0%     1.11k ± 0%  -64.48%  (p=0.000 n=10+10)
DiffMain-8                     83.0 ± 0%      84.0 ± 0%   +1.20%  (p=0.000 n=9+10)
DiffMainLarge-8               46.3k ± 0%     46.3k ± 0%   -0.08%  (p=0.000 n=10+10)
DiffMainRunesLargeLines-8     1.08k ± 0%     1.08k ± 0%     ~     (all equal)
  • Loading branch information
josharian committed Nov 13, 2017
1 parent 639b52b commit ded6142
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,7 @@ func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune {
func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
changes := false
// Stack of indices where equalities are found.
type equality struct {
data int
next *equality
}
var equalities *equality
equalities := make([]int, 0, len(diffs))

var lastequality string
// Always equal to diffs[equalities[equalitiesLength - 1]][1]
Expand All @@ -662,11 +658,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
for pointer < len(diffs) {
if diffs[pointer].Type == DiffEqual {
// Equality found.

equalities = &equality{
data: pointer,
next: equalities,
}
equalities = append(equalities, pointer)
lengthInsertions1 = lengthInsertions2
lengthDeletions1 = lengthDeletions2
lengthInsertions2 = 0
Expand All @@ -687,21 +679,20 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
(len(lastequality) <= difference1) &&
(len(lastequality) <= difference2) {
// Duplicate record.
insPoint := equalities.data
insPoint := equalities[len(equalities)-1]
diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality})

// Change second copy to insert.
diffs[insPoint+1].Type = DiffInsert
// Throw away the equality we just deleted.
equalities = equalities.next
equalities = equalities[:len(equalities)-1]

if equalities != nil {
equalities = equalities.next
if len(equalities) > 0 {
equalities = equalities[:len(equalities)-1]
}
if equalities != nil {
pointer = equalities.data
} else {
pointer = -1
pointer = -1
if len(equalities) > 0 {
pointer = equalities[len(equalities)-1]
}

lengthInsertions1 = 0 // Reset the counters.
Expand Down

0 comments on commit ded6142

Please sign in to comment.