Skip to content

Commit d6ba394

Browse files
committed
Fix SyscallVectorisedWriter
1 parent 6414292 commit d6ba394

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

common/bufio/vectorised_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func TestWriteVectorised(t *testing.T) {
2626
finish := Timeout(t)
2727
_, err = WriteVectorised(vectorisedWriter, [][]byte{bufA[:], bufB[:]})
2828
require.NoError(t, err)
29+
_, err = WriteVectorised(vectorisedWriter, [][]byte{})
30+
require.NoError(t, err)
2931
output := make([]byte, 2048)
3032
_, err = io.ReadFull(outputConn, output)
3133
finish()

common/bufio/vectorised_unix.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ func (w *SyscallVectorisedWriter) WriteVectorised(buffers []*buf.Buffer) error {
2525
defer buf.ReleaseMulti(buffers)
2626
iovecList := w.iovecList
2727
for _, buffer := range buffers {
28+
if buffer.IsEmpty() {
29+
continue
30+
}
2831
iovecList = append(iovecList, buffer.Iovec(buffer.Len()))
2932
}
30-
if cap(iovecList) > cap(w.iovecList) {
33+
if len(iovecList) == 0 {
34+
return nil
35+
} else if cap(iovecList) > cap(w.iovecList) {
3136
w.iovecList = iovecList[:0]
3237
}
3338
var innerErr unix.Errno
@@ -36,17 +41,14 @@ func (w *SyscallVectorisedWriter) WriteVectorised(buffers []*buf.Buffer) error {
3641
for {
3742
var r0 uintptr
3843
//nolint:staticcheck
39-
r0, _, innerErr = unix.RawSyscall(unix.SYS_WRITEV, fd, uintptr(unsafe.Pointer(&writeIovecList[0])), uintptr(len(writeIovecList)))
40-
writeN := int(r0)
41-
for writeN > 0 {
42-
if buffers[0].Len() > writeN {
43-
buffers[0].Advance(writeN)
44-
writeIovecList[0] = buffers[0].Iovec(buffers[0].Len())
44+
r0, _, innerErr = unix.RawSyscall(unix.SYS_WRITEV, fd, uintptr(unsafe.Pointer(unsafe.SliceData(writeIovecList))), uintptr(len(writeIovecList)))
45+
writeN := uint64(r0)
46+
for writeN > 0 && len(writeIovecList) > 0 {
47+
if writeIovecList[0].Len > writeN {
48+
writeIovecList[0].Base = (*byte)(unsafe.Add(unsafe.Pointer(writeIovecList[0].Base), writeN))
4549
break
4650
} else {
47-
writeN -= buffers[0].Len()
48-
buffers[0].Release()
49-
buffers = buffers[1:]
51+
writeN -= writeIovecList[0].Len
5052
writeIovecList = writeIovecList[1:]
5153
}
5254
}
@@ -69,6 +71,9 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
6971
defer buf.ReleaseMulti(buffers)
7072
iovecList := w.iovecList
7173
for _, buffer := range buffers {
74+
if buffer.IsEmpty() {
75+
continue
76+
}
7277
iovecList = append(iovecList, buffer.Iovec(buffer.Len()))
7378
}
7479
if cap(iovecList) > cap(w.iovecList) {

common/bufio/vectorised_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
4545
defer buf.ReleaseMulti(buffers)
4646
iovecList := w.iovecList
4747
for _, buffer := range buffers {
48+
if buffer.IsEmpty() {
49+
continue
50+
}
4851
iovecList = append(iovecList, buffer.Iovec(buffer.Len()))
4952
}
5053
var n uint32

0 commit comments

Comments
 (0)