Skip to content

Commit f7432e0

Browse files
qmuntalgopherbot
authored andcommitted
internal/poll: remove fd field from Windows' poll.operation
There is no need to keep the fd in the poll.operation struct, given that all usages of this field have direct access to the fd struct. This skims down the size of os.File by 16 bytes. Change-Id: I837e345250387f62e294cc1772d752865a04ef6d Reviewed-on: https://go-review.googlesource.com/c/go/+/685415 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Knyszek <[email protected]>
1 parent e84ed38 commit f7432e0

File tree

2 files changed

+61
-67
lines changed

2 files changed

+61
-67
lines changed

src/internal/poll/fd_windows.go

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ type operation struct {
7777
mode int32
7878

7979
// fields used only by net package
80-
fd *FD
8180
buf syscall.WSABuf
8281
msg windows.WSAMsg
8382
sa syscall.Sockaddr
@@ -105,8 +104,8 @@ func (o *operation) close() {
105104
}
106105
}
107106

108-
func (o *operation) overlapped() *syscall.Overlapped {
109-
if o.fd.isBlocking {
107+
func (fd *FD) overlapped(o *operation) *syscall.Overlapped {
108+
if fd.isBlocking {
110109
// Don't return the overlapped object if the file handle
111110
// doesn't use overlapped I/O. It could be used, but
112111
// that would then use the file pointer stored in the
@@ -171,11 +170,10 @@ func (o *operation) InitMsg(p []byte, oob []byte) {
171170
}
172171

173172
// waitIO waits for the IO operation o to complete.
174-
func waitIO(o *operation) error {
175-
if o.fd.isBlocking {
173+
func (fd *FD) waitIO(o *operation) error {
174+
if fd.isBlocking {
176175
panic("can't wait on blocking operations")
177176
}
178-
fd := o.fd
179177
if !fd.pollable() {
180178
// The overlapped handle is not added to the runtime poller,
181179
// the only way to wait for the IO to complete is block until
@@ -195,8 +193,7 @@ func waitIO(o *operation) error {
195193
}
196194

197195
// cancelIO cancels the IO operation o and waits for it to complete.
198-
func cancelIO(o *operation) {
199-
fd := o.fd
196+
func (fd *FD) cancelIO(o *operation) {
200197
if !fd.pollable() {
201198
return
202199
}
@@ -214,8 +211,7 @@ func cancelIO(o *operation) {
214211
// It supports both synchronous and asynchronous IO.
215212
// o.qty and o.flags are set to zero before calling submit
216213
// to avoid reusing the values from a previous call.
217-
func execIO(o *operation, submit func(o *operation) error) (int, error) {
218-
fd := o.fd
214+
func (fd *FD) execIO(o *operation, submit func(o *operation) error) (int, error) {
219215
// Notify runtime netpoll about starting IO.
220216
err := fd.pd.prepare(int(o.mode), fd.isFile)
221217
if err != nil {
@@ -234,13 +230,13 @@ func execIO(o *operation, submit func(o *operation) error) (int, error) {
234230
var waitErr error
235231
// Blocking operations shouldn't return ERROR_IO_PENDING.
236232
// Continue without waiting if that happens.
237-
if !o.fd.isBlocking && (err == syscall.ERROR_IO_PENDING || (err == nil && !o.fd.skipSyncNotif)) {
233+
if !fd.isBlocking && (err == syscall.ERROR_IO_PENDING || (err == nil && !fd.skipSyncNotif)) {
238234
// IO started asynchronously or completed synchronously but
239235
// a sync notification is required. Wait for it to complete.
240-
waitErr = waitIO(o)
236+
waitErr = fd.waitIO(o)
241237
if waitErr != nil {
242238
// IO interrupted by "close" or "timeout".
243-
cancelIO(o)
239+
fd.cancelIO(o)
244240
// We issued a cancellation request, but the IO operation may still succeeded
245241
// before the cancellation request runs.
246242
}
@@ -399,8 +395,6 @@ func (fd *FD) Init(net string, pollable bool) error {
399395
fd.isBlocking = !pollable
400396
fd.rop.mode = 'r'
401397
fd.wop.mode = 'w'
402-
fd.rop.fd = fd
403-
fd.wop.fd = fd
404398

405399
// It is safe to add overlapped handles that also perform I/O
406400
// outside of the runtime poller. The runtime poller will ignore
@@ -514,8 +508,8 @@ func (fd *FD) Read(buf []byte) (int, error) {
514508
case kindFile, kindPipe:
515509
o := &fd.rop
516510
o.InitBuf(buf)
517-
n, err = execIO(o, func(o *operation) error {
518-
return syscall.ReadFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, o.overlapped())
511+
n, err = fd.execIO(o, func(o *operation) error {
512+
return syscall.ReadFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, fd.overlapped(o))
519513
})
520514
fd.addOffset(n)
521515
switch err {
@@ -530,8 +524,8 @@ func (fd *FD) Read(buf []byte) (int, error) {
530524
case kindNet:
531525
o := &fd.rop
532526
o.InitBuf(buf)
533-
n, err = execIO(o, func(o *operation) error {
534-
return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
527+
n, err = fd.execIO(o, func(o *operation) error {
528+
return syscall.WSARecv(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
535529
})
536530
if race.Enabled {
537531
race.Acquire(unsafe.Pointer(&ioSync))
@@ -645,8 +639,8 @@ func (fd *FD) Pread(b []byte, off int64) (int, error) {
645639
o := &fd.rop
646640
o.InitBuf(b)
647641
fd.setOffset(off)
648-
n, err := execIO(o, func(o *operation) error {
649-
return syscall.ReadFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o)
642+
n, err := fd.execIO(o, func(o *operation) error {
643+
return syscall.ReadFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o)
650644
})
651645
if err == syscall.ERROR_HANDLE_EOF {
652646
err = io.EOF
@@ -671,12 +665,12 @@ func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) {
671665
defer fd.readUnlock()
672666
o := &fd.rop
673667
o.InitBuf(buf)
674-
n, err := execIO(o, func(o *operation) error {
668+
n, err := fd.execIO(o, func(o *operation) error {
675669
if o.rsa == nil {
676670
o.rsa = new(syscall.RawSockaddrAny)
677671
}
678672
o.rsan = int32(unsafe.Sizeof(*o.rsa))
679-
return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
673+
return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
680674
})
681675
err = fd.eofError(n, err)
682676
if err != nil {
@@ -700,12 +694,12 @@ func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
700694
defer fd.readUnlock()
701695
o := &fd.rop
702696
o.InitBuf(buf)
703-
n, err := execIO(o, func(o *operation) error {
697+
n, err := fd.execIO(o, func(o *operation) error {
704698
if o.rsa == nil {
705699
o.rsa = new(syscall.RawSockaddrAny)
706700
}
707701
o.rsan = int32(unsafe.Sizeof(*o.rsa))
708-
return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
702+
return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
709703
})
710704
err = fd.eofError(n, err)
711705
if err != nil {
@@ -729,12 +723,12 @@ func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
729723
defer fd.readUnlock()
730724
o := &fd.rop
731725
o.InitBuf(buf)
732-
n, err := execIO(o, func(o *operation) error {
726+
n, err := fd.execIO(o, func(o *operation) error {
733727
if o.rsa == nil {
734728
o.rsa = new(syscall.RawSockaddrAny)
735729
}
736730
o.rsan = int32(unsafe.Sizeof(*o.rsa))
737-
return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
731+
return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
738732
})
739733
err = fd.eofError(n, err)
740734
if err != nil {
@@ -770,8 +764,8 @@ func (fd *FD) Write(buf []byte) (int, error) {
770764
case kindPipe, kindFile:
771765
o := &fd.wop
772766
o.InitBuf(b)
773-
n, err = execIO(o, func(o *operation) error {
774-
return syscall.WriteFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, o.overlapped())
767+
n, err = fd.execIO(o, func(o *operation) error {
768+
return syscall.WriteFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, fd.overlapped(o))
775769
})
776770
fd.addOffset(n)
777771
case kindNet:
@@ -780,8 +774,8 @@ func (fd *FD) Write(buf []byte) (int, error) {
780774
}
781775
o := &fd.wop
782776
o.InitBuf(b)
783-
n, err = execIO(o, func(o *operation) error {
784-
return syscall.WSASend(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil)
777+
n, err = fd.execIO(o, func(o *operation) error {
778+
return syscall.WSASend(fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil)
785779
})
786780
}
787781
ntotal += n
@@ -869,8 +863,8 @@ func (fd *FD) Pwrite(buf []byte, off int64) (int, error) {
869863
o := &fd.wop
870864
o.InitBuf(b)
871865
fd.setOffset(off + int64(ntotal))
872-
n, err := execIO(o, func(o *operation) error {
873-
return syscall.WriteFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o)
866+
n, err := fd.execIO(o, func(o *operation) error {
867+
return syscall.WriteFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o)
874868
})
875869
if n > 0 {
876870
ntotal += n
@@ -898,8 +892,8 @@ func (fd *FD) Writev(buf *[][]byte) (int64, error) {
898892
}
899893
o := &fd.wop
900894
o.InitBufs(buf)
901-
n, err := execIO(o, func(o *operation) error {
902-
return syscall.WSASend(o.fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil)
895+
n, err := fd.execIO(o, func(o *operation) error {
896+
return syscall.WSASend(fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil)
903897
})
904898
o.ClearBufs()
905899
TestHookDidWritev(n)
@@ -919,8 +913,8 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
919913
o := &fd.wop
920914
o.InitBuf(buf)
921915
o.sa = sa
922-
n, err := execIO(o, func(o *operation) error {
923-
return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
916+
n, err := fd.execIO(o, func(o *operation) error {
917+
return syscall.WSASendto(fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
924918
})
925919
return n, err
926920
}
@@ -934,8 +928,8 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
934928
o := &fd.wop
935929
o.InitBuf(b)
936930
o.sa = sa
937-
n, err := execIO(o, func(o *operation) error {
938-
return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
931+
n, err := fd.execIO(o, func(o *operation) error {
932+
return syscall.WSASendto(fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
939933
})
940934
ntotal += int(n)
941935
if err != nil {
@@ -957,8 +951,8 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
957951
// handle zero-byte payload
958952
o := &fd.wop
959953
o.InitBuf(buf)
960-
n, err := execIO(o, func(o *operation) error {
961-
return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
954+
n, err := fd.execIO(o, func(o *operation) error {
955+
return windows.WSASendtoInet4(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
962956
})
963957
return n, err
964958
}
@@ -971,8 +965,8 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
971965
}
972966
o := &fd.wop
973967
o.InitBuf(b)
974-
n, err := execIO(o, func(o *operation) error {
975-
return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
968+
n, err := fd.execIO(o, func(o *operation) error {
969+
return windows.WSASendtoInet4(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
976970
})
977971
ntotal += int(n)
978972
if err != nil {
@@ -994,8 +988,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
994988
// handle zero-byte payload
995989
o := &fd.wop
996990
o.InitBuf(buf)
997-
n, err := execIO(o, func(o *operation) error {
998-
return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
991+
n, err := fd.execIO(o, func(o *operation) error {
992+
return windows.WSASendtoInet6(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
999993
})
1000994
return n, err
1001995
}
@@ -1008,8 +1002,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
10081002
}
10091003
o := &fd.wop
10101004
o.InitBuf(b)
1011-
n, err := execIO(o, func(o *operation) error {
1012-
return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
1005+
n, err := fd.execIO(o, func(o *operation) error {
1006+
return windows.WSASendtoInet6(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
10131007
})
10141008
ntotal += int(n)
10151009
if err != nil {
@@ -1026,8 +1020,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
10261020
func (fd *FD) ConnectEx(ra syscall.Sockaddr) error {
10271021
o := &fd.wop
10281022
o.sa = ra
1029-
_, err := execIO(o, func(o *operation) error {
1030-
return ConnectExFunc(o.fd.Sysfd, o.sa, nil, 0, nil, &o.o)
1023+
_, err := fd.execIO(o, func(o *operation) error {
1024+
return ConnectExFunc(fd.Sysfd, o.sa, nil, 0, nil, &o.o)
10311025
})
10321026
return err
10331027
}
@@ -1036,8 +1030,8 @@ func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *ope
10361030
// Submit accept request.
10371031
o.handle = s
10381032
o.rsan = int32(unsafe.Sizeof(rawsa[0]))
1039-
_, err := execIO(o, func(o *operation) error {
1040-
return AcceptFunc(o.fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o)
1033+
_, err := fd.execIO(o, func(o *operation) error {
1034+
return AcceptFunc(fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o)
10411035
})
10421036
if err != nil {
10431037
CloseFunc(s)
@@ -1179,11 +1173,11 @@ func (fd *FD) RawRead(f func(uintptr) bool) error {
11791173
// socket is readable. h/t https://stackoverflow.com/a/42019668/332798
11801174
o := &fd.rop
11811175
o.InitBuf(nil)
1182-
_, err := execIO(o, func(o *operation) error {
1176+
_, err := fd.execIO(o, func(o *operation) error {
11831177
if !fd.IsStream {
11841178
o.flags |= windows.MSG_PEEK
11851179
}
1186-
return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
1180+
return syscall.WSARecv(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
11871181
})
11881182
if err == windows.WSAEMSGSIZE {
11891183
// expected with a 0-byte peek, ignore.
@@ -1278,8 +1272,8 @@ func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.S
12781272
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
12791273
o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
12801274
o.msg.Flags = uint32(flags)
1281-
n, err := execIO(o, func(o *operation) error {
1282-
return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
1275+
n, err := fd.execIO(o, func(o *operation) error {
1276+
return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
12831277
})
12841278
err = fd.eofError(n, err)
12851279
var sa syscall.Sockaddr
@@ -1308,8 +1302,8 @@ func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.Sockadd
13081302
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
13091303
o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
13101304
o.msg.Flags = uint32(flags)
1311-
n, err := execIO(o, func(o *operation) error {
1312-
return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
1305+
n, err := fd.execIO(o, func(o *operation) error {
1306+
return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
13131307
})
13141308
err = fd.eofError(n, err)
13151309
if err == nil {
@@ -1337,8 +1331,8 @@ func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.Sockadd
13371331
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
13381332
o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
13391333
o.msg.Flags = uint32(flags)
1340-
n, err := execIO(o, func(o *operation) error {
1341-
return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
1334+
n, err := fd.execIO(o, func(o *operation) error {
1335+
return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
13421336
})
13431337
err = fd.eofError(n, err)
13441338
if err == nil {
@@ -1371,8 +1365,8 @@ func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, err
13711365
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
13721366
o.msg.Namelen = len
13731367
}
1374-
n, err := execIO(o, func(o *operation) error {
1375-
return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
1368+
n, err := fd.execIO(o, func(o *operation) error {
1369+
return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
13761370
})
13771371
return n, int(o.msg.Control.Len), err
13781372
}
@@ -1396,8 +1390,8 @@ func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (in
13961390
len := sockaddrInet4ToRaw(o.rsa, sa)
13971391
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
13981392
o.msg.Namelen = len
1399-
n, err := execIO(o, func(o *operation) error {
1400-
return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
1393+
n, err := fd.execIO(o, func(o *operation) error {
1394+
return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
14011395
})
14021396
return n, int(o.msg.Control.Len), err
14031397
}
@@ -1421,8 +1415,8 @@ func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (in
14211415
len := sockaddrInet6ToRaw(o.rsa, sa)
14221416
o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
14231417
o.msg.Namelen = len
1424-
n, err := execIO(o, func(o *operation) error {
1425-
return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
1418+
n, err := fd.execIO(o, func(o *operation) error {
1419+
return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
14261420
})
14271421
return n, int(o.msg.Control.Len), err
14281422
}

src/internal/poll/sendfile_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func SendFile(fd *FD, src uintptr, size int64) (written int64, err error, handle
7474
o.o.Offset = uint32(off)
7575
o.o.OffsetHigh = uint32(off >> 32)
7676

77-
n, err := execIO(o, func(o *operation) error {
77+
n, err := fd.execIO(o, func(o *operation) error {
7878
o.qty = uint32(chunkSize)
79-
return syscall.TransmitFile(o.fd.Sysfd, o.handle, o.qty, 0, &o.o, nil, syscall.TF_WRITE_BEHIND)
79+
return syscall.TransmitFile(fd.Sysfd, o.handle, o.qty, 0, &o.o, nil, syscall.TF_WRITE_BEHIND)
8080
})
8181
if err != nil {
8282
return written, err, written > 0

0 commit comments

Comments
 (0)