diff --git a/fileio/signature.go b/fileio/signature.go index 700312d..f178b71 100644 --- a/fileio/signature.go +++ b/fileio/signature.go @@ -41,7 +41,8 @@ func ReadSignature(file string) ([]sync.Table, error) { } defer f.Close() - read := []sync.Table{} + var read []sync.Table + dataDecoder := gob.NewDecoder(f) err = dataDecoder.Decode(&read) diff --git a/sync/sync.go b/sync/sync.go index 35ee177..3277fa7 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -10,7 +10,6 @@ import ( "io" "github.com/geolffreym/rolling-sync/adler32" - "github.com/geolffreym/rolling-sync/utils" ) const S = 16 @@ -20,11 +19,12 @@ type Indexes map[uint32]map[string]int /* Bytes store block differences - Missing = true && len(Lit) == 0 (block missing) - Missing = false && len(Lit) > 0 (block exist and some changes made to block) - Missing == false && Lit == nil (block intact just copy it) - Literal matches = any textual/literal value match found eg. "abcdef" - No literal matches = any match found by position range in block to copy eg. Block missing && Start > 0 && Offset > 0 + + Missing = true && len(Lit) == 0 (block missing) + Missing = false && len(Lit) > 0 (block exist and some changes made to block) + Missing == false && Lit == nil (block intact just copy it) + Literal matches = any textual/literal value match found eg. "abcdef" + No literal matches = any match found by position range in block to copy eg. Block missing && Start > 0 && Offset > 0 */ type Bytes struct { Offset int // End of diff position in block @@ -200,8 +200,8 @@ func (s *Sync) Delta(sig []Table, reader *bufio.Reader) Delta { delta.Add(index, newBlock) // Add new block to delta matches // Clear garbage collectable - utils.Clear(&tmpLitMatches) // Clear tmp literal matches - utils.Clear(&weak) // Clear weak adler object + tmpLitMatches = tmpLitMatches[:0] // clear tmp literal matches + weak = adler32.New() // replace weak adler object } } @@ -209,13 +209,6 @@ func (s *Sync) Delta(sig []Table, reader *bufio.Reader) Delta { // Missing blocks? // Finally check the blocks integrity // Return cleaned/amplified copy for delta matches - delta = s.IntegrityCheck(sig, delta) - - // Garbage collectable needed based on mem profiling analysis - utils.Clear(&weak) - utils.Clear(&tmpLitMatches) - return delta + return s.IntegrityCheck(sig, delta) } - -// TODO: add patch method diff --git a/utils/utils.go b/utils/utils.go deleted file mode 100644 index a7dbd7b..0000000 --- a/utils/utils.go +++ /dev/null @@ -1,12 +0,0 @@ -package utils - -import ( - "reflect" -) - -// Clear garbage collectable struct/* -func Clear(v any) { - // https://stackoverflow.com/questions/29168905/how-to-clear-values-of-a-instance-of-a-type-struct-dynamically/51006888#51006888 - p := reflect.ValueOf(v).Elem() - p.Set(reflect.Zero(p.Type())) -} diff --git a/utils/utils_test.go b/utils/utils_test.go deleted file mode 100644 index abbeb17..0000000 --- a/utils/utils_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package utils - -import ( - "reflect" - "testing" -) - -func TestUtilClear(t *testing.T) { - - type S struct { - a int - b int - c []byte - } - - a := []byte("abcdef") - b := []int{1, 2, 3, 4, 5, 6} - c := S{ - a: 1, - b: 2, - c: a, - } - - Clear(&a) - Clear(&b) - Clear(&c) - - if len(a) > 0 || len(b) > 0 { - t.Errorf("Expected empty slices after clearing") - } - - if (!reflect.DeepEqual(c, S{})) { - t.Errorf("Expected empty struct after clearing") - } - -}