Merge pull request #217 from claytono/emn-optimization

Convert escapeMetricName to use strings.Builder
This commit is contained in:
Matthias Rampke 2019-05-17 13:38:09 +00:00 committed by GitHub
commit 27d9273107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -364,32 +364,56 @@ type Exporter struct {
// Replace invalid characters in the metric name with "_" // Replace invalid characters in the metric name with "_"
// Valid characters are a-z, A-Z, 0-9, and _ // Valid characters are a-z, A-Z, 0-9, and _
func escapeMetricName(metricName string) string { func escapeMetricName(metricName string) string {
// If a metric starts with a digit, prepend an underscore. metricLen := len(metricName)
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' { if metricLen == 0 {
metricName = "_" + metricName return ""
} }
// this is an character replacement method optimized for this limited escaped := false
var sb strings.Builder
// If a metric starts with a digit, allocate the memory and prepend an
// underscore.
if metricName[0] >= '0' && metricName[0] <= '9' {
escaped = true
sb.Grow(metricLen + 1)
sb.WriteByte('_')
}
// This is an character replacement method optimized for this limited
// use case. It is much faster than using a regex. // use case. It is much faster than using a regex.
out := make([]byte, len(metricName)) offset := 0
j := 0 for i, c := range metricName {
for _, c := range metricName { // Seek forward, skipping valid characters until we find one that needs
// check if the rune is valid for a metric name // to be replaced, then add all the characters we've seen so far to the
// and replace it if it is not. // string.Builder.
// As only certain ASCII characters are valid in metric names, if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
// we can use a byte. (c >= '0' && c <= '9') || (c == '_') {
if (c >= 'a' && c <= 'z') || // Character is valid, so skip over it without doing anything.
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') {
out[j] = byte(c)
} else { } else {
out[j] = byte('_') if !escaped {
// Up until now we've been lazy and avoided actually allocating
// memory. Unfortunately we've now determined this string needs
// escaping, so allocate the buffer for the whole string.
escaped = true
sb.Grow(metricLen)
}
sb.WriteString(metricName[offset:i])
offset = i + utf8.RuneLen(c)
sb.WriteByte('_')
} }
j++
} }
return string(out[:j]) if !escaped {
// This is the happy path where nothing had to be escaped, so we can
// avoid doing anything.
return metricName
}
if offset < metricLen {
sb.WriteString(metricName[offset:])
}
return sb.String()
} }
// Listen handles all events sent to the given channel sequentially. It // Listen handles all events sent to the given channel sequentially. It