- create a map of byte offsets to Kakoune ranges once, to avoid having to iterate the content for every highlight event - create a direct lookup for faces from the token index passed by the highlighter event, rather than performing a string map lookup each time - minimize allocations by pushing only a single string with sufficient capacity, rather than building small strings
74 lines
1.8 KiB
Rust
74 lines
1.8 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
|
use tree_sitter_highlight::{HighlightConfiguration, Highlighter};
|
|
|
|
use kak_highlight::daemon::worker::highlight_content;
|
|
|
|
fn make_tokens() -> HashMap<String, String> {
|
|
[
|
|
"constructor",
|
|
"function",
|
|
"function.macro",
|
|
"function.method",
|
|
"keyword",
|
|
"module",
|
|
"operator",
|
|
"punctuation",
|
|
"string",
|
|
"type",
|
|
"variable",
|
|
"variable.parameter",
|
|
]
|
|
.into_iter()
|
|
.fold(HashMap::new(), |mut map, val| {
|
|
map.insert(String::from(val), String::from(val));
|
|
map
|
|
})
|
|
}
|
|
|
|
fn run_benchmark(c: &mut Criterion, name: &str, content: &str) {
|
|
let timestamp = 0;
|
|
let tokens = make_tokens();
|
|
let names: Vec<_> = tokens.keys().collect();
|
|
|
|
let mut highlighter = Highlighter::new();
|
|
|
|
let mut highlight_config = HighlightConfiguration::new(
|
|
tree_sitter_rust::language(),
|
|
tree_sitter_rust::HIGHLIGHT_QUERY,
|
|
"",
|
|
tree_sitter_rust::LOCALS_QUERY,
|
|
)
|
|
.unwrap();
|
|
highlight_config.configure(&names);
|
|
|
|
c.bench_function(name, |b| {
|
|
b.iter(|| {
|
|
let _ = highlight_content(
|
|
&mut highlighter,
|
|
&highlight_config,
|
|
&tokens,
|
|
content.as_bytes(),
|
|
timestamp,
|
|
);
|
|
})
|
|
});
|
|
}
|
|
|
|
fn bench_main_rs(c: &mut Criterion) {
|
|
let content = r#"fn main() {
|
|
println!("Hello, world!");
|
|
}
|
|
"#;
|
|
|
|
run_benchmark(c, "main.rs", content);
|
|
}
|
|
|
|
fn bench_ast_rs(c: &mut Criterion) {
|
|
let content = include_str!("../tree-sitter-rust/examples/ast.rs");
|
|
run_benchmark(c, "ast.rs", content);
|
|
}
|
|
|
|
criterion_group!(benches, bench_main_rs, bench_ast_rs);
|
|
criterion_main!(benches);
|