unix: avoid allocations for common uses of Readv, Writev, etc.

Fixes golang/go#57296

Change-Id: Ifd57487122a590df467e97e2d35f388a58cc080d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/457815
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Eric Lagergren
2022-12-21 03:31:56 +00:00
committed by Gopher Robot
parent 2204b6615f
commit 3b1fc93fc1
2 changed files with 70 additions and 13 deletions
+41
View File
@@ -1132,3 +1132,44 @@ func TestPwritevOffsets(t *testing.T) {
t.Fatalf("expected size to be %d, got %d", want, info.Size())
}
}
func TestReadvAllocate(t *testing.T) {
f, err := os.Create(filepath.Join(t.TempDir(), "test"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { f.Close() })
test := func(name string, fn func(fd int)) {
n := int(testing.AllocsPerRun(100, func() {
fn(int(f.Fd()))
}))
if n != 0 {
t.Errorf("%q got %d allocations, want 0", name, n)
}
}
iovs := make([][]byte, 8)
for i := range iovs {
iovs[i] = []byte{'A'}
}
test("Writev", func(fd int) {
unix.Writev(fd, iovs)
})
test("Pwritev", func(fd int) {
unix.Pwritev(fd, iovs, 0)
})
test("Pwritev2", func(fd int) {
unix.Pwritev2(fd, iovs, 0, 0)
})
test("Readv", func(fd int) {
unix.Readv(fd, iovs)
})
test("Preadv", func(fd int) {
unix.Preadv(fd, iovs, 0)
})
test("Preadv2", func(fd int) {
unix.Preadv2(fd, iovs, 0, 0)
})
}