]> Cypherpunks repositories - gostls13.git/commitdiff
net: avoid allocation in setAddr
authorDave Cheney <dave@cheney.net>
Fri, 26 Oct 2012 08:41:21 +0000 (19:41 +1100)
committerDave Cheney <dave@cheney.net>
Fri, 26 Oct 2012 08:41:21 +0000 (19:41 +1100)
setAddr was showing up in profiles due to string concatenation construction the os.File name field. netFD.sysfile's Name() is never used, except in dup() so I believe it is safe to avoid this allocation.

R=mikioh.mikioh, rsc
CC=golang-dev
https://golang.org/cl/6742058

src/pkg/net/fd_unix.go

index e231c3e21290d39402a5d1007514bcc0ca4c92d3..72317426aa9770177fa5623728c4664769ebacaf 100644 (file)
@@ -312,14 +312,18 @@ func newFD(fd, family, sotype int, net string) (*netFD, error) {
 func (fd *netFD) setAddr(laddr, raddr Addr) {
        fd.laddr = laddr
        fd.raddr = raddr
+       fd.sysfile = os.NewFile(uintptr(fd.sysfd), fd.net)
+}
+
+func (fd *netFD) name() string {
        var ls, rs string
-       if laddr != nil {
-               ls = laddr.String()
+       if fd.laddr != nil {
+               ls = fd.laddr.String()
        }
-       if raddr != nil {
-               rs = raddr.String()
+       if fd.raddr != nil {
+               rs = fd.raddr.String()
        }
-       fd.sysfile = os.NewFile(uintptr(fd.sysfd), fd.net+":"+ls+"->"+rs)
+       return fd.net + ":" + ls + "->" + rs
 }
 
 func (fd *netFD) connect(ra syscall.Sockaddr) error {
@@ -660,7 +664,7 @@ func (fd *netFD) dup() (f *os.File, err error) {
                return nil, &OpError{"setnonblock", fd.net, fd.laddr, err}
        }
 
-       return os.NewFile(uintptr(ns), fd.sysfile.Name()), nil
+       return os.NewFile(uintptr(ns), fd.name()), nil
 }
 
 func closesocket(s int) error {