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

Improve performance of lexers.Get function #351

Merged

Conversation

igor-drozdov
Copy link
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
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