Skip to content

Commit 19dedc3

Browse files
typeless6543
andauthored
Speed up git diff highlight generation (#16180)
Co-authored-by: Mura Li <typeless@users.noreply.github.com> Co-authored-by: 6543 <6543@obermui.de>
1 parent b3fbd37 commit 19dedc3

File tree

14 files changed

+1249
-0
lines changed

14 files changed

+1249
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
6060
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
6161
github.com/hashicorp/go-version v1.3.1
62+
github.com/hashicorp/golang-lru v0.5.1
6263
github.com/huandu/xstrings v1.3.2
6364
github.com/issue9/identicon v1.2.0
6465
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
593593
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
594594
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
595595
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
596+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
596597
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
597598
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
598599
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=

modules/highlight/highlight.go

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package highlight
88
import (
99
"bufio"
1010
"bytes"
11+
"fmt"
1112
gohtml "html"
1213
"path/filepath"
1314
"strings"
@@ -20,6 +21,7 @@ import (
2021
"github.com/alecthomas/chroma/formatters/html"
2122
"github.com/alecthomas/chroma/lexers"
2223
"github.com/alecthomas/chroma/styles"
24+
lru "github.com/hashicorp/golang-lru"
2325
)
2426

2527
// don't index files larger than this many bytes for performance purposes
@@ -30,6 +32,8 @@ var (
3032
highlightMapping = map[string]string{}
3133

3234
once sync.Once
35+
36+
cache *lru.ARCCache
3337
)
3438

3539
// NewContext loads custom highlight map from local config
@@ -39,6 +43,13 @@ func NewContext() {
3943
for i := range keys {
4044
highlightMapping[keys[i].Name()] = keys[i].Value()
4145
}
46+
47+
// The size 512 is simply a conservative rule of thumb
48+
c, err := lru.NewARC(512)
49+
if err != nil {
50+
panic(fmt.Sprintf("failed to initialize LRU cache for highlighter: %s", err))
51+
}
52+
cache = c
4253
})
4354
}
4455

@@ -73,11 +84,18 @@ func Code(fileName, code string) string {
7384
lexer = lexers.Get(val)
7485
}
7586

87+
if lexer == nil {
88+
if l, ok := cache.Get(fileName); ok {
89+
lexer = l.(chroma.Lexer)
90+
}
91+
}
92+
7693
if lexer == nil {
7794
lexer = lexers.Match(fileName)
7895
if lexer == nil {
7996
lexer = lexers.Fallback
8097
}
98+
cache.Add(fileName, lexer)
8199
}
82100

83101
iterator, err := lexer.Tokenise(nil, string(code))

vendor/github.com/hashicorp/golang-lru/.gitignore

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/golang-lru/2q.go

+223
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)