Browse Source

More memory efficient index sending

Jakob Borg 11 years ago
parent
commit
593f098276
1 changed files with 11 additions and 8 deletions
  1. 11 8
      protocol/protocol.go

+ 11 - 8
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})