From 8aea9a00a838736189d4a9e381f916b0c3ceaf1d Mon Sep 17 00:00:00 2001 From: Cristian Staretu Date: Wed, 9 Jul 2014 18:50:38 +1000 Subject: [PATCH] syscall: NetlinkRIB, avoid allocation in loop NetlinkRIB is currently allocating a page sized slice of bytes in a for loop and it's also calling Getpagesize() in the same for loop. This CL changes NetlinkRIB to preallocate the page sized slice of bytes before reaching the for loop. This reduces memory allocations and lowers the number of calls to Getpagesize() to 1 per NetlinkRIB call. This CL reduces the allocated memory from 141.5 MB down to 52 MB in a test. LGTM=crawshaw, dave R=dave, dsymonds, crawshaw CC=bradfitz, dsymonds, golang-codereviews https://golang.org/cl/110920043 --- src/pkg/syscall/netlink_linux.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pkg/syscall/netlink_linux.go b/src/pkg/syscall/netlink_linux.go index 49550ea2f0..1b73dce827 100644 --- a/src/pkg/syscall/netlink_linux.go +++ b/src/pkg/syscall/netlink_linux.go @@ -64,9 +64,10 @@ func NetlinkRIB(proto, family int) ([]byte, error) { return nil, err } var tab []byte + rbNew := make([]byte, Getpagesize()) done: for { - rb := make([]byte, Getpagesize()) + rb := rbNew nr, _, err := Recvfrom(s, rb, 0) if err != nil { return nil, err -- 2.50.0