aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorMura Li <2606021+typeless@users.noreply.github.com>2021-06-17 22:55:16 +0800
committerGitHub <noreply@github.com>2021-06-17 10:55:16 -0400
commit19dedc3fa521fdb6456bad2b080bb1aaa6722b24 (patch)
tree2153d435ad225049884c5991ba55408498482a39 /modules
parentb3fbd37e992cf3f9f42f49818087c67d464000eb (diff)
downloadgitea-19dedc3fa521fdb6456bad2b080bb1aaa6722b24.tar.gz
gitea-19dedc3fa521fdb6456bad2b080bb1aaa6722b24.zip
Speed up git diff highlight generation (#16180)
Co-authored-by: Mura Li <typeless@users.noreply.github.com> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules')
-rw-r--r--modules/highlight/highlight.go18
1 files changed, 18 insertions, 0 deletions
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
})
}
@@ -74,10 +85,17 @@ func Code(fileName, code string) string {
}
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))