From 276c873f0dec2cc25f4c49c0d0f8849d53eb55e8 Mon Sep 17 00:00:00 2001 From: Conor Branagan Date: Thu, 13 Oct 2016 13:43:42 -0400 Subject: [PATCH] Fast duplication check in inodes processing. Instead of encoding a JSON string of each connection (non-trivial at high connection volumes) we can use the connTmp struct for map look-ups if we eliminate the unused `uids` field. Also switches to using the empty struct instead of bool for zero memory overhead. --- net/net_linux.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/net/net_linux.go b/net/net_linux.go index 0042c00..8905b13 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -281,7 +281,6 @@ type connTmp struct { laddr Addr raddr Addr status string - uids []int32 pid int32 boundPid int32 path string @@ -347,7 +346,7 @@ func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error } func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap) ([]ConnectionStat, error) { - dupCheckMap := make(map[string]bool) + dupCheckMap := make(map[connTmp]struct{}) var ret []ConnectionStat var err error @@ -367,13 +366,16 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode return nil, err } for _, c := range ls { + if _, ok := dupCheckMap[c]; ok { + continue + } + conn := ConnectionStat{ Fd: c.fd, Family: c.family, Type: c.sockType, Laddr: c.laddr, Raddr: c.raddr, - Uids: c.uids, Status: c.status, Pid: c.pid, } @@ -387,13 +389,8 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode proc := process{Pid: conn.Pid} conn.Uids, _ = proc.getUids() - // check duplicate using JSON format - json := conn.String() - _, exists := dupCheckMap[json] - if !exists { - ret = append(ret, conn) - dupCheckMap[json] = true - } + ret = append(ret, conn) + dupCheckMap[c] = struct{}{} } }