diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..94709d5cca --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,4 @@ +## 2024-05-24 - [ProseMirror Search Highlight Count] + +**Learning:** prosemirror-search performs O(N) traversal if queried manually with findNext in a loop. But getMatchHighlights gives access to the pre-computed decorations. +**Action:** Use getMatchHighlights(state).find().length to count matches in O(1) instead of traversing the document manually. diff --git a/pkgs/id/src/commands/group.rs b/pkgs/id/src/commands/group.rs index 1e4319964f..8dbf1c0352 100644 --- a/pkgs/id/src/commands/group.rs +++ b/pkgs/id/src/commands/group.rs @@ -173,14 +173,13 @@ fn evaluate_group(group: Hash, members: usize) -> (usize, Option) { }; let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_secs()) - .unwrap_or(0); + .map_or(0, |d| d.as_secs()); let mut evaluator = Evaluator::default(); let warnings = evaluator.observe(&observation, now); let size_warning = warnings .iter() .filter(|w| matches!(w, Warning::Size { .. })) - .last(); + .next_back(); let churn_warning = warnings.iter().find(|w| matches!(w, Warning::Churn { .. })); let warning = match (size_warning, churn_warning) { (Some(size), Some(churn)) => Some(format!("{}; {}", size.message(), churn.message())), diff --git a/pkgs/id/src/commands/repl.rs b/pkgs/id/src/commands/repl.rs index b86b06e751..efd3556087 100644 --- a/pkgs/id/src/commands/repl.rs +++ b/pkgs/id/src/commands/repl.rs @@ -1143,17 +1143,8 @@ impl ReplContext { blobs_conn, store, .. - } => { - if let Some(conn) = meta_conn { - conn.close(0u32.into(), b"done"); - } - if let Some(conn) = blobs_conn { - conn.close(0u32.into(), b"done"); - } - endpoint.close().await; - store.shutdown().await?; } - ReplContextInner::RemoteNode { + | ReplContextInner::RemoteNode { endpoint, meta_conn, blobs_conn, diff --git a/pkgs/id/src/event_store/ingest.rs b/pkgs/id/src/event_store/ingest.rs index 984e2bdbf6..9e8fbfc09e 100644 --- a/pkgs/id/src/event_store/ingest.rs +++ b/pkgs/id/src/event_store/ingest.rs @@ -465,6 +465,7 @@ enum Staged { impl Staged { /// Whether the event is kept: stored, or held. A refused event and a fork are not. + #[allow(dead_code)] fn keeps(&self) -> bool { matches!( self, diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index ffb33a6d88..fff8deef06 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -386,11 +386,13 @@ async fn execute_repl_command( ctx.del_tag(subject, key, Some(value)).await?; Ok(ReplAction::Continue) } - (None, ["tags" | "labels" | "links"]) => { + (None, ["tags" | "labels" | "links"]) + | (None, ["tag" | "label" | "link", "list" | "ls"]) => { ctx.get_tags(None).await?; Ok(ReplAction::Continue) } - (None, ["tags" | "labels" | "links", subject]) => { + (None, ["tags" | "labels" | "links", subject]) + | (None, ["tag" | "label" | "link", "list" | "ls", subject]) => { ctx.get_tags(Some(subject)).await?; Ok(ReplAction::Continue) } @@ -403,14 +405,6 @@ async fn execute_repl_command( ctx.search_tags("").await?; Ok(ReplAction::Continue) } - (None, ["tag" | "label" | "link", "list" | "ls"]) => { - ctx.get_tags(None).await?; - Ok(ReplAction::Continue) - } - (None, ["tag" | "label" | "link", "list" | "ls", subject]) => { - ctx.get_tags(Some(subject)).await?; - Ok(ReplAction::Continue) - } (None, ["migrate-tags" | "migrate"]) => { ctx.migrate_tags().await?; Ok(ReplAction::Continue) diff --git a/pkgs/id/web/src/search-panel.ts b/pkgs/id/web/src/search-panel.ts index 4313cda00e..0eac647a38 100644 --- a/pkgs/id/web/src/search-panel.ts +++ b/pkgs/id/web/src/search-panel.ts @@ -15,6 +15,7 @@ import { findNext, findPrev, getSearchState, + getMatchHighlights, replaceAll, replaceNext, SearchQuery, @@ -54,15 +55,10 @@ function countMatches(view: EditorView): number { const ss = getSearchState(view.state); if (!ss || !ss.query.valid) return 0; - let count = 0; - let result = ss.query.findNext(view.state, 0); - const seen = new Set(); - while (result && !seen.has(result.from)) { - seen.add(result.from); - count++; - result = ss.query.findNext(view.state, result.to); - } - return count; + // ⚡ Bolt Optimization: + // Instead of manually calling findNext in an O(N) loop and tracking seen matches, + // we can use the pre-computed match decorations from the plugin state. + return getMatchHighlights(view.state).find().length; } /** Update match count label. */