Stat in the walk, hash only shared sizes, flush batches mid-scan
Restructure scan into three phases: walk+stat, hash, update. The stat pass is folded into the walk workers: each regular file is lstatted as its directory is read, while the metadata is hot. The walk builds a scan-wide size census (walked files plus records outside the scan roots), and unchanged already-hashed files resolve during the walk without further work. Only files whose size at least one other file shares are ever read: a size-unique file cannot be a duplicate, so it is recorded without hashes (head and tail empty). When a later scan makes its size shared, the file is hashed then, even if otherwise unchanged. report excludes unhashed records; trees gives them a never-matching signature so a tree containing one never compares equal to another. Hashed records are committed in batched transactions while the hash phase runs, so an interrupted scan keeps everything hashed so far and the next run resumes cheaply. The hash phase total is exact, giving a meaningful ETA. Memory drops accordingly: the existing-record index holds only path, size, mtime, and a hashed flag (no hash values); the walk carries one small record per candidate file; overlapping operands are pruned up front instead of deduplicating every walked path in a scan-wide set. Files no bigger than one chunk are hashed with a single read.
This commit is contained in:
17
progress.go
17
progress.go
@@ -38,7 +38,10 @@ func stderrIsTTY() bool {
|
||||
// stderr is not a TTY it emits no ANSI redraws: it prints a plain
|
||||
// one-line update no more often than every plainInterval.
|
||||
//
|
||||
// All methods must be called from the main goroutine only.
|
||||
// All methods must be called from the main goroutine only. A nil
|
||||
// *progress is a valid no-display receiver: every method is a no-op,
|
||||
// so batched database flushes during the streaming pass can reuse the
|
||||
// update-pass helpers without rendering anything.
|
||||
type progress struct {
|
||||
label string
|
||||
total int64 // -1 when unknown (walk pass)
|
||||
@@ -82,6 +85,10 @@ func newProgress(label string, total int64) *progress {
|
||||
|
||||
// increment records one completed item and refreshes the display.
|
||||
func (p *progress) increment() {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.count++
|
||||
if p.bar != nil {
|
||||
_ = p.bar.Add(1)
|
||||
@@ -97,6 +104,10 @@ func (p *progress) increment() {
|
||||
|
||||
// warnf prints a one-line warning to stderr without corrupting the bar.
|
||||
func (p *progress) warnf(format string, args ...any) {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p.bar != nil {
|
||||
_ = p.bar.Clear()
|
||||
}
|
||||
@@ -106,6 +117,10 @@ func (p *progress) warnf(format string, args ...any) {
|
||||
|
||||
// finish terminates the pass's display.
|
||||
func (p *progress) finish() {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p.bar != nil {
|
||||
_ = p.bar.Finish()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user