From c3dc2f160316b675a5221b2ed480db3efab83a8d Mon Sep 17 00:00:00 2001 From: "Muse (repo-worker lane A)" Date: Sun, 1 Nov 2026 06:54:35 -0600 Subject: [PATCH] id(search): memoize subject_kind resolution in search_terms (#3013) search_terms documented subject_kind as called at most once per subject, but its per-term loop re-filtered every subject through the lookup for each SubjectKind term (N subjects x K kind terms calls). Resolve each subject kind once into a memo (including the unresolvable None) and let every kind term read it; AND results are unchanged. Adds a counting-lookup test: 3 subjects x 3 kind terms (one duplicated) resolve exactly 3 lookups, and a single kind term still returns the same subjects. --- pkgs/id/src/search.rs | 10 ++++++- pkgs/id/tests/search.rs | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/search.rs b/pkgs/id/src/search.rs index 7bec033cb8..53be0f5b33 100644 --- a/pkgs/id/src/search.rs +++ b/pkgs/id/src/search.rs @@ -103,6 +103,10 @@ pub fn search_terms( .or_default() .push(claim.id); } + // `subject_kind` is resolved at most once per subject, however many kind terms the + // search carries: the resolved kind (including an unresolvable `None`) is memoised + // here on first use, and every kind term reads the memo. + let mut kind_memo: HashMap<[u8; 32], Option> = HashMap::new(); // For each term, the subjects it matches and, for claim terms, the claims that match it. let mut matched: Vec<(BTreeSet<[u8; 32]>, BTreeMap<[u8; 32], BTreeSet<[u8; 32]>>)> = Vec::with_capacity(terms.len()); @@ -137,7 +141,11 @@ pub fn search_terms( .keys() .copied() .filter(|subject| { - subject_kind(&Hash::from_bytes(*subject)).as_deref() == Some(*kind) + kind_memo + .entry(*subject) + .or_insert_with(|| subject_kind(&Hash::from_bytes(*subject))) + .as_deref() + == Some(*kind) }) .collect(), _ => claims_by_subject.keys().copied().collect(), diff --git a/pkgs/id/tests/search.rs b/pkgs/id/tests/search.rs index d821f5a21d..f8c9c8e234 100644 --- a/pkgs/id/tests/search.rs +++ b/pkgs/id/tests/search.rs @@ -582,6 +582,65 @@ fn test_search_by_subject_kind_matches_only_events_of_that_kind() { assert!(hits.is_empty()); } +#[test] +fn test_search_terms_resolves_subject_kind_at_most_once_per_subject() { + use std::cell::Cell; + use std::collections::HashMap as Map; + + // Three subjects, each with one live claim. + let mut author = Writer::new(1); + let one = Hash::digest(b"counting-one"); + let two = Hash::digest(b"counting-two"); + let three = Hash::digest(b"counting-three"); + let on_one = author.set(one, "city", Some("Seattle"), vec![]); + let on_two = author.set(two, "city", Some("Seattle"), vec![on_one.id]); + let on_three = author.set(three, "city", Some("Seattle"), vec![on_two.id]); + + let (_dir, mut store) = open(); + for event in [&on_one, &on_two, &on_three] { + deliver(&mut store, event); + } + let live = store.tag_claims().unwrap(); + + // Subjects one and two resolve to a kind; three resolves to none. + let kinds: Map> = [ + (one, Some("kind-a".to_string())), + (two, Some("kind-a".to_string())), + (three, None), + ] + .into_iter() + .collect(); + let calls = Cell::new(0usize); + let lookup = |subject: &Hash| { + calls.set(calls.get() + 1); + kinds.get(subject).cloned().unwrap_or(None) + }; + + // Three kind terms (one duplicated) over three subjects: without the memo + // that is nine lookups; the documented contract is at most one per subject. + let hits = search_terms( + &live, + &[ + Term::SubjectKind("kind-a"), + Term::SubjectKind("kind-a"), + Term::SubjectKind("kind-b"), + ], + &lookup, + ); + assert_eq!(calls.get(), 3); + // AND semantics are unchanged: no subject is both kind-a and kind-b. + assert!(hits.is_empty()); + + // A single kind term gives the same subjects as before, still one call each. + calls.set(0); + let hits = search_terms(&live, &[Term::SubjectKind("kind-a")], &lookup); + assert_eq!(calls.get(), 3); + assert_eq!( + shape(&hits), + vec![(one, vec![on_one.id]), (two, vec![on_two.id])] + ); +} + #[test] fn test_search_terms_without_kind_succeeds_even_when_subject_event_is_missing() { let mut author = Writer::new(1);