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
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
depth := runtime.Callers(3, pcs)
for i := 0; i < depth; i++ {
@ -105,10 +110,7 @@ func getCaller() (method string) {
fullFuncName = fullFuncName[idx:]
}
matched, err := regexp.MatchString("logrus.*", fullFuncName)
if err != nil {
return "CALLER_LOOKUP_FAILED"
}
matched := matchesLogrus.MatchString(fullFuncName)
// If the caller isn't part of logrus, we're done
if !matched {