Skip to content

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

Major performance improvements to the Chrome extension (#1417)

Elijah Potterfae8aaa9f5f8
diff --git a/harper-core/src/expr/fixed_phrase.rs b/harper-core/src/expr/fixed_phrase.rs
index 7cc417ed6..78a77d6fc 100644
--- a/harper-core/src/expr/fixed_phrase.rs
+++ b/harper-core/src/expr/fixed_phrase.rs
@@ -1,5 +1,5 @@
use crate::patterns::Word;
-use crate::{Document, Token, TokenKind};
+use crate::{Document, Span, Token, TokenKind};
use super::{Expr, SequenceExpr};
@@ -21,14 +21,14 @@ pub struct FixedPhrase {
}
impl FixedPhrase {
- /// Creates a [FixedPhrase] from a plain text string.
+ /// Creates a [`FixedPhrase`] from a plaintext string.
/// Uses plain English tokenization rules.
pub fn from_phrase(text: &str) -> Self {
let document = Document::new_plain_english_curated(text);
Self::from_document(&document)
}
- /// Creates a [FixedPhrase] from a pre-tokenized document.
+ /// Creates a [`FixedPhrase`] from a pre-tokenized document.
/// Allows custom tokenization by creating a `Document` first.
pub fn from_document(doc: &Document) -> Self {
let mut phrase = SequenceExpr::default();
@@ -62,7 +62,7 @@ impl FixedPhrase {
}
impl Expr for FixedPhrase {
- fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<crate::Span> {
+ fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {
self.inner.run(cursor, tokens, source)
}
}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index c8caba76d..2edffe9e3 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -9,7 +9,7 @@ use cached::proc_macro::cached;
use foldhash::quality::RandomState;
use hashbrown::HashMap;
use lru::LruCache;
-use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::a_part::APart;
use super::adjective_of_a::AdjectiveOfA;
@@ -95,13 +95,30 @@ use crate::linting::{closed_compounds, initialisms, phrase_corrections};
use crate::{CharString, Dialect, Document, TokenStringExt};
use crate::{Dictionary, MutableDictionary};
+fn ser_ordered<S>(map: &HashMap<String, Option<bool>>, ser: S) -> Result<S::Ok, S::Error>
+where
+ S: Serializer,
+{
+ let ordered: BTreeMap<_, _> = map.iter().map(|(k, v)| (k.clone(), *v)).collect();
+ ordered.serialize(ser)
+}
+
+fn de_hashbrown<'de, D>(de: D) -> Result<HashMap<String, Option<bool>>, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ let ordered: BTreeMap<String, Option<bool>> = BTreeMap::deserialize(de)?;
+ Ok(ordered.into_iter().collect())
+}
+
/// The configuration for a [`LintGroup`].
/// Each child linter can be enabled, disabled, or set to a curated value.
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
#[serde(transparent)]
pub struct LintGroupConfig {
- /// A `BTreeMap` so that the config has a stable ordering when written to disk.
- inner: BTreeMap<String, Option<bool>>,
+ /// We do this shenanigans with the [`BTreeMap`] to keep the serialized format consistent.
+ #[serde(serialize_with = "ser_ordered", deserialize_with = "de_hashbrown")]
+ inner: HashMap<String, Option<bool>>,
}
#[cached]
diff --git a/justfile b/justfile
index cb63ca2f8..476dd3b92 100644
--- a/justfile
+++ b/justfile
@@ -104,6 +104,16 @@ build-chrome-plugin: build-harperjs
pnpm install
pnpm zip-for-chrome
+# Start a development server for the Chrome extension.
+dev-chrome-plugin: build-harperjs
+ #! /bin/bash
+ set -eo pipefail
+
+ cd "{{justfile_directory()}}/packages/chrome-plugin"
+
+ pnpm install
+ pnpm dev
+
# Build the Firefox extension.
build-firefox-plugin: build-harperjs
#! /bin/bash
diff --git a/packages/chrome-plugin/src/LintFramework.ts b/packages/chrome-plugin/src/LintFramework.ts
index f08459470..e48d3d23c 100644
--- a/packages/chrome-plugin/src/LintFramework.ts
+++ b/packages/chrome-plugin/src/LintFramework.ts
@@ -1,7 +1,10 @@
+import { clone } from 'lodash-es';
+import { isBoxInScreen } from './Box';
import Highlights from './Highlights';
import PopupHandler from './PopupHandler';
import ProtocolClient from './ProtocolClient';
import computeLintBoxes from './computeLintBoxes';
+import { isVisible } from './domUtils';
/** Events on an input (any kind) that can trigger a re-render. */
const INPUT_EVENTS = ['focus', 'keyup', 'paste', 'change', 'scroll'];
@@ -12,8 +15,9 @@ const PAGE_EVENTS = ['resize', 'scroll'];
export default class LintFramework {
private highlights: Highlights;
private popupHandler: PopupHandler;
- private targets: Set<HTMLElement>;
+ private targets: Set<Node>;
private scrollableAncestors: Set<HTMLElement>;
+ private frameRequested = false;
/** The function to be called to re-render the highlights. This is a variable because it is used to register/deregister event listeners. */
private updateEventCallback: () => void;
@@ -39,36 +43,60 @@ export default class LintFramework {
this.attachWindowListeners();
}
- async update() {
- const boxes = [];
+ /** Returns the currents targets that are visible on-screen. */
+ onScreenTargets(): Node[] {
+ const onScreen = [];
for (const target of this.targets) {
- let text: string | null = null;
-
- if (!document.contains(target)) {
- this.targets.delete(target);
- continue;
- }
-
- if (target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement) {
- text = target.value;
- } else {
- text = target.textContent;
+ if (isVisible(target)) {
+ onScreen.push(target);
}
+ }
- if (text == null || text.length > 120000) {
- continue;
- }
+ return onScreen;
+ }
- const lints = await ProtocolClient.lint(text, window.location.hostname);
- boxes.push(...lints.flatMap((l) => computeLintBoxes(target, l)));
+ async update() {
+ // To avoid multiple redundant calls to try running at the same time.
+ if (this.frameRequested) {
+ return;
}
- this.highlights.renderLintBoxes(boxes);
- this.popupHandler.updateLintBoxes(boxes);
+ this.frameRequested = true;
+
+ const lintResults = await Promise.all(
+ this.onScreenTargets().map(async (target) => {
+ if (!document.contains(target)) {
+ this.targets.delete(target);
+ return { target: null as HTMLElement | null, lints: [] };
+ }
+
+ const text =
+ target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement
+ ? target.value
+ : target.textContent;
+
+ if (!text || text.length > 120000) {
+ return { target: null as HTMLElement | null, lints: [] };
+ }
+
+ const lints = await ProtocolClient.lint(text, window.location.hostname);
+ return { target: target as HTMLElement, lints };
+ }),
+ );
+
+ requestAnimationFrame(() => {
+ const boxes = lintResults.flatMap(({ target, lints }) =>
+ target ? lints.flatMap((l) => computeLintBoxes(target, l)) : [],
+ );
+ this.highlights.renderLintBoxes(boxes);
+ this.popupHandler.updateLintBoxes(boxes);
+
+ this.frameRequested = false;
+ });
}
- public async addTarget(target: HTMLElement) {
+ public async addTarget(target: Node) {
if (!this.targets.has(target)) {
this.targets.add(target);
this.update();
@@ -86,7 +114,7 @@ export default class LintFramework {
}
}
- private attachTargetListeners(target: HTMLElement) {
+ private attachTargetListeners(target: Node) {
for (const event of INPUT_EVENTS) {
target.addEventListener(event, this.updateEventCallback);
}
@@ -105,7 +133,7 @@ export default class LintFramework {
for (const el of scrollableAncestors) {
if (!this.scrollableAncestors.has(el)) {
this.scrollableAncestors.add(el);
- el.addEventListener('scroll', this.updateEventCallback);
+ el.addEventListener('scroll', this.updateEventCallback, { capture: true, passive: true });
}
}
}
@@ -133,7 +161,7 @@ export default class LintFramework {
* Returns all scrollable ancestor elements of a given element,
* ordered from nearest to furthest (ending with the page scroller).
*/
-function getScrollableAncestors(element: Element): Element[] {
+function getScrollableAncestors(element: Node): Element[] {
const scrollables: Element[] = [];
const root = document.scrollingElement || document.documentElement;
let parent = element.parentElement;
@@ -141,14 +169,9 @@ function getScrollableAncestors(element: Element): Element[] {
while (parent) {
const style = window.getComputedStyle(parent);
const { overflowY, overflowX } = style;
- // Vertical scroll check: overflow-y is scrollable and content overflows
- const canScrollY =
- (overflowY.includes('auto') || overflowY.includes('scroll')) &&
- parent.scrollHeight > parent.clientHeight;
- // Horizontal scroll check: overflow-x is scrollable and content overflows
- const canScrollX =
- (overflowX.includes('auto') || overflowX.includes('scroll')) &&
- parent.scrollWidth > parent.clientWidth;
+ const canScrollY = overflowY.includes('auto') || overflowY.includes('scroll');
+ const canScrollX = overflowX.includes('auto') || overflowX.includes('scroll');
+
if (canScrollY || canScrollX) {
scrollables.push(parent);
}
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index 8953be925..8c0f3cb21 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -3,8 +3,8 @@ import { LRUCache } from 'lru-cache';
import type { UnpackedLint } from './unpackLint';
export default class ProtocolClient {
- private static readonly lintCache = new LRUCache<string, UnpackedLint[]>({
- max: 500,
+ private static readonly lintCache = new LRUCache<string, Promise<any>>({
+ max: 5000,
ttl: 5_000,
});
@@ -14,11 +14,12 @@ export default class ProtocolClient {
public static async lint(text: string, domain: string): Promise<UnpackedLint[]> {
const key = this.cacheKey(text, domain);
- const cached = this.lintCache.get(key);
- if (cached) return cached;
- const resp = await chrome.runtime.sendMessage({ kind: 'lint', text, domain });
- this.lintCache.set(key, resp.lints);
- return resp.lints;
+ let p = this.lintCache.get(key);
+ if (!p) {
+ p = chrome.runtime.sendMessage({ kind: 'lint', text, domain }).then((r) => r.lints);
+ this.lintCache.set(key, p);
+ }
+ return p;
}
public static async getLintConfig(): Promise<LintConfig> {
diff --git a/packages/chrome-plugin/src/computeLintBoxes.ts b/packages/chrome-plugin/src/computeLintBoxes.ts
index 6cba6b7bd..712752ecd 100644
--- a/packages/chrome-plugin/src/computeLintBoxes.ts
+++ b/packages/chrome-plugin/src/computeLintBoxes.ts
@@ -1,3 +1,4 @@
+import type { Span } from 'harper.js';
import { type IgnorableLintBox, type LintBox, domRectToBox, isBottomEdgeInBox } from './Box';
import ProtocolClient from './ProtocolClient';
import TextFieldRange from './TextFieldRange';
@@ -16,14 +17,14 @@ function isFormEl(el: HTMLElement): el is HTMLTextAreaElement | HTMLInputElement
}
export default function computeLintBoxes(el: HTMLElement, lint: UnpackedLint): IgnorableLintBox[] {
- let range: Range | TextFieldRange;
+ let range: Range | TextFieldRange | null = null;
let text: string | null = null;
if (isFormEl(el)) {
range = new TextFieldRange(el, lint.span.start, lint.span.end);
text = el.value;
} else {
- range = getRangeForTextSpan(el, lint.span);
+ range = getRangeForTextSpan(el, lint.span as Span);
}
const targetRects = range.getClientRects();
diff --git a/packages/chrome-plugin/src/contentScript/index.ts b/packages/chrome-plugin/src/contentScript/index.ts
index 7ea330f1c..ec680816a 100644
--- a/packages/chrome-plugin/src/contentScript/index.ts
+++ b/packages/chrome-plugin/src/contentScript/index.ts
@@ -1,7 +1,7 @@
import '@webcomponents/custom-elements';
import $ from 'jquery';
import LintFramework from '../LintFramework';
-import { leafNodes } from '../domUtils';
+import { isVisible, leafNodes } from '../domUtils';
const fw = new LintFramework();
@@ -26,7 +26,11 @@ function scan() {
continue;
}
- fw.addTarget(leaf as HTMLElement);
+ if (!isVisible(leaf)) {
+ continue;
+ }
+
+ fw.addTarget(leaf);
}
});
}
diff --git a/packages/chrome-plugin/src/domUtils.ts b/packages/chrome-plugin/src/domUtils.ts
index a2c906ca0..dfab717c3 100644
--- a/packages/chrome-plugin/src/domUtils.ts
+++ b/packages/chrome-plugin/src/domUtils.ts
@@ -1,4 +1,5 @@
import type { Span } from 'harper.js';
+import { isBoxInScreen } from './Box';
/**
* Turn a `NodeList` into a normal JavaScript array.
@@ -86,3 +87,27 @@ export function getRangeForTextSpan(target: Element, span: Span): Range | null {
return null;
}
+
+/** Check if an element is visible to the user.
+ * It is coarse and meant for performance improvements, not precision.*/
+export function isVisible(node: Node): boolean {
+ try {
+ if (node instanceof Element) {
+ return node.checkVisibility();
+ }
+
+ const range = document.createRange();
+ range.selectNode(node);
+ const rects = range.getClientRects();
+
+ for (const rect of rects) {
+ if (isBoxInScreen(rect)) {
+ return true;
+ }
+ }
+ } catch (e) {
+ return false;
+ }
+
+ return false;
+}
diff --git a/packages/web/src/routes/install-browser-extension/+page.svelte b/packages/web/src/routes/install-browser-extension/+page.svelte
index c1dfcd68f..3b3735c0d 100644
--- a/packages/web/src/routes/install-browser-extension/+page.svelte
+++ b/packages/web/src/routes/install-browser-extension/+page.svelte
@@ -39,13 +39,18 @@ let demoText =
Give It a Whirl!
</h2>
- <div class="md:col-span-2 relative">
- <Textarea
- rows={10}
- bind:value={demoText}
- class="w-full rounded-lg border border-base-300 focus:ring-4 focus:ring-primary/30 transition text-xl!"
- />
- </div>
+ <h3 class="text-center text-xl mb-4 max-w-xl mx-auto">
+ It may take a moment for the engine to start...
+ </h3>
+
+ <div class="md:col-span-2 relative">
+ <Textarea
+ rows={10}
+ bind:value={demoText}
+ class="w-full rounded-lg border border-base-300 focus:ring-4 focus:ring-primary/30 transition text-xl!"
+ />
+ </div>
+
</section>
</div>
</div>