Skip to content

Commit

Permalink
Switched benchmarking to Divan + fixed spellcheck crashing issue + im…
Browse files Browse the repository at this point in the history
…proved web perf
  • Loading branch information
elijah-potter committed Jan 29, 2024
1 parent 2ebe258 commit 85c83f3
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 272 deletions.
294 changes: 48 additions & 246 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion harper-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ smallvec = "1.12.0"
thiserror = "1.0.56"

[dev-dependencies]
criterion = "0.5.1"
divan = "0.1.11"

[[bench]]
name = "parse_demo"
Expand Down
42 changes: 24 additions & 18 deletions harper-core/benches/parse_demo.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
use criterion::{criterion_group, criterion_main, Criterion};
use divan::{black_box, AllocProfiler, Bencher};
use harper_core::{Dictionary, Document, LintSet, Linter};

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

c.bench_function("parse_demo", |b| {
b.iter(|| {
let _document = Document::new_markdown(demo);
})
static DEMO: &str = include_str!("../../demo.md");

#[divan::bench]
fn parse_demo(bencher: Bencher) {
bencher.bench_local(|| {
let _document = Document::new_markdown(black_box(DEMO));
});
}

#[divan::bench]
fn create_lint_set(bencher: Bencher) {
let dictionary = Dictionary::new();

c.bench_function("create_lint_set", |b| {
b.iter(|| {
let _lint_set = LintSet::new().with_standard(dictionary.clone());
})
bencher.bench_local(|| {
let _lint_set = LintSet::new().with_standard(dictionary.clone());
});
}

#[divan::bench]
fn lint_demo(bencher: Bencher) {
let dictionary = Dictionary::new();
let mut lint_set = LintSet::new().with_standard(dictionary);
let document = Document::new_markdown(demo);
let document = Document::new_markdown(black_box(DEMO));

c.bench_function("lint_demo", |b| {
b.iter(|| {
lint_set.lint(&document);
})
bencher.bench_local(|| {
lint_set.lint(&document);
});
}

criterion_group!(benches, parse_demo);
criterion_main!(benches);
fn main() {
divan::main();
}
4 changes: 2 additions & 2 deletions harper-core/src/linting/lint_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Dictionary, Lint};
use crate::{Dictionary, Document, Lint};

use super::{spaces::Spaces, Linter};
use paste::paste;
Expand All @@ -14,7 +14,7 @@ pub struct LintSet {
}

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

for linter in &mut self.linters {
Expand Down
4 changes: 2 additions & 2 deletions harper-core/src/spell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ pub fn suggest_correct_spelling<'a>(
found.push(found_dist[a].0);
found.push(found_dist.remove(b).0);
if a < b {
found_dist.remove(a - 1);
found_dist.remove(b - 1);
} else {
found_dist.remove(a);
found_dist.remove(b);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions harper-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ wasm-bindgen = "0.2.90"
harper-core = { path = "../harper-core" }
serde = "1.0.195"
serde-wasm-bindgen = "0.6.3"
once_cell = "1.19.0"
10 changes: 7 additions & 3 deletions harper-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::sync::Mutex;

use harper_core::{Dictionary, Document, LintSet, Linter};
use once_cell::sync::Lazy;
use serde::Serialize;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

static LINTER: Lazy<Mutex<LintSet>> =
Lazy::new(|| Mutex::new(LintSet::new().with_standard(Dictionary::new())));

/// Create the serializer that preserves types across the JavaScript barrier
fn glue_serializer() -> serde_wasm_bindgen::Serializer {
serde_wasm_bindgen::Serializer::new().serialize_missing_as_null(true)
Expand All @@ -19,11 +25,9 @@ pub fn setup() {

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

let mut linter = LintSet::new().with_standard(dictionary);
let lints = linter.lint(&document);
let lints = LINTER.lock().unwrap().lint(&document);

lints
.into_iter()
Expand Down
3 changes: 3 additions & 0 deletions web/src/lib/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function spanContent(span: Span, source: string): string {
}

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

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

console.timeEnd('lintText');

return lints;
}

Expand Down

0 comments on commit 85c83f3

Please sign in to comment.