Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap maps implementations to xsync #35

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/thushan/smash
go 1.21

require (
github.com/alphadose/haxmap v1.3.0
github.com/cespare/xxhash v1.1.0
github.com/dustin/go-humanize v1.0.1
github.com/pterm/pterm v0.12.71
github.com/puzpuzpuz/xsync/v3 v3.0.2
github.com/spaolacci/murmur3 v1.1.0
github.com/spf13/cobra v1.8.0
github.com/thediveo/enumflag/v2 v2.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alphadose/haxmap v1.3.0 h1:C/2LboOnPCZP27GmmSXOcwx360st0P8N0fTJ3voefKc=
github.com/alphadose/haxmap v1.3.0/go.mod h1:rjHw1IAqbxm0S3U5tD16GoKsiAd8FWx5BJ2IYqXwgmM=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down Expand Up @@ -72,6 +70,8 @@ github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5b
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
github.com/pterm/pterm v0.12.71 h1:KcEJ98EiVCbzDkFbktJ2gMlr4pn8IzyGb9bwK6ffkuA=
github.com/pterm/pterm v0.12.71/go.mod h1:SUAcoZjRt+yjPWlWba+/Fd8zJJ2lSXBQWf0Z0HbFiIQ=
github.com/puzpuzpuz/xsync/v3 v3.0.2 h1:3yESHrRFYr6xzkz61LLkvNiPFXxJEAABanTQpKbAaew=
github.com/puzpuzpuz/xsync/v3 v3.0.2/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
7 changes: 4 additions & 3 deletions internal/report/smashfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"encoding/hex"
"sync"

"github.com/alphadose/haxmap"
"github.com/puzpuzpuz/xsync/v3"

"github.com/dustin/go-humanize"
"github.com/thushan/smash/pkg/slicer"
)
Expand All @@ -27,7 +28,7 @@ type EmptyFiles struct {
sync.RWMutex
}

func SummariseSmashedFile(stats slicer.SlicerStats, filename string, ms int64, duplicates *haxmap.Map[string, *DuplicateFiles], empty *EmptyFiles) {
func SummariseSmashedFile(stats slicer.SlicerStats, filename string, ms int64, duplicates *xsync.MapOf[string, *DuplicateFiles], empty *EmptyFiles) {
file := SmashFile{
Hash: hex.EncodeToString(stats.Hash),
Filename: filename,
Expand All @@ -42,7 +43,7 @@ func SummariseSmashedFile(stats slicer.SlicerStats, filename string, ms int64, d
empty.Files = append(empty.Files, file)
empty.Unlock()
} else {
dupes, _ := duplicates.GetOrSet(file.Hash, &DuplicateFiles{})
dupes, _ := duplicates.LoadOrStore(file.Hash, &DuplicateFiles{})
dupes.Lock()
dupes.Files = append(dupes.Files, file)
dupes.Unlock()
Expand Down
30 changes: 14 additions & 16 deletions internal/smash/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"fmt"
"os"
"sync"
"sync/atomic"
"time"

"github.com/puzpuzpuz/xsync/v3"

"golang.org/x/term"

"github.com/thushan/smash/internal/report"

"github.com/pterm/pterm"
"github.com/thushan/smash/internal/theme"

"github.com/alphadose/haxmap"

"github.com/thushan/smash/internal/algorithms"
"github.com/thushan/smash/pkg/slicer"

Expand All @@ -31,8 +30,8 @@ type App struct {
Locations []string
}
type AppSession struct {
Dupes *haxmap.Map[string, *report.DuplicateFiles]
Fails *haxmap.Map[string, error]
Dupes *xsync.MapOf[string, *report.DuplicateFiles]
Fails *xsync.MapOf[string, error]
Empty *report.EmptyFiles
StartTime int64
EndTime int64
Expand All @@ -57,8 +56,8 @@ func (app *App) Run() error {
}

app.Session = &AppSession{
Dupes: haxmap.New[string, *report.DuplicateFiles](),
Fails: haxmap.New[string, error](),
Dupes: xsync.NewMapOf[string, *report.DuplicateFiles](),
Fails: xsync.NewMapOf[string, error](),
Empty: &report.EmptyFiles{},
StartTime: time.Now().UnixNano(),
EndTime: -1,
Expand Down Expand Up @@ -118,12 +117,12 @@ func (app *App) Exec() error {
if isVerbose {
theme.WarnSkipWithContext(location, err)
}
_, _ = session.Fails.GetOrSet(location, err)
_, _ = session.Fails.LoadAndStore(location, err)
}
}
}()

totalFiles := int64(0)
totalFiles := xsync.NewCounter()

pss, _ := theme.SmashingSpinner().WithWriter(pap.NewWriter()).Start("Finding duplicates...")

Expand All @@ -132,7 +131,7 @@ func (app *App) Exec() error {
updateProgressTicker := make(chan bool)

if showProgress {
app.updateDupeCount(updateProgressTicker, pss, &totalFiles)
app.updateDupeCount(updateProgressTicker, pss, *totalFiles)
}

for i := 0; i < app.Flags.MaxWorkers; i++ {
Expand All @@ -142,7 +141,7 @@ func (app *App) Exec() error {
for file := range files {
sf := resolveFilename(file)

atomic.AddInt64(&totalFiles, 1)
totalFiles.Inc()

startTime := time.Now().UnixMilli()
stats, err := sl.SliceFS(file.FileSystem, file.Path, slo)
Expand All @@ -152,7 +151,7 @@ func (app *App) Exec() error {
if isVerbose {
theme.WarnSkipWithContext(file.FullName, err)
}
_, _ = session.Fails.GetOrSet(sf, err)
_, _ = session.Fails.LoadOrStore(sf, err)
} else {
report.SummariseSmashedFile(stats, sf, elapsedMs, session.Dupes, session.Empty)
}
Expand All @@ -167,7 +166,7 @@ func (app *App) Exec() error {
pss.Success("Finding duplicates...Done!")

psr, _ := theme.FinaliseSpinner().WithWriter(pap.NewWriter()).Start("Finding smash hits...")
app.generateRunSummary(totalFiles)
app.generateRunSummary(totalFiles.Value())
psr.Success("Finding smash hits...Done!")

pap.Stop()
Expand All @@ -186,7 +185,7 @@ func (app *App) Exec() error {
return nil
}

func (app *App) updateDupeCount(updateProgressTicker chan bool, pss *pterm.SpinnerPrinter, totalFiles *int64) {
func (app *App) updateDupeCount(updateProgressTicker chan bool, pss *pterm.SpinnerPrinter, totalFiles xsync.Counter) {
if app.Flags.HideProgress {
return
}
Expand All @@ -195,8 +194,7 @@ func (app *App) updateDupeCount(updateProgressTicker chan bool, pss *pterm.Spinn
for {
select {
case <-ticker:
latestFileCount := atomic.LoadInt64(totalFiles)
pss.UpdateText(fmt.Sprintf("Finding duplicates... (%s files smash'd)", pterm.Gray(latestFileCount)))
pss.UpdateText(fmt.Sprintf("Finding duplicates... (%s files smash'd)", pterm.Gray(totalFiles.Value())))
case <-updateProgressTicker:
return
}
Expand Down
17 changes: 8 additions & 9 deletions internal/smash/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ func (app *App) PrintRunAnalysis(ignoreEmptyFiles bool) {

theme.StyleHeading.Println("---| Duplicate Files (", totalDuplicates, ")")

if duplicates.Len() == 0 || len(topFiles) == 0 {
if duplicates.Size() == 0 || len(topFiles) == 0 {
theme.Println(theme.ColourSuccess("No duplicates found :-)"))
} else {

if !app.Flags.HideTopList {
theme.StyleSubHeading.Println("---[ Top ", app.Flags.ShowTop, " Duplicates ]---")
for _, tf := range topFiles {
if files, ok := duplicates.Get(tf.Key); ok {
if files, ok := duplicates.Load(tf.Key); ok {
displayFiles(files.Files)
}
}
}

if app.Flags.ShowDuplicates {
theme.StyleSubHeading.Println("---[ All Duplicates ]---")
duplicates.ForEach(func(hash string, files *report.DuplicateFiles) bool {
duplicates.Range(func(hash string, files *report.DuplicateFiles) bool {
displayFiles(files.Files)
return true
})
Expand Down Expand Up @@ -92,24 +92,23 @@ func (app *App) generateRunSummary(totalFiles int64) {
topFiles := analysis.NewSummary(app.Flags.ShowTop)

totalDuplicates := 0
totalUniqueFiles := int64(duplicates.Len())
totalUniqueFiles := int64(duplicates.Size())
totalDuplicateSize := uint64(0)
totalFailFileCount := int64(session.Fails.Len())
totalFailFileCount := int64(session.Fails.Size())
totalEmptyFileCount := int64(len(emptyFiles))

duplicates.ForEach(func(hash string, df *report.DuplicateFiles) bool {
duplicates.Range(func(hash string, df *report.DuplicateFiles) bool {
files := df.Files
duplicateFiles := len(files) - 1
if duplicateFiles == 0 {
// prune unique files
duplicates.Del(hash)
duplicates.Delete(hash)
} else {
root := files[0]
dupes := files[1:]

topFiles.Add(analysis.Item{Key: hash, Size: root.FileSize})

totalDuplicates += len(dupes)
totalDuplicates += duplicateFiles
totalDuplicateSize += root.FileSize * uint64(duplicateFiles)
}
return true
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ This project was possible thanks to the following projects or folks.
* [@wader/fq](https://github.com/wader/fq) - countless nights of inspecting binary blobs!
* [@cespare/xxhash](https://github.com/cespare/xxhash) - xxhash implementation
* [@spaolacci/murmur3](https://github.com/spaolacci/murmur3) - murmur3 implementation
* [@alphadose/haxmap](https://github.com/alphadose/haxmap) - Amazingly efficient map implementation
* [@puzpuzpuz/xsync](https://github.com/puzpuzpuz/xsync) - Amazingly efficient map implementation
* [@pterm/pterm](https://github.com/pterm/pterm) - Amazing TUI framework used
* [@spf13/cobra](https://github.com/spf13/cobra) - CLI Magic with Cobra
* [@golangci/golangci-lint](https://github.com/golangci/golangci-lint) - Go Linter
Expand Down
Loading