From 593f098276206aad9fed607c36020ced096a4867 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 28 May 2014 07:32:52 +0200 Subject: [PATCH] More memory efficient index sending --- protocol/protocol.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/protocol/protocol.go b/protocol/protocol.go index 442b185ab..67ac60732 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -77,7 +77,7 @@ type rawConnection struct { xw *xdr.Writer wmut sync.Mutex - indexSent map[string]map[string][2]int64 + indexSent map[string]uint64 awaiting []chan asyncResult imut sync.Mutex @@ -117,8 +117,8 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M cw: cw, wb: wb, xw: xdr.NewWriter(wb), + indexSent: make(map[string]uint64), awaiting: make([]chan asyncResult, 0x1000), - indexSent: make(map[string]map[string][2]int64), outbox: make(chan []encodable), nextID: make(chan int), closed: make(chan struct{}), @@ -140,26 +140,29 @@ func (c *rawConnection) ID() string { func (c *rawConnection) Index(repo string, idx []FileInfo) { c.imut.Lock() var msgType int - if c.indexSent[repo] == nil { + maxSent := c.indexSent[repo] + var newMaxSent uint64 + if maxSent == 0 { // This is the first time we send an index. msgType = messageTypeIndex - - c.indexSent[repo] = make(map[string][2]int64) for _, f := range idx { - c.indexSent[repo][f.Name] = [2]int64{f.Modified, int64(f.Version)} + if f.Version > newMaxSent { + newMaxSent = f.Version + } } } else { // We have sent one full index. Only send updates now. msgType = messageTypeIndexUpdate var diff []FileInfo for _, f := range idx { - if vs, ok := c.indexSent[repo][f.Name]; !ok || f.Modified != vs[0] || int64(f.Version) != vs[1] { + if f.Version > maxSent { diff = append(diff, f) - c.indexSent[repo][f.Name] = [2]int64{f.Modified, int64(f.Version)} + newMaxSent = f.Version } } idx = diff } + c.indexSent[repo] = newMaxSent c.imut.Unlock() c.send(header{0, -1, msgType}, IndexMessage{repo, idx})