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

Replace deprecated io/ioutil functions #1452

Merged
merged 1 commit into from
Dec 20, 2023
Merged

Replace deprecated io/ioutil functions #1452

merged 1 commit into from
Dec 20, 2023

Conversation

Juneezee
Copy link
Contributor

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.

Footnotes

  1. https://golang.org/doc/go1.16#ioutil

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 <[email protected]>
Comment on lines -54 to +64
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 {
Copy link
Contributor Author

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

  1. https://pkg.go.dev/os#Open

  2. https://pkg.go.dev/os#File.Readdirnames

Copy link
Owner

@johnkerl johnkerl left a 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!! :)

@johnkerl johnkerl merged commit f4cf166 into johnkerl:main Dec 20, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants