From 4f6cccbf8caeb08e9d4475fe64f231727e2a447d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 11 Nov 2017 21:35:31 +0000 Subject: [PATCH] Split strings explicitly in ParseNodeID and ParseEndpointNodeID to save allocations These routines are called a lot, so this change reduces garbage collection --- report/id.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/report/id.go b/report/id.go index b0a9aa3237..8d2cec26e1 100644 --- a/report/id.go +++ b/report/id.go @@ -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.