Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 2 additions & 3 deletions pkgs/id/src/commands/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ fn evaluate_group(group: Hash, members: usize) -> (usize, Option<String>) {
};
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())),
Expand Down
11 changes: 1 addition & 10 deletions pkgs/id/src/commands/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkgs/id/src/event_store/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 4 additions & 10 deletions pkgs/id/src/repl/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions pkgs/id/web/src/search-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
findNext,
findPrev,
getSearchState,
getMatchHighlights,
replaceAll,
replaceNext,
SearchQuery,
Expand Down Expand Up @@ -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<number>();
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. */
Expand Down
Loading