Browse Source

Faster and leaner comparisons in fileset

Jakob Borg 11 years ago
parent
commit
41c228cb56
3 changed files with 69 additions and 39 deletions
  1. 4 39
      files/set.go
  2. 44 0
      files/set_anal.go
  3. 21 0
      files/set_fast.go

+ 4 - 39
files/set.go

@@ -2,7 +2,6 @@
 package files
 
 import (
-	"crypto/md5"
 	"sync"
 
 	"github.com/calmh/syncthing/cid"
@@ -11,13 +10,6 @@ import (
 	"github.com/calmh/syncthing/scanner"
 )
 
-type key struct {
-	Name     string
-	Version  uint64
-	Modified int64
-	Hash     [md5.Size]byte
-}
-
 type fileRecord struct {
 	Usage int
 	File  scanner.File
@@ -25,34 +17,6 @@ type fileRecord struct {
 
 type bitset uint64
 
-func keyFor(f scanner.File) key {
-	h := md5.New()
-	for _, b := range f.Blocks {
-		h.Write(b.Hash)
-	}
-	return key{
-		Name:     f.Name,
-		Version:  f.Version,
-		Modified: f.Modified,
-		Hash:     md5.Sum(nil),
-	}
-}
-
-func (a key) newerThan(b key) bool {
-	if a.Version != b.Version {
-		return a.Version > b.Version
-	}
-	if a.Modified != b.Modified {
-		return a.Modified > b.Modified
-	}
-	for i := 0; i < md5.Size; i++ {
-		if a.Hash[i] != b.Hash[i] {
-			return a.Hash[i] > b.Hash[i]
-		}
-	}
-	return false
-}
-
 type Set struct {
 	sync.Mutex
 	files              map[key]fileRecord
@@ -148,9 +112,10 @@ func (m *Set) Need(id uint) []scanner.File {
 	rkID := m.remoteKey[id]
 	for name, gk := range m.globalKey {
 		if gk.newerThan(rkID[name]) {
-			if m.files[gk].File.Flags&protocol.FlagDirectory == 0 || // Regular file
-				m.files[gk].File.Flags&(protocol.FlagDirectory|protocol.FlagDeleted) == protocol.FlagDirectory { // Non-deleted directory
-				fs = append(fs, m.files[gk].File)
+			file := m.files[gk].File
+			if file.Flags&protocol.FlagDirectory == 0 || // Regular file
+				file.Flags&(protocol.FlagDirectory|protocol.FlagDeleted) == protocol.FlagDirectory { // Non-deleted directory
+				fs = append(fs, file)
 			}
 		}
 	}

+ 44 - 0
files/set_anal.go

@@ -0,0 +1,44 @@
+//+build anal
+
+package files
+
+import (
+	"crypto/md5"
+
+	"github.com/calmh/syncthing/scanner"
+)
+
+type key struct {
+	Name     string
+	Version  uint64
+	Modified int64
+	Hash     [md5.Size]byte
+}
+
+func keyFor(f scanner.File) key {
+	h := md5.New()
+	for _, b := range f.Blocks {
+		h.Write(b.Hash)
+	}
+	return key{
+		Name:     f.Name,
+		Version:  f.Version,
+		Modified: f.Modified,
+		Hash:     md5.Sum(nil),
+	}
+}
+
+func (a key) newerThan(b key) bool {
+	if a.Version != b.Version {
+		return a.Version > b.Version
+	}
+	if a.Modified != b.Modified {
+		return a.Modified > b.Modified
+	}
+	for i := 0; i < md5.Size; i++ {
+		if a.Hash[i] != b.Hash[i] {
+			return a.Hash[i] > b.Hash[i]
+		}
+	}
+	return false
+}

+ 21 - 0
files/set_fast.go

@@ -0,0 +1,21 @@
+//+build !anal
+
+package files
+
+import "github.com/calmh/syncthing/scanner"
+
+type key struct {
+	Name    string
+	Version uint64
+}
+
+func keyFor(f scanner.File) key {
+	return key{
+		Name:    f.Name,
+		Version: f.Version,
+	}
+}
+
+func (a key) newerThan(b key) bool {
+	return a.Version > b.Version
+}