Speed up git diff highlight generation (#16180)

Co-authored-by: Mura Li <typeless@users.noreply.github.com>
Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Mura Li 2021-06-17 22:55:16 +08:00 committed by GitHub
parent b3fbd37e99
commit 19dedc3fa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1249 additions and 0 deletions

View file

@ -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))