Created
April 4, 2025 15:47
-
-
Save DeedleFake/b931c0393e4385dd39118ea8088a5729 to your computer and use it in GitHub Desktop.
`io.Reader` vs. `iter.Seq2` vs. `iter.Pull()`
This file contains hidden or 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
| goos: linux | |
| goarch: amd64 | |
| pkg: seqread | |
| cpu: AMD Ryzen 9 3900X 12-Core Processor | |
| │ simple.txt │ push.txt │ pull.txt │ | |
| │ sec/op │ sec/op vs base │ sec/op vs base │ | |
| Read-24 245.9n ± 1% 246.2n ± 2% ~ (p=0.725 n=10) 1137.0n ± 2% +362.29% (p=0.000 n=10) | |
| │ simple.txt │ push.txt │ pull.txt │ | |
| │ B/op │ B/op vs base │ B/op vs base │ | |
| Read-24 1.000Ki ± 0% 1.000Ki ± 0% ~ (p=1.000 n=10) ¹ 1.422Ki ± 0% +42.19% (p=0.000 n=10) | |
| ¹ all samples are equal | |
| │ simple.txt │ push.txt │ pull.txt │ | |
| │ allocs/op │ allocs/op vs base │ allocs/op vs base │ | |
| Read-24 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ 15.000 ± 0% +1400.00% (p=0.000 n=10) | |
| ¹ all samples are equal |
This file contains hidden or 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
| module seqread | |
| go 1.24.1 |
This file contains hidden or 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
| package seqread | |
| import ( | |
| "errors" | |
| "io" | |
| "iter" | |
| ) | |
| const bufsize = 1024 | |
| func seq(r io.Reader) iter.Seq2[[]byte, error] { | |
| return func(yield func([]byte, error) bool) { | |
| var buf [bufsize]byte | |
| for { | |
| n, err := r.Read(buf[:]) | |
| if err != nil { | |
| if errors.Is(err, io.EOF) { | |
| if n > 0 { | |
| yield(buf[:n], nil) | |
| } | |
| return | |
| } | |
| yield(buf[:n], err) | |
| return | |
| } | |
| if !yield(buf[:n], nil) { | |
| return | |
| } | |
| } | |
| } | |
| } | |
| func Simple(r io.Reader) { | |
| var buf [bufsize]byte | |
| for { | |
| _, err := r.Read(buf[:]) | |
| if err != nil { | |
| if errors.Is(err, io.EOF) { | |
| return | |
| } | |
| panic(err) | |
| } | |
| } | |
| } | |
| func Push(r io.Reader) { | |
| for _, err := range seq(r) { | |
| if err != nil { | |
| panic(err) | |
| } | |
| } | |
| } | |
| func Pull(r io.Reader) { | |
| next, done := iter.Pull2(seq(r)) | |
| defer done() | |
| for { | |
| _, err, ok := next() | |
| if !ok { | |
| return | |
| } | |
| if err != nil { | |
| panic(err) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| package seqread_test | |
| import ( | |
| "crypto/rand" | |
| "io" | |
| "seqread" | |
| "testing" | |
| ) | |
| var reader = io.LimitReader(rand.Reader, 1<<24) | |
| func BenchmarkRead(b *testing.B) { | |
| for b.Loop() { | |
| seqread.Simple(reader) | |
| //seqread.Push(reader) | |
| //seqread.Pull(reader) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment