From 4de1398b7b34fa6e89ac25fd6db7d7e65ed89815 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Mon, 2 Jun 2025 16:05:03 +0200 Subject: [PATCH 1/2] optimize ldiff compare --- app/ldiff/diff.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/ldiff/diff.go b/app/ldiff/diff.go index 83b4c214..74741461 100644 --- a/app/ldiff/diff.go +++ b/app/ldiff/diff.go @@ -381,17 +381,23 @@ func (d *diff) compareResults(dctx *diffCtx, r Range, myRes, otherRes RangeResul } func (d *diff) compareElementsEqual(dctx *diffCtx, my, other []Element) { - find := func(list []Element, targetEl Element) (has, eq bool) { - for _, el := range list { - if el.Id == targetEl.Id { - return true, el.Head == targetEl.Head - } + find := func(list map[string]string, targetEl Element) (has, eq bool) { + if head, ok := list[targetEl.Id]; ok { + return true, head == targetEl.Head } return false, false } + toMap := func(els []Element) map[string]string { + result := make(map[string]string, len(els)) + for _, el := range els { + result[el.Id] = el.Head + } + return result + } + for _, el := range my { - has, eq := find(other, el) + has, eq := find(toMap(other), el) if !has { dctx.removedIds = append(dctx.removedIds, el.Id) continue @@ -403,7 +409,7 @@ func (d *diff) compareElementsEqual(dctx *diffCtx, my, other []Element) { } for _, el := range other { - if has, _ := find(my, el); !has { + if has, _ := find(toMap(my), el); !has { dctx.newIds = append(dctx.newIds, el.Id) } } From ebb12e2fc76ff4144765faf01501849404f61b3b Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Mon, 2 Jun 2025 16:12:28 +0200 Subject: [PATCH 2/2] fix --- app/ldiff/diff.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/ldiff/diff.go b/app/ldiff/diff.go index 74741461..f1581b0d 100644 --- a/app/ldiff/diff.go +++ b/app/ldiff/diff.go @@ -396,8 +396,9 @@ func (d *diff) compareElementsEqual(dctx *diffCtx, my, other []Element) { return result } + otherMap := toMap(other) for _, el := range my { - has, eq := find(toMap(other), el) + has, eq := find(otherMap, el) if !has { dctx.removedIds = append(dctx.removedIds, el.Id) continue @@ -408,8 +409,9 @@ func (d *diff) compareElementsEqual(dctx *diffCtx, my, other []Element) { } } + myMap := toMap(my) for _, el := range other { - if has, _ := find(toMap(my), el); !has { + if has, _ := find(myMap, el); !has { dctx.newIds = append(dctx.newIds, el.Id) } }