From 20f6dc92bc7f843834187f041d3007ef3b4fe9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Mon, 15 Jul 2024 18:46:39 -0700 Subject: [PATCH] large performance improvement sorting 400k - 800k files is much faster, and no longer unbearable --- internal/dirs/dirs.go | 10 +++++----- internal/files/disk.go | 13 +++++++------ internal/files/files.go | 2 -- internal/files/trash.go | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/internal/dirs/dirs.go b/internal/dirs/dirs.go index f20fa1a..b491774 100644 --- a/internal/dirs/dirs.go +++ b/internal/dirs/dirs.go @@ -8,17 +8,17 @@ import ( const sep = string(os.PathSeparator) +var ( + home string = os.Getenv("HOME") + pwd, _ = os.Getwd() +) + // UnExpand unexpands some directory shortcuts // // $HOME -> ~ // $PWD -> . // workdir -> / func UnExpand(dir, workdir string) (outdir string) { - var ( - home string = os.Getenv("HOME") - pwd, _ = os.Getwd() - ) - if dir != "" { outdir = cleanDir(dir, pwd) } diff --git a/internal/files/disk.go b/internal/files/disk.go index bd63e83..c771008 100644 --- a/internal/files/disk.go +++ b/internal/files/disk.go @@ -1,7 +1,6 @@ package files import ( - "fmt" "io/fs" "os" "path/filepath" @@ -22,7 +21,7 @@ type DiskFile struct { } func (f DiskFile) Name() string { return f.name } -func (f DiskFile) Path() string { return filepath.Join(f.path, f.name) } +func (f DiskFile) Path() string { return f.path } func (f DiskFile) Date() time.Time { return f.modified } func (f DiskFile) IsDir() bool { return f.isdir } func (f DiskFile) Filesize() int64 { @@ -33,7 +32,9 @@ func (f DiskFile) Filesize() int64 { } func (f DiskFile) String() string { - return fmt.Sprintf(string_format, f.name, f.path, f.modified.Format(time.UnixDate), f.filesize, f.isdir) + // this is unique enough because two files can't be named the same in the same directory + // right??? + return f.name + f.path } func NewDisk(path string) (DiskFile, error) { @@ -56,7 +57,7 @@ func NewDisk(path string) (DiskFile, error) { return DiskFile{ name: name, - path: base_path, + path: filepath.Join(base_path, name), filesize: info.Size(), modified: info.ModTime(), isdir: info.IsDir(), @@ -132,7 +133,7 @@ func walk_dir(dir string, f *filter.Filter) (files Files) { return nil } files = append(files, DiskFile{ - path: filepath.Dir(p), + path: p, name: name, filesize: i.Size(), modified: i.ModTime(), @@ -173,7 +174,7 @@ func read_dir(dir string, f *filter.Filter) (files Files) { log.Debugf("found matching file: %s %s", name, info.ModTime()) files = append(files, DiskFile{ name: name, - path: path, + path: filepath.Join(path, name), modified: info.ModTime(), filesize: info.Size(), isdir: info.IsDir(), diff --git a/internal/files/files.go b/internal/files/files.go index d8def04..0cf74d6 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -7,8 +7,6 @@ import ( "time" ) -const string_format = "%s %s %s %d %t" - type File interface { Name() string Path() string diff --git a/internal/files/trash.go b/internal/files/trash.go index 5dc83d1..a72fdb6 100644 --- a/internal/files/trash.go +++ b/internal/files/trash.go @@ -55,7 +55,7 @@ func (t TrashInfo) Filesize() int64 { } func (t TrashInfo) String() string { - return fmt.Sprintf(string_format, t.name, t.path, t.trashed.Format(time.UnixDate), t.filesize, t.isdir) + return t.name + t.path + t.ogpath + t.trashinfo } func FindTrash(trashdir, ogdir string, f *filter.Filter) (files Files, outerr error) {