performance: precompile regex before iterating

This commit is contained in:
Dave Clendenan 2016-11-28 14:47:38 -08:00
parent 473c3448ab
commit 8161d932a1
1 changed files with 6 additions and 4 deletions

View File

@ -96,6 +96,11 @@ func getCaller() (method string) {
// Restrict the lookback to 25 frames - if it's further than that, report UNKNOWN // Restrict the lookback to 25 frames - if it's further than that, report UNKNOWN
pcs := make([]uintptr, 25) pcs := make([]uintptr, 25)
matchesLogrus, err := regexp.Compile("logrus.*")
if err != nil {
return "CALLER_LOOKUP_FAILED"
}
// the first non-logrus caller is at least three frames away // the first non-logrus caller is at least three frames away
depth := runtime.Callers(3, pcs) depth := runtime.Callers(3, pcs)
for i := 0; i < depth; i++ { for i := 0; i < depth; i++ {
@ -105,10 +110,7 @@ func getCaller() (method string) {
fullFuncName = fullFuncName[idx:] fullFuncName = fullFuncName[idx:]
} }
matched, err := regexp.MatchString("logrus.*", fullFuncName) matched := matchesLogrus.MatchString(fullFuncName)
if err != nil {
return "CALLER_LOOKUP_FAILED"
}
// If the caller isn't part of logrus, we're done // If the caller isn't part of logrus, we're done
if !matched { if !matched {