diff options
author | Cameron Moore <moorereason@gmail.com> | 2020-10-16 13:27:09 -0500 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-10-16 21:14:02 +0200 |
commit | 14bce18a6c5aca8cb3e70a74d5045ca8b2358fee (patch) | |
tree | 02257562d2e0f093925049e2c45536e376ab215e | |
parent | 837e084bbe53e9e2e6cd471d2a3daf273a874d92 (diff) |
highlight: Avoid making unnecessary allocation
Avoid creating a local copy of the highlight configuration when no
options are passed.
Benchmarks of building the docs site:
name old time/op new time/op delta
DocsSite-2 1.94s ± 4% 1.93s ± 4% ~ (p=0.841 n=5+5)
name old alloc/op new alloc/op delta
DocsSite-2 666MB ± 1% 656MB ± 0% -1.48% (p=0.008 n=5+5)
name old allocs/op new allocs/op delta
DocsSite-2 8.85M ± 0% 8.76M ± 0% -1.04% (p=0.029 n=4+4)
-rw-r--r-- | markup/highlight/highlight.go | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/markup/highlight/highlight.go b/markup/highlight/highlight.go index 2bd77af0b..4727843cc 100644 --- a/markup/highlight/highlight.go +++ b/markup/highlight/highlight.go @@ -37,12 +37,15 @@ type Highlighter struct { } func (h Highlighter) Highlight(code, lang, optsStr string) (string, error) { + if optsStr == "" { + return highlight(code, lang, h.cfg) + } + cfg := h.cfg - if optsStr != "" { - if err := applyOptionsFromString(optsStr, &cfg); err != nil { - return "", err - } + if err := applyOptionsFromString(optsStr, &cfg); err != nil { + return "", err } + return highlight(code, lang, cfg) } |