From: qmuntal Date: Wed, 2 Jul 2025 09:05:47 +0000 (+0200) Subject: internal/poll: remove fd field from Windows' poll.operation X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f7432e0230eef9f40117a901bc71e09db6432033;p=gostls13.git 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 Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI Auto-Submit: Michael Knyszek --- diff --git a/src/internal/poll/fd_windows.go b/src/internal/poll/fd_windows.go index acc2ab0c6e..f052a2a016 100644 --- a/src/internal/poll/fd_windows.go +++ b/src/internal/poll/fd_windows.go @@ -77,7 +77,6 @@ type operation struct { mode int32 // fields used only by net package - fd *FD buf syscall.WSABuf msg windows.WSAMsg sa syscall.Sockaddr @@ -105,8 +104,8 @@ func (o *operation) close() { } } -func (o *operation) overlapped() *syscall.Overlapped { - if o.fd.isBlocking { +func (fd *FD) overlapped(o *operation) *syscall.Overlapped { + if fd.isBlocking { // Don't return the overlapped object if the file handle // doesn't use overlapped I/O. It could be used, but // that would then use the file pointer stored in the @@ -171,11 +170,10 @@ func (o *operation) InitMsg(p []byte, oob []byte) { } // waitIO waits for the IO operation o to complete. -func waitIO(o *operation) error { - if o.fd.isBlocking { +func (fd *FD) waitIO(o *operation) error { + if fd.isBlocking { panic("can't wait on blocking operations") } - fd := o.fd if !fd.pollable() { // The overlapped handle is not added to the runtime poller, // the only way to wait for the IO to complete is block until @@ -195,8 +193,7 @@ func waitIO(o *operation) error { } // cancelIO cancels the IO operation o and waits for it to complete. -func cancelIO(o *operation) { - fd := o.fd +func (fd *FD) cancelIO(o *operation) { if !fd.pollable() { return } @@ -214,8 +211,7 @@ func cancelIO(o *operation) { // It supports both synchronous and asynchronous IO. // o.qty and o.flags are set to zero before calling submit // to avoid reusing the values from a previous call. -func execIO(o *operation, submit func(o *operation) error) (int, error) { - fd := o.fd +func (fd *FD) execIO(o *operation, submit func(o *operation) error) (int, error) { // Notify runtime netpoll about starting IO. err := fd.pd.prepare(int(o.mode), fd.isFile) if err != nil { @@ -234,13 +230,13 @@ func execIO(o *operation, submit func(o *operation) error) (int, error) { var waitErr error // Blocking operations shouldn't return ERROR_IO_PENDING. // Continue without waiting if that happens. - if !o.fd.isBlocking && (err == syscall.ERROR_IO_PENDING || (err == nil && !o.fd.skipSyncNotif)) { + if !fd.isBlocking && (err == syscall.ERROR_IO_PENDING || (err == nil && !fd.skipSyncNotif)) { // IO started asynchronously or completed synchronously but // a sync notification is required. Wait for it to complete. - waitErr = waitIO(o) + waitErr = fd.waitIO(o) if waitErr != nil { // IO interrupted by "close" or "timeout". - cancelIO(o) + fd.cancelIO(o) // We issued a cancellation request, but the IO operation may still succeeded // before the cancellation request runs. } @@ -399,8 +395,6 @@ func (fd *FD) Init(net string, pollable bool) error { fd.isBlocking = !pollable fd.rop.mode = 'r' fd.wop.mode = 'w' - fd.rop.fd = fd - fd.wop.fd = fd // It is safe to add overlapped handles that also perform I/O // outside of the runtime poller. The runtime poller will ignore @@ -514,8 +508,8 @@ func (fd *FD) Read(buf []byte) (int, error) { case kindFile, kindPipe: o := &fd.rop o.InitBuf(buf) - n, err = execIO(o, func(o *operation) error { - return syscall.ReadFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, o.overlapped()) + n, err = fd.execIO(o, func(o *operation) error { + return syscall.ReadFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, fd.overlapped(o)) }) fd.addOffset(n) switch err { @@ -530,8 +524,8 @@ func (fd *FD) Read(buf []byte) (int, error) { case kindNet: o := &fd.rop o.InitBuf(buf) - n, err = execIO(o, func(o *operation) error { - return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) + n, err = fd.execIO(o, func(o *operation) error { + return syscall.WSARecv(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) }) if race.Enabled { race.Acquire(unsafe.Pointer(&ioSync)) @@ -645,8 +639,8 @@ func (fd *FD) Pread(b []byte, off int64) (int, error) { o := &fd.rop o.InitBuf(b) fd.setOffset(off) - n, err := execIO(o, func(o *operation) error { - return syscall.ReadFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o) + n, err := fd.execIO(o, func(o *operation) error { + return syscall.ReadFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o) }) if err == syscall.ERROR_HANDLE_EOF { err = io.EOF @@ -671,12 +665,12 @@ func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) { defer fd.readUnlock() o := &fd.rop o.InitBuf(buf) - n, err := execIO(o, func(o *operation) error { + n, err := fd.execIO(o, func(o *operation) error { if o.rsa == nil { o.rsa = new(syscall.RawSockaddrAny) } o.rsan = int32(unsafe.Sizeof(*o.rsa)) - return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) + return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) }) err = fd.eofError(n, err) if err != nil { @@ -700,12 +694,12 @@ func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) defer fd.readUnlock() o := &fd.rop o.InitBuf(buf) - n, err := execIO(o, func(o *operation) error { + n, err := fd.execIO(o, func(o *operation) error { if o.rsa == nil { o.rsa = new(syscall.RawSockaddrAny) } o.rsan = int32(unsafe.Sizeof(*o.rsa)) - return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) + return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) }) err = fd.eofError(n, err) if err != nil { @@ -729,12 +723,12 @@ func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) defer fd.readUnlock() o := &fd.rop o.InitBuf(buf) - n, err := execIO(o, func(o *operation) error { + n, err := fd.execIO(o, func(o *operation) error { if o.rsa == nil { o.rsa = new(syscall.RawSockaddrAny) } o.rsan = int32(unsafe.Sizeof(*o.rsa)) - return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) + return syscall.WSARecvFrom(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil) }) err = fd.eofError(n, err) if err != nil { @@ -770,8 +764,8 @@ func (fd *FD) Write(buf []byte) (int, error) { case kindPipe, kindFile: o := &fd.wop o.InitBuf(b) - n, err = execIO(o, func(o *operation) error { - return syscall.WriteFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, o.overlapped()) + n, err = fd.execIO(o, func(o *operation) error { + return syscall.WriteFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, fd.overlapped(o)) }) fd.addOffset(n) case kindNet: @@ -780,8 +774,8 @@ func (fd *FD) Write(buf []byte) (int, error) { } o := &fd.wop o.InitBuf(b) - n, err = execIO(o, func(o *operation) error { - return syscall.WSASend(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil) + n, err = fd.execIO(o, func(o *operation) error { + return syscall.WSASend(fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil) }) } ntotal += n @@ -869,8 +863,8 @@ func (fd *FD) Pwrite(buf []byte, off int64) (int, error) { o := &fd.wop o.InitBuf(b) fd.setOffset(off + int64(ntotal)) - n, err := execIO(o, func(o *operation) error { - return syscall.WriteFile(o.fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o) + n, err := fd.execIO(o, func(o *operation) error { + return syscall.WriteFile(fd.Sysfd, unsafe.Slice(o.buf.Buf, o.buf.Len), &o.qty, &o.o) }) if n > 0 { ntotal += n @@ -898,8 +892,8 @@ func (fd *FD) Writev(buf *[][]byte) (int64, error) { } o := &fd.wop o.InitBufs(buf) - n, err := execIO(o, func(o *operation) error { - return syscall.WSASend(o.fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return syscall.WSASend(fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil) }) o.ClearBufs() TestHookDidWritev(n) @@ -919,8 +913,8 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) { o := &fd.wop o.InitBuf(buf) o.sa = sa - n, err := execIO(o, func(o *operation) error { - return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return syscall.WSASendto(fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) }) return n, err } @@ -934,8 +928,8 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) { o := &fd.wop o.InitBuf(b) o.sa = sa - n, err := execIO(o, func(o *operation) error { - return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return syscall.WSASendto(fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil) }) ntotal += int(n) if err != nil { @@ -957,8 +951,8 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) // handle zero-byte payload o := &fd.wop o.InitBuf(buf) - n, err := execIO(o, func(o *operation) error { - return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendtoInet4(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) }) return n, err } @@ -971,8 +965,8 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) } o := &fd.wop o.InitBuf(b) - n, err := execIO(o, func(o *operation) error { - return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendtoInet4(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil) }) ntotal += int(n) if err != nil { @@ -994,8 +988,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) // handle zero-byte payload o := &fd.wop o.InitBuf(buf) - n, err := execIO(o, func(o *operation) error { - return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendtoInet6(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) }) return n, err } @@ -1008,8 +1002,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) } o := &fd.wop o.InitBuf(b) - n, err := execIO(o, func(o *operation) error { - return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendtoInet6(fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil) }) ntotal += int(n) if err != nil { @@ -1026,8 +1020,8 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) func (fd *FD) ConnectEx(ra syscall.Sockaddr) error { o := &fd.wop o.sa = ra - _, err := execIO(o, func(o *operation) error { - return ConnectExFunc(o.fd.Sysfd, o.sa, nil, 0, nil, &o.o) + _, err := fd.execIO(o, func(o *operation) error { + return ConnectExFunc(fd.Sysfd, o.sa, nil, 0, nil, &o.o) }) return err } @@ -1036,8 +1030,8 @@ func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *ope // Submit accept request. o.handle = s o.rsan = int32(unsafe.Sizeof(rawsa[0])) - _, err := execIO(o, func(o *operation) error { - return AcceptFunc(o.fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o) + _, err := fd.execIO(o, func(o *operation) error { + return AcceptFunc(fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o) }) if err != nil { CloseFunc(s) @@ -1179,11 +1173,11 @@ func (fd *FD) RawRead(f func(uintptr) bool) error { // socket is readable. h/t https://stackoverflow.com/a/42019668/332798 o := &fd.rop o.InitBuf(nil) - _, err := execIO(o, func(o *operation) error { + _, err := fd.execIO(o, func(o *operation) error { if !fd.IsStream { o.flags |= windows.MSG_PEEK } - return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) + return syscall.WSARecv(fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) }) if err == windows.WSAEMSGSIZE { // 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 o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) o.msg.Flags = uint32(flags) - n, err := execIO(o, func(o *operation) error { - return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil) }) err = fd.eofError(n, err) var sa syscall.Sockaddr @@ -1308,8 +1302,8 @@ func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.Sockadd o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) o.msg.Flags = uint32(flags) - n, err := execIO(o, func(o *operation) error { - return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil) }) err = fd.eofError(n, err) if err == nil { @@ -1337,8 +1331,8 @@ func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.Sockadd o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa)) o.msg.Flags = uint32(flags) - n, err := execIO(o, func(o *operation) error { - return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSARecvMsg(fd.Sysfd, &o.msg, &o.qty, &o.o, nil) }) err = fd.eofError(n, err) if err == nil { @@ -1371,8 +1365,8 @@ func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, err o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = len } - n, err := execIO(o, func(o *operation) error { - return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) }) return n, int(o.msg.Control.Len), err } @@ -1396,8 +1390,8 @@ func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (in len := sockaddrInet4ToRaw(o.rsa, sa) o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = len - n, err := execIO(o, func(o *operation) error { - return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) }) return n, int(o.msg.Control.Len), err } @@ -1421,8 +1415,8 @@ func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (in len := sockaddrInet6ToRaw(o.rsa, sa) o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa)) o.msg.Namelen = len - n, err := execIO(o, func(o *operation) error { - return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) + n, err := fd.execIO(o, func(o *operation) error { + return windows.WSASendMsg(fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil) }) return n, int(o.msg.Control.Len), err } diff --git a/src/internal/poll/sendfile_windows.go b/src/internal/poll/sendfile_windows.go index d72bcd5871..d1cc04069b 100644 --- a/src/internal/poll/sendfile_windows.go +++ b/src/internal/poll/sendfile_windows.go @@ -74,9 +74,9 @@ func SendFile(fd *FD, src uintptr, size int64) (written int64, err error, handle o.o.Offset = uint32(off) o.o.OffsetHigh = uint32(off >> 32) - n, err := execIO(o, func(o *operation) error { + n, err := fd.execIO(o, func(o *operation) error { o.qty = uint32(chunkSize) - return syscall.TransmitFile(o.fd.Sysfd, o.handle, o.qty, 0, &o.o, nil, syscall.TF_WRITE_BEHIND) + return syscall.TransmitFile(fd.Sysfd, o.handle, o.qty, 0, &o.o, nil, syscall.TF_WRITE_BEHIND) }) if err != nil { return written, err, written > 0