From 41c228cb5658f59461555c3bf8a6c3b13bdbe823 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 13 Apr 2014 14:30:40 +0200 Subject: [PATCH] Faster and leaner comparisons in fileset --- files/set.go | 43 ++++--------------------------------------- files/set_anal.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ files/set_fast.go | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+), 39 deletions(-) create mode 100644 files/set_anal.go create mode 100644 files/set_fast.go diff --git a/files/set.go b/files/set.go index 14491b07a..cd62e7d4a 100644 --- a/files/set.go +++ b/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) } } } diff --git a/files/set_anal.go b/files/set_anal.go new file mode 100644 index 000000000..76cb43f93 --- /dev/null +++ b/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 +} diff --git a/files/set_fast.go b/files/set_fast.go new file mode 100644 index 000000000..d509d7e28 --- /dev/null +++ b/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 +}