Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When unsorted, Walk uses Scanner to enumerate directories
Slightly smaller memory footprint and fewer allocations. Some room for improvement, although this is enough to capture what I hoped to do with this library for the past few months. ``` go test -bench=. -benchmem goos: linux goarch: amd64 pkg: github.com/karrick/godirwalk BenchmarkReadDirnamesStandardLibrary-8 50000 29312 ns/op 10240 B/op 10 allocs/op BenchmarkReadDirnamesThisLibrary-8 100000 14611 ns/op 4560 B/op 12 allocs/op BenchmarkFilepathWalk-8 10 207360158 ns/op 46235054 B/op 86129 allocs/op BenchmarkGodirwalk-8 10 138169018 ns/op 20777411 B/op 93295 allocs/op BenchmarkGodirwalkUnsorted-8 20 130196966 ns/op 20293127 B/op 78388 allocs/op BenchmarkFlameGraphFilepathWalk-8 1 2456320457 ns/op 462351888 B/op 861292 allocs/op BenchmarkFlameGraphGodirwalk-8 1 1373471992 ns/op 207774112 B/op 932952 allocs/op PASS ok github.com/karrick/godirwalk 13.829s ```
- Loading branch information
Showing
5 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// +build godirwalk_debug | ||
|
||
package godirwalk | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// debug formats and prints arguments to stderr for development builds | ||
func debug(f string, a ...interface{}) { | ||
// fmt.Fprintf(os.Stderr, f, a...) | ||
os.Stderr.Write([]byte("godirwalk: " + fmt.Sprintf(f, a...))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// +build !godirwalk_debug | ||
|
||
package godirwalk | ||
|
||
// debug is a no-op for release builds | ||
func debug(_ string, _ ...interface{}) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package godirwalk | ||
|
||
type scanner interface { | ||
Err() error | ||
Scan() bool | ||
Dirent() (*Dirent, error) | ||
} | ||
|
||
type dirents struct { | ||
dd []*Dirent | ||
de *Dirent | ||
} | ||
|
||
func (d *dirents) Err() error { | ||
d.dd, d.de = nil, nil | ||
return nil | ||
} | ||
|
||
func (d *dirents) Dirent() (*Dirent, error) { return d.de, nil } | ||
|
||
func (d *dirents) Scan() bool { | ||
if len(d.dd) > 0 { | ||
d.de, d.dd = d.dd[0], d.dd[1:] | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters