From 9ad8250a786bfe8814e3372da089f70213742386 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 26 Jul 2020 18:54:41 +0200 Subject: [PATCH] Reduce overhead of debug calls if no log is enabled In case no debug log is configured, then calls to debug.Log only incur the costs to check a single boolean flag making the call really cheap. --- internal/debug/debug.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 543755e25..3df2c2ef4 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -17,9 +17,10 @@ import ( ) var opts struct { - logger *log.Logger - funcs map[string]bool - files map[string]bool + isEnabled bool + logger *log.Logger + funcs map[string]bool + files map[string]bool } // make sure that all the initialization happens before the init() functions @@ -30,6 +31,12 @@ func initDebug() bool { initDebugLogger() initDebugTags() + if opts.logger == nil && len(opts.funcs) == 0 && len(opts.files) == 0 { + opts.isEnabled = false + return false + } + + opts.isEnabled = true fmt.Fprintf(os.Stderr, "debug enabled\n") return true @@ -173,6 +180,10 @@ func checkFilter(filter map[string]bool, key string) bool { // Log prints a message to the debug log (if debug is enabled). func Log(f string, args ...interface{}) { + if !opts.isEnabled { + return + } + fn, dir, file, line := getPosition() goroutine := goroutineNum()