From 885de4f352ee5967cdb5deeedffd39131a9c6b08 Mon Sep 17 00:00:00 2001 From: Ian Preston Date: Sat, 28 Oct 2017 12:14:49 +0100 Subject: [PATCH] optimise pin update command This handles merkle links that aren't named. And improves the Peergos usage from worst case 30s to ~20ms License: MIT Signed-off-by: Ian Preston --- merkledag/utils/diffenum.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/merkledag/utils/diffenum.go b/merkledag/utils/diffenum.go index 7e3a76356..2066fa338 100644 --- a/merkledag/utils/diffenum.go +++ b/merkledag/utils/diffenum.go @@ -65,27 +65,34 @@ type diffpair struct { // getLinkDiff returns a changeset between nodes 'a' and 'b'. Currently does // not log deletions as our usecase doesnt call for this. func getLinkDiff(a, b node.Node) []diffpair { - have := make(map[string]*node.Link) - names := make(map[string]*node.Link) + ina := make(map[string]*node.Link) + inb := make(map[string]*node.Link) + var aonly []*cid.Cid + for _, l := range b.Links() { + inb[l.Cid.KeyString()] = l + } for _, l := range a.Links() { - have[l.Cid.KeyString()] = l - names[l.Name] = l + ina[l.Cid.KeyString()] = l + if inb[l.Cid.KeyString()] == nil { + aonly = append(aonly, l.Cid) + } } var out []diffpair + var aindex = 0 for _, l := range b.Links() { - if have[l.Cid.KeyString()] != nil { + if ina[l.Cid.KeyString()] != nil { continue } - match, ok := names[l.Name] - if !ok { + if aindex < len(aonly) { + out = append(out, diffpair{bef: aonly[aindex], aft: l.Cid}) + aindex++ + } else { out = append(out, diffpair{aft: l.Cid}) continue } - - out = append(out, diffpair{bef: match.Cid, aft: l.Cid}) } return out }