Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up git diff highlight generation #16180

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Speed up git diff highlight generation
typeless committed Jun 17, 2021
commit 9bff2e27367d36fbf308bbbac2a5b88014b7ff6c
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/hashicorp/go-version v1.3.1
github.com/hashicorp/golang-lru v0.5.1
github.com/huandu/xstrings v1.3.2
github.com/issue9/identicon v1.2.0
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -593,6 +593,7 @@ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
18 changes: 18 additions & 0 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
@@ -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))
23 changes: 23 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

223 changes: 223 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/2q.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

362 changes: 362 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/LICENSE

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/README.md
257 changes: 257 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/arc.go
21 changes: 21 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/doc.go
1 change: 1 addition & 0 deletions vendor/github.com/hashicorp/golang-lru/go.mod
116 changes: 116 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/lru.go
161 changes: 161 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/simplelru/lru.go
36 changes: 36 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go
4 changes: 4 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -467,6 +467,10 @@ github.com/hashicorp/go-retryablehttp
# github.com/hashicorp/go-version v1.3.1 => github.com/6543/go-version v1.3.1
## explicit
github.com/hashicorp/go-version
# github.com/hashicorp/golang-lru v0.5.1
## explicit
github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl
github.com/hashicorp/hcl/hcl/ast