diff options
author | Bryan C. Mills <bcmills@google.com> | 2017-05-13 00:01:50 -0400 |
---|---|---|
committer | Bryan Mills <bcmills@google.com> | 2017-08-17 18:14:16 +0000 |
commit | d5b0ec858b3760e93722c13958dea767ab8da34b (patch) | |
tree | 65f54ece8b1df00ed5d299f47ef1e7839a40ee34 /src/net/cgo_unix.go | |
parent | 6711fa70cecce261662c20613cc63eec0c21a16a (diff) | |
download | go-git-d5b0ec858b3760e93722c13958dea767ab8da34b.tar.gz |
{net,os/user,plugin}: eliminate unnecessary C round-trips
We're making two extra round-trips to C to malloc and free strings
that originate in Go and don't escape. Skip those round-trips by
allocating null-terminated slices in Go memory instead.
Change-Id: I9e4c5ad999a7924ba50b82293c52073ec75518be
Reviewed-on: https://go-review.googlesource.com/56530
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/net/cgo_unix.go')
-rw-r--r-- | src/net/cgo_unix.go | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/net/cgo_unix.go b/src/net/cgo_unix.go index d5173d68be..1baa01f036 100644 --- a/src/net/cgo_unix.go +++ b/src/net/cgo_unix.go @@ -12,7 +12,6 @@ package net #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> -#include <stdlib.h> #include <unistd.h> #include <string.h> */ @@ -95,15 +94,14 @@ func cgoLookupPort(ctx context.Context, network, service string) (port int, err } func cgoLookupServicePort(hints *C.struct_addrinfo, network, service string) (port int, err error) { - s := C.CString(service) - // Lowercase the service name in the C-allocated memory. - for i := 0; i < len(service); i++ { - bp := (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(s)) + uintptr(i))) - *bp = lowerASCII(*bp) + cservice := make([]byte, len(service)+1) + copy(cservice, service) + // Lowercase the C service name. + for i, b := range cservice[:len(service)] { + cservice[i] = lowerASCII(b) } var res *C.struct_addrinfo - defer C.free(unsafe.Pointer(s)) - gerrno, err := C.getaddrinfo(nil, s, hints, &res) + gerrno, err := C.getaddrinfo(nil, (*C.char)(unsafe.Pointer(&cservice[0])), hints, &res) if gerrno != 0 { switch gerrno { case C.EAI_SYSTEM: @@ -145,10 +143,10 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error) { hints.ai_flags = cgoAddrInfoFlags hints.ai_socktype = C.SOCK_STREAM - h := C.CString(name) - defer C.free(unsafe.Pointer(h)) + h := make([]byte, len(name)+1) + copy(h, name) var res *C.struct_addrinfo - gerrno, err := C.getaddrinfo(h, nil, &hints, &res) + gerrno, err := C.getaddrinfo((*C.char)(unsafe.Pointer(&h[0])), nil, &hints, &res) if gerrno != 0 { switch gerrno { case C.EAI_SYSTEM: |