Skip to content

Improve performance of lexers.Get function - #351

Merged
alecthomas merged 2 commits into
alecthomas:masterfrom
igor-drozdov:id-improve-performance-of-lexers-get
Apr 25, 2020
Merged

Improve performance of lexers.Get function#351
alecthomas merged 2 commits into
alecthomas:masterfrom
igor-drozdov:id-improve-performance-of-lexers-get

Conversation

@igor-drozdov

Copy link
Copy Markdown
Contributor

The following benchmark test:

func BenchmarkGet(b *testing.B) {
       for i := 0; i < b.N; i++ {
               lexers.Get("go")
       }
}

Shows the following results

go test -bench BenchmarkGet -run BenchmarkGet -v
goos: darwin
goarch: amd64
pkg: github.com/alecthomas/chroma/lexers
BenchmarkGet
BenchmarkGet-4   	    9336	    128457 ns/op
PASS
ok  	github.com/alecthomas/chroma/lexers	1.491s

Which is not so fast for a function that simply returns a lexer for a language. The reason is that we always try to find a lexer by a filename: https://github.com/alecthomas/chroma/blob/master/lexers/internal/api.go#L54. It has been done due to #94.

@alecthomas what do you think about returning a lexer if we found one by a provided name and only if we didn't find one we'd try to search by file extension? That would dramatically improve the performance:

goos: darwin
goarch: amd64
pkg: github.com/alecthomas/chroma/lexers
BenchmarkGet
BenchmarkGet-4   	40540636	        28.1 ns/op
PASS
ok  	github.com/alecthomas/chroma/lexers	1.329s

I've included the benchmark test just for demonstrating purposes

Look for a lexer by file extension only when it failed to find a
lexer by name
@alecthomas

Copy link
Copy Markdown
Owner

Seems fairly reasonable, and I can't imagine a situation where it could break existing code really.

@alecthomas
alecthomas merged commit 80f4853 into alecthomas:master Apr 25, 2020
@igor-drozdov
igor-drozdov deleted the id-improve-performance-of-lexers-get branch April 26, 2020 04:57
mrsdizzie pushed a commit to mrsdizzie/chroma that referenced this pull request Jul 15, 2020
* Benchmark test for lexers.Get function

* Improve performance of lexers.Get function

Look for a lexer by file extension only when it failed to find a
lexer by name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants