From: Sergey Matveev Date: Wed, 25 Jun 2025 15:03:10 +0000 (+0300) Subject: Use faster BLAKE3 X-Git-Tag: v0.3.0^0 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=sgodup.git Use faster BLAKE3 --- diff --git a/README b/README index 56beec9..2479e5b 100644 --- a/README +++ b/README @@ -34,7 +34,7 @@ There are 3 stages this command will do: * read first 4 KiB (one disk sector) of each file * if that sector differs, then files are not duplicates * read each file's contents sequentially with 128 KiB chunks and - calculate BLAKE2b-512 digest + calculate BLAKE3-256 digest Action can be the following: diff --git a/go.mod b/go.mod index 547702d..f8b0d41 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,14 @@ module go.stargrave.org/sgodup -go 1.21 +go 1.24 require ( - github.com/dustin/go-humanize v1.0.0 + github.com/dustin/go-humanize v1.0.1 go.cypherpunks.su/netstring/v3 v3.0.0 - golang.org/x/crypto v0.25.0 + lukechampine.com/blake3 v1.4.1 ) -require golang.org/x/sys v0.22.0 // indirect +require ( + github.com/klauspost/cpuid/v2 v2.2.11 // indirect + golang.org/x/sys v0.33.0 // indirect +) diff --git a/go.sum b/go.sum index b788fc3..a787279 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,10 @@ -github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= -github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU= +github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= go.cypherpunks.su/netstring/v3 v3.0.0 h1:wwFjxTb/LZM8cQN/UiOPMO5wcuq4xCQWdLAYz74E6kY= go.cypherpunks.su/netstring/v3 v3.0.0/go.mod h1:S9pYNVqT6kL2uXbdHz+yxc+A4sAFxBkjSzu+g6KD0QE= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +lukechampine.com/blake3 v1.4.1 h1:I3Smz7gso8w4/TunLKec6K2fn+kyKtDxr/xcQEN84Wg= +lukechampine.com/blake3 v1.4.1/go.mod h1:QFosUxmjB8mnrWFSNwKmvxHpfY72bmD2tQ0kBMM3kwo= diff --git a/main.go b/main.go index e49a520..0444131 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ import ( "github.com/dustin/go-humanize" "go.cypherpunks.su/netstring/v3" - "golang.org/x/crypto/blake2b" + "lukechampine.com/blake3" ) const ( @@ -300,10 +300,7 @@ along with this program. If not, see .`) bufOrig := make([]byte, SectorSize) seenDup := make(map[string]struct{}, len(queue)/2) seenOrig := make(map[string]struct{}, len(queue)/2) - hasher, err := blake2b.New512(nil) - if err != nil { - panic(err) - } + hasher := blake3.New(32, nil) rdDup := bufio.NewReaderSize(nil, BufSize) rdOrig := bufio.NewReaderSize(nil, BufSize) var deduped int @@ -349,7 +346,7 @@ along with this program. If not, see .`) readOrig, readDup, ) } - if bytes.Compare(bufDup[:readDup], bufOrig[:readOrig]) != 0 { + if !bytes.Equal(bufDup[:readDup], bufOrig[:readOrig]) { if err = fdOrig.Close(); err != nil { log.Fatal(err) } @@ -389,7 +386,7 @@ along with this program. If not, see .`) if err = fdOrig.Close(); err != nil { log.Fatal(err) } - if bytes.Compare(hashDup, hasher.Sum(nil)) != 0 { + if !bytes.Equal(hashDup, hasher.Sum(nil)) { continue } link(fi.Path, orig.Path) @@ -403,6 +400,7 @@ along with this program. If not, see .`) log.Fatal(err) } } + var err error if action == ActNS { if err = stdoutW.Flush(); err != nil { log.Fatal(err) diff --git a/progress.go b/progress.go index 59d0faf..b5e56ad 100644 --- a/progress.go +++ b/progress.go @@ -53,7 +53,7 @@ func NewProgress( humanize.IBytes(uint64(fullSize)), suffixFiles, suffixSize, - make(chan struct{}, 0), + make(chan struct{}), } go p.Run(files, size) return p