-
Notifications
You must be signed in to change notification settings - Fork 217
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
Replace deprecated io/ioutil
functions
#1452
Conversation
The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
entries, err := ioutil.ReadDir(dirname) | ||
f, err := os.Open(dirname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
for i := range entries { | ||
entry := &entries[i] | ||
name := (*entry).Name() | ||
names, err := f.Readdirnames(-1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, name := range names { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced ioutil.ReadDir
with os.Open
1 + Readdirnames
2 because we are only making use of the file names here.
I wrote a small benchmark, and the results show that it is slightly faster and more efficient to use Readdirnames
.
// Before running the benchmark, create 1000 .txt files in /tmp
// for i in {1..1000}; do touch "/tmp/file_$i.txt"; done
func IoutilReadDir(dirname string, extension string) ([]string, error) {
dslStrings := make([]string, 0)
entries, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
for i := range entries {
entry := &entries[i]
name := (*entry).Name()
if !strings.HasSuffix(name, extension) {
continue
}
path := dirname + "/" + name
dslString, err := LoadStringFromFile(path)
if err != nil {
return nil, err
}
dslStrings = append(dslStrings, dslString)
}
return dslStrings, nil
}
func OsReadDir(dirname string, extension string) ([]string, error) {
dslStrings := make([]string, 0)
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
for i := range entries {
entry := &entries[i]
name := (*entry).Name()
if !strings.HasSuffix(name, extension) {
continue
}
path := dirname + "/" + name
dslString, err := LoadStringFromFile(path)
if err != nil {
return nil, err
}
dslStrings = append(dslStrings, dslString)
}
return dslStrings, nil
}
func OsReaddirnames(dirname string, extension string) ([]string, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
entries, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
dslStrings := make([]string, 0, len(entries))
for _, name := range entries {
if !strings.HasSuffix(name, extension) {
continue
}
path := dirname + "/" + name
dslString, err := LoadStringFromFile(path)
if err != nil {
return nil, err
}
dslStrings = append(dslStrings, dslString)
}
return dslStrings, nil
}
func BenchmarkIoutilReadDir(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := IoutilReadDir("/tmp", ".txt")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkOsReadDir(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := OsReadDir("/tmp", ".txt")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkOsReaddirnames(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := OsReaddirnames("/tmp", ".txt")
if err != nil {
b.Fatal(err)
}
}
}
goos: linux
goarch: amd64
pkg: github.com/johnkerl/miller/pkg/lib
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkIoutilReadDir-16 126 9289861 ns/op 1237645 B/op 9122 allocs/op
BenchmarkOsReadDir-16 158 7633255 ns/op 1056843 B/op 8091 allocs/op
BenchmarkOsReaddirnames-16 218 5493111 ns/op 959764 B/op 7048 allocs/op
PASS
ok github.com/johnkerl/miller/pkg/lib 5.852s
Footnotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic @Juneezee -- thank you!! :)
The
io/ioutil
package has been deprecated as of Go 1.16 1. This commit replaces the existingio/ioutil
functions with their new definitions inio
andos
packages.Footnotes
https://golang.org/doc/go1.16#ioutil ↩