Skip to content

Commit 85c83f3

Browse files
committed
Switched benchmarking to Divan + fixed spellcheck crashing issue + improved web perf
1 parent 2ebe258 commit 85c83f3

File tree

8 files changed

+88
-272
lines changed

8 files changed

+88
-272
lines changed

Cargo.lock

Lines changed: 48 additions & 246 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

harper-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ smallvec = "1.12.0"
1919
thiserror = "1.0.56"
2020

2121
[dev-dependencies]
22-
criterion = "0.5.1"
22+
divan = "0.1.11"
2323

2424
[[bench]]
2525
name = "parse_demo"

harper-core/benches/parse_demo.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
use criterion::{criterion_group, criterion_main, Criterion};
1+
use divan::{black_box, AllocProfiler, Bencher};
22
use harper_core::{Dictionary, Document, LintSet, Linter};
33

4-
fn parse_demo(c: &mut Criterion) {
5-
let demo = include_str!("../../demo.md");
4+
#[global_allocator]
5+
static ALLOC: AllocProfiler = AllocProfiler::system();
66

7-
c.bench_function("parse_demo", |b| {
8-
b.iter(|| {
9-
let _document = Document::new_markdown(demo);
10-
})
7+
static DEMO: &str = include_str!("../../demo.md");
8+
9+
#[divan::bench]
10+
fn parse_demo(bencher: Bencher) {
11+
bencher.bench_local(|| {
12+
let _document = Document::new_markdown(black_box(DEMO));
1113
});
14+
}
1215

16+
#[divan::bench]
17+
fn create_lint_set(bencher: Bencher) {
1318
let dictionary = Dictionary::new();
1419

15-
c.bench_function("create_lint_set", |b| {
16-
b.iter(|| {
17-
let _lint_set = LintSet::new().with_standard(dictionary.clone());
18-
})
20+
bencher.bench_local(|| {
21+
let _lint_set = LintSet::new().with_standard(dictionary.clone());
1922
});
23+
}
2024

25+
#[divan::bench]
26+
fn lint_demo(bencher: Bencher) {
27+
let dictionary = Dictionary::new();
2128
let mut lint_set = LintSet::new().with_standard(dictionary);
22-
let document = Document::new_markdown(demo);
29+
let document = Document::new_markdown(black_box(DEMO));
2330

24-
c.bench_function("lint_demo", |b| {
25-
b.iter(|| {
26-
lint_set.lint(&document);
27-
})
31+
bencher.bench_local(|| {
32+
lint_set.lint(&document);
2833
});
2934
}
3035

31-
criterion_group!(benches, parse_demo);
32-
criterion_main!(benches);
36+
fn main() {
37+
divan::main();
38+
}

harper-core/src/linting/lint_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Dictionary, Lint};
1+
use crate::{Dictionary, Document, Lint};
22

33
use super::{spaces::Spaces, Linter};
44
use paste::paste;
@@ -14,7 +14,7 @@ pub struct LintSet {
1414
}
1515

1616
impl Linter for LintSet {
17-
fn lint(&mut self, document: &crate::Document) -> Vec<Lint> {
17+
fn lint(&mut self, document: &Document) -> Vec<Lint> {
1818
let mut lints = Vec::new();
1919

2020
for linter in &mut self.linters {

harper-core/src/spell/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub fn suggest_correct_spelling<'a>(
7878
found.push(found_dist[a].0);
7979
found.push(found_dist.remove(b).0);
8080
if a < b {
81-
found_dist.remove(a - 1);
81+
found_dist.remove(b - 1);
8282
} else {
83-
found_dist.remove(a);
83+
found_dist.remove(b);
8484
}
8585
}
8686
}

harper-wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ wasm-bindgen = "0.2.90"
2020
harper-core = { path = "../harper-core" }
2121
serde = "1.0.195"
2222
serde-wasm-bindgen = "0.6.3"
23+
once_cell = "1.19.0"

harper-wasm/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
use std::sync::Mutex;
2+
13
use harper_core::{Dictionary, Document, LintSet, Linter};
4+
use once_cell::sync::Lazy;
25
use serde::Serialize;
36
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
47

8+
static LINTER: Lazy<Mutex<LintSet>> =
9+
Lazy::new(|| Mutex::new(LintSet::new().with_standard(Dictionary::new())));
10+
511
/// Create the serializer that preserves types across the JavaScript barrier
612
fn glue_serializer() -> serde_wasm_bindgen::Serializer {
713
serde_wasm_bindgen::Serializer::new().serialize_missing_as_null(true)
@@ -19,11 +25,9 @@ pub fn setup() {
1925

2026
#[wasm_bindgen]
2127
pub fn lint(text: String) -> Vec<JsValue> {
22-
let dictionary = Dictionary::new();
2328
let document = Document::new_markdown(&text);
2429

25-
let mut linter = LintSet::new().with_standard(dictionary);
26-
let lints = linter.lint(&document);
30+
let lints = LINTER.lock().unwrap().lint(&document);
2731

2832
lints
2933
.into_iter()

web/src/lib/analysis.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function spanContent(span: Span, source: string): string {
8181
}
8282

8383
export async function lintText(text: string, useWasm = defaultUseWasm): Promise<Lint[]> {
84+
console.time('lintText');
8485
let lints;
8586

8687
if (useWasm) {
@@ -106,6 +107,8 @@ export async function lintText(text: string, useWasm = defaultUseWasm): Promise<
106107
// The `Underlines` component assumes the lints do not overlap.
107108
lints = removeOverlaps(lints);
108109

110+
console.timeEnd('lintText');
111+
109112
return lints;
110113
}
111114

0 commit comments

Comments
 (0)