From 22fd8ac32d2370b32807c5375feb98c048e362ad Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Mon, 20 Jul 2015 11:02:03 -0500 Subject: [PATCH] Speed up mungedoc TOC by pre-compiling a regex Brings the time to run the TOC over the docs directory from .7 seconds to .1 seconds --- cmd/mungedocs/toc.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/mungedocs/toc.go b/cmd/mungedocs/toc.go index ecb97001c7d..3febd6b0e56 100644 --- a/cmd/mungedocs/toc.go +++ b/cmd/mungedocs/toc.go @@ -57,12 +57,13 @@ func buildTOC(markdown []byte) ([]byte, error) { buffer.WriteString("\n") scanner := bufio.NewScanner(bytes.NewReader(markdown)) inBlockQuotes := false + blockQuoteRegex, err := regexp.Compile("^```") + if err != nil { + return nil, err + } for scanner.Scan() { line := scanner.Text() - match, err := regexp.Match("^```", []byte(line)) - if err != nil { - return nil, err - } + match := blockQuoteRegex.Match([]byte(line)) if match { inBlockQuotes = !inBlockQuotes continue