Skip to content

Commit

Permalink
Copy net.ParseIP and modify to save memory allocations
Browse files Browse the repository at this point in the history
Pass in a slice on the stack instead of allocating one on the heap:
reduces garbage, hence makes the program run faster

Also apply knowledge that critbitgo will do an append() with one extra
byte, so we do that allocation up-front too.  This is innocuous should
we stop using critbitgo or should its internals change.
  • Loading branch information
bboreham committed Nov 14, 2017
1 parent 4f6cccb commit 41e33ed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions render/id.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"net"
"strings"

"github.com/weaveworks/scope/probe/endpoint"
Expand Down Expand Up @@ -66,7 +65,8 @@ func externalNodeID(n report.Node, addr string, local report.Networks) (string,

// If the dstNodeAddr is not in a network local to this report, we emit an
// internet pseudoNode
if ip := net.ParseIP(addr); ip != nil && !local.Contains(ip) {
var into [5]byte // one extra byte to save a memory allocation in critbitgo
if ip := report.ParseIPb([]byte(addr), into[:4]); ip != nil && !local.Contains(ip) {
// emit one internet node for incoming, one for outgoing
if len(n.Adjacency) > 0 {
return IncomingInternetID, true
Expand Down
67 changes: 67 additions & 0 deletions report/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,70 @@ func commonIPv4PrefixLen(a, b net.IP) int {
}
return cpl
}

// ParseIP parses s as an IP address into a byte slice if supplied, returning the result.
// (mostly copied from net.ParseIP, modified to save memory allocations)
func ParseIPb(s []byte, into []byte) net.IP {
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return parseIPv4(s, into)
case ':':
return net.ParseIP(string(s)) // leave IPv6 to the original code since we don't see many of those
}
}
return nil
}

// Parse IPv4 address (d.d.d.d).
// (mostly copied from net.parseIPv4, modified to save memory allocations)
func parseIPv4(s []byte, into []byte) net.IP {
var p []byte
if len(into) >= net.IPv4len { // check if we can use the supplied slice
p = into[:net.IPv4len]
} else {
p = make([]byte, net.IPv4len)
}
for i := 0; i < net.IPv4len; i++ {
if len(s) == 0 {
// Missing octets.
return nil
}
if i > 0 {
if s[0] != '.' {
return nil
}
s = s[1:]
}
n, c, ok := dtoi(s)
if !ok || n > 0xFF {
return nil
}
s = s[c:]
p[i] = byte(n)
}
if len(s) != 0 {
return nil
}
return p
}

// Bigger than we need, not too big to worry about overflow
const big = 0xFFFFFF

// Decimal to integer.
// Returns number, characters consumed, success.
// (completely copied from net.dtoi, just because it wasn't exported)
func dtoi(s []byte) (n int, i int, ok bool) {
n = 0
for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
n = n*10 + int(s[i]-'0')
if n >= big {
return big, i, false
}
}
if i == 0 {
return 0, 0, false
}
return n, i, true
}

0 comments on commit 41e33ed

Please sign in to comment.