From 19dedc3fa521fdb6456bad2b080bb1aaa6722b24 Mon Sep 17 00:00:00 2001 From: Mura Li <2606021+typeless@users.noreply.github.com> Date: Thu, 17 Jun 2021 22:55:16 +0800 Subject: Speed up git diff highlight generation (#16180) Co-authored-by: Mura Li Co-authored-by: 6543 <6543@obermui.de> --- modules/highlight/highlight.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'modules/highlight') diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index a46499691e..e22e9d5b32 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -8,6 +8,7 @@ package highlight import ( "bufio" "bytes" + "fmt" gohtml "html" "path/filepath" "strings" @@ -20,6 +21,7 @@ import ( "github.com/alecthomas/chroma/formatters/html" "github.com/alecthomas/chroma/lexers" "github.com/alecthomas/chroma/styles" + lru "github.com/hashicorp/golang-lru" ) // don't index files larger than this many bytes for performance purposes @@ -30,6 +32,8 @@ var ( highlightMapping = map[string]string{} once sync.Once + + cache *lru.ARCCache ) // NewContext loads custom highlight map from local config @@ -39,6 +43,13 @@ func NewContext() { for i := range keys { highlightMapping[keys[i].Name()] = keys[i].Value() } + + // The size 512 is simply a conservative rule of thumb + c, err := lru.NewARC(512) + if err != nil { + panic(fmt.Sprintf("failed to initialize LRU cache for highlighter: %s", err)) + } + cache = c }) } @@ -73,11 +84,18 @@ func Code(fileName, code string) string { lexer = lexers.Get(val) } + if lexer == nil { + if l, ok := cache.Get(fileName); ok { + lexer = l.(chroma.Lexer) + } + } + if lexer == nil { lexer = lexers.Match(fileName) if lexer == nil { lexer = lexers.Fallback } + cache.Add(fileName, lexer) } iterator, err := lexer.Tokenise(nil, string(code)) -- cgit v1.2.3