Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

Switched benchmarking to Divan + fixed spellcheck crashing issue + improved web perf

Elijah Potter85c83f395ef4
diff --git a/Cargo.lock b/Cargo.lock
index 307f0962a..ff26818fd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -50,12 +50,6 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
-[[package]]
-name = "anes"
-version = "0.1.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
-
[[package]]
name = "anstream"
version = "0.6.11"
@@ -221,12 +215,6 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
-[[package]]
-name = "cast"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
-
[[package]]
name = "cc"
version = "1.0.83"
@@ -242,33 +230,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
-[[package]]
-name = "ciborium"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
-dependencies = [
- "ciborium-io",
- "ciborium-ll",
- "serde",
-]
-
-[[package]]
-name = "ciborium-io"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
-
-[[package]]
-name = "ciborium-ll"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
-dependencies = [
- "ciborium-io",
- "half",
-]
-
[[package]]
name = "clap"
version = "4.4.18"
@@ -289,6 +250,7 @@ dependencies = [
"anstyle",
"clap_lex",
"strsim",
+ "terminal_size",
]
[[package]]
@@ -315,6 +277,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
+[[package]]
+name = "condtype"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf0a07a401f374238ab8e2f11a104d2851bf9ce711ec69804834de8af45c7af"
+
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
@@ -326,83 +294,41 @@ dependencies = [
]
[[package]]
-name = "criterion"
-version = "0.5.1"
+name = "dashmap"
+version = "5.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
+checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
dependencies = [
- "anes",
- "cast",
- "ciborium",
- "clap",
- "criterion-plot",
- "is-terminal",
- "itertools 0.10.5",
- "num-traits",
+ "cfg-if",
+ "hashbrown",
+ "lock_api",
"once_cell",
- "oorandom",
- "plotters",
- "rayon",
- "regex",
- "serde",
- "serde_derive",
- "serde_json",
- "tinytemplate",
- "walkdir",
-]
-
-[[package]]
-name = "criterion-plot"
-version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
-dependencies = [
- "cast",
- "itertools 0.10.5",
-]
-
-[[package]]
-name = "crossbeam-deque"
-version = "0.8.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
-dependencies = [
- "crossbeam-epoch",
- "crossbeam-utils",
+ "parking_lot_core",
]
[[package]]
-name = "crossbeam-epoch"
-version = "0.9.18"
+name = "divan"
+version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
+checksum = "5398159ee27f2b123d89b856bad61725442f37df5fb98c30cd570c318d594aee"
dependencies = [
- "crossbeam-utils",
+ "cfg-if",
+ "clap",
+ "condtype",
+ "divan-macros",
+ "libc",
+ "regex-lite",
]
[[package]]
-name = "crossbeam-utils"
-version = "0.8.19"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
-
-[[package]]
-name = "crunchy"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
-
-[[package]]
-name = "dashmap"
-version = "5.5.3"
+name = "divan-macros"
+version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
+checksum = "5092f66eb3563a01e85552731ae82c04c934ff4efd7ad1a0deae7b948f4b3ec4"
dependencies = [
- "cfg-if",
- "hashbrown",
- "lock_api",
- "once_cell",
- "parking_lot_core",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.48",
]
[[package]]
@@ -528,24 +454,14 @@ version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
-[[package]]
-name = "half"
-version = "2.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872"
-dependencies = [
- "cfg-if",
- "crunchy",
-]
-
[[package]]
name = "harper-core"
version = "0.5.3"
dependencies = [
- "criterion",
+ "divan",
"hashbrown",
"is-macro",
- "itertools 0.11.0",
+ "itertools",
"once_cell",
"paste",
"pulldown-cmark",
@@ -597,6 +513,7 @@ version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"harper-core",
+ "once_cell",
"serde",
"serde-wasm-bindgen",
"tracing",
@@ -705,26 +622,6 @@ dependencies = [
"syn 2.0.48",
]
-[[package]]
-name = "is-terminal"
-version = "0.4.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455"
-dependencies = [
- "hermit-abi",
- "rustix",
- "windows-sys 0.52.0",
-]
-
-[[package]]
-name = "itertools"
-version = "0.10.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
-dependencies = [
- "either",
-]
-
[[package]]
name = "itertools"
version = "0.11.0"
@@ -844,15 +741,6 @@ dependencies = [
"winapi",
]
-[[package]]
-name = "num-traits"
-version = "0.2.17"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
-dependencies = [
- "autocfg",
-]
-
[[package]]
name = "num_cpus"
version = "1.16.0"
@@ -878,12 +766,6 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
-[[package]]
-name = "oorandom"
-version = "11.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
-
[[package]]
name = "overload"
version = "0.1.1"
@@ -957,34 +839,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
-[[package]]
-name = "plotters"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45"
-dependencies = [
- "num-traits",
- "plotters-backend",
- "plotters-svg",
- "wasm-bindgen",
- "web-sys",
-]
-
-[[package]]
-name = "plotters-backend"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609"
-
-[[package]]
-name = "plotters-svg"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab"
-dependencies = [
- "plotters-backend",
-]
-
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@@ -1039,26 +893,6 @@ dependencies = [
"proc-macro2",
]
-[[package]]
-name = "rayon"
-version = "1.8.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051"
-dependencies = [
- "either",
- "rayon-core",
-]
-
-[[package]]
-name = "rayon-core"
-version = "1.12.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
-dependencies = [
- "crossbeam-deque",
- "crossbeam-utils",
-]
-
[[package]]
name = "redox_syscall"
version = "0.4.1"
@@ -1091,6 +925,12 @@ dependencies = [
"regex-syntax",
]
+[[package]]
+name = "regex-lite"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "30b661b2f27137bdbc16f00eda72866a92bb28af1753ffbd56744fb6e2e9cd8e"
+
[[package]]
name = "regex-syntax"
version = "0.8.2"
@@ -1128,15 +968,6 @@ version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
-[[package]]
-name = "same-file"
-version = "1.0.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
-dependencies = [
- "winapi-util",
-]
-
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -1295,6 +1126,16 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
+[[package]]
+name = "terminal_size"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
+dependencies = [
+ "rustix",
+ "windows-sys 0.48.0",
+]
+
[[package]]
name = "thiserror"
version = "1.0.56"
@@ -1325,16 +1166,6 @@ dependencies = [
"once_cell",
]
-[[package]]
-name = "tinytemplate"
-version = "1.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
-dependencies = [
- "serde",
- "serde_json",
-]
-
[[package]]
name = "tinyvec"
version = "1.6.0"
@@ -1727,16 +1558,6 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
-[[package]]
-name = "walkdir"
-version = "2.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
-dependencies = [
- "same-file",
- "winapi-util",
-]
-
[[package]]
name = "want"
version = "0.3.1"
@@ -1806,16 +1627,6 @@ version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b"
-[[package]]
-name = "web-sys"
-version = "0.3.67"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "58cd2333b6e0be7a39605f0e255892fd7418a682d8da8fe042fe25128794d2ed"
-dependencies = [
- "js-sys",
- "wasm-bindgen",
-]
-
[[package]]
name = "winapi"
version = "0.3.9"
@@ -1832,15 +1643,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
-[[package]]
-name = "winapi-util"
-version = "0.1.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
-dependencies = [
- "winapi",
-]
-
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index ced2ed0ed..a6410aec0 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -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"
diff --git a/harper-core/benches/parse_demo.rs b/harper-core/benches/parse_demo.rs
index da1c1fa49..3e436233d 100644
--- a/harper-core/benches/parse_demo.rs
+++ b/harper-core/benches/parse_demo.rs
@@ -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();
+}
diff --git a/harper-core/src/linting/lint_set.rs b/harper-core/src/linting/lint_set.rs
index 0feb02cd7..a4e8f789d 100644
--- a/harper-core/src/linting/lint_set.rs
+++ b/harper-core/src/linting/lint_set.rs
@@ -1,4 +1,4 @@
-use crate::{Dictionary, Lint};
+use crate::{Dictionary, Document, Lint};
use super::{spaces::Spaces, Linter};
use paste::paste;
@@ -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 {
diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rs
index d7fce8fdb..e45b5eae0 100644
--- a/harper-core/src/spell/mod.rs
+++ b/harper-core/src/spell/mod.rs
@@ -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);
}
}
}
diff --git a/harper-wasm/Cargo.toml b/harper-wasm/Cargo.toml
index 11554e347..763e6b124 100644
--- a/harper-wasm/Cargo.toml
+++ b/harper-wasm/Cargo.toml
@@ -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"
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 9cfe30621..0f5499786 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -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)
@@ -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()
diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.ts
index 374525b17..c1ab11dce 100644
--- a/web/src/lib/analysis.ts
+++ b/web/src/lib/analysis.ts
@@ -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) {
@@ -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;
}