Skip to content

Commit

Permalink
Split strings explicitly in ParseNodeID and ParseEndpointNodeID to sa…
Browse files Browse the repository at this point in the history
…ve allocations

These routines are called a lot, so this change reduces garbage collection
  • Loading branch information
bboreham committed Nov 14, 2017
1 parent c5bdebd commit 4f6cccb
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions report/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,25 @@ func ParseOverlayNodeID(id string) (overlayPrefix string, peerName string) {
// ParseNodeID produces the host ID and remainder (typically an address) from
// a node ID. Note that hostID may be blank.
func ParseNodeID(nodeID string) (hostID string, remainder string, ok bool) {
fields := strings.SplitN(nodeID, ScopeDelim, 2)
if len(fields) != 2 {
first := strings.Index(nodeID, ScopeDelim)
if first == -1 {
return "", "", false
}
return fields[0], fields[1], true
return nodeID[:first], nodeID[first+1:], true
}

// ParseEndpointNodeID produces the scope, address, and port and remainder.
// Note that hostID may be blank.
// Note that scope may be blank.
func ParseEndpointNodeID(endpointNodeID string) (scope, address, port string, ok bool) {
fields := strings.SplitN(endpointNodeID, ScopeDelim, 3)
if len(fields) != 3 {
first := strings.Index(endpointNodeID, ScopeDelim)
if first == -1 {
return "", "", "", false
}

return fields[0], fields[1], fields[2], true
second := strings.Index(endpointNodeID[first+1:], ScopeDelim)
if second == -1 {
return "", "", "", false
}
return endpointNodeID[:first], endpointNodeID[first+1 : first+1+second], endpointNodeID[first+1+second+1:], true
}

// ParseAddressNodeID produces the host ID, address from an address node ID.
Expand Down

0 comments on commit 4f6cccb

Please sign in to comment.