From 8161d932a1c23cea2db63cc7cccbc00e78de48f2 Mon Sep 17 00:00:00 2001 From: Dave Clendenan Date: Mon, 28 Nov 2016 14:47:38 -0800 Subject: [PATCH] performance: precompile regex before iterating --- entry.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/entry.go b/entry.go index 1e721ce..290232a 100644 --- a/entry.go +++ b/entry.go @@ -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 {