From 3453e65cb184ef71e5c950e37d06bf1ae3e37658 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 06:12:46 -0600 Subject: [PATCH] id(search): memoize subject_kind resolution in search_terms 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 = N*K calls). Resolve kinds lazily into a per-call cache so the lookup runs at most once per distinct subject, including for duplicate kind terms. AND results are unchanged. Adds a counting-lookup integration test: 3 subjects x 2 identical kind terms and x 3 kind terms (two distinct + a duplicate) each assert <= 3 lookup calls with results identical to single-term behaviour. Fixes #3013 --- pkgs/id/src/search.rs | 13 ++++++++- pkgs/id/tests/search.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/search.rs b/pkgs/id/src/search.rs index 7bec033cb8..977dd2f5a7 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); } + // Memoized subject kinds, resolved lazily as kind terms are evaluated: + // `subject_kind` is called at most once per subject across the whole call, + // however many kind terms the term set carries (including duplicates). + let kind_cache: RefCell>> = RefCell::new(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,14 @@ pub fn search_terms( .keys() .copied() .filter(|subject| { - subject_kind(&Hash::from_bytes(*subject)).as_deref() == Some(*kind) + let resolved = { + let mut cache = kind_cache.borrow_mut(); + cache + .entry(*subject) + .or_insert_with(|| subject_kind(&Hash::from_bytes(*subject))) + .clone() + }; + resolved.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..122a570bd7 100644 --- a/pkgs/id/tests/search.rs +++ b/pkgs/id/tests/search.rs @@ -582,6 +582,68 @@ 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() { + // Three subjects, one live claim each. The counting lookup maps one to + // kind "alpha", one to "beta", and one to no kind at all. + let alpha = Hash::digest(b"alpha-subject"); + let beta = Hash::digest(b"beta-subject"); + let unknown = Hash::digest(b"unknown-subject"); + let mut a = Writer::new(1); + let on_alpha = a.set(alpha, "city", Some("Seattle"), vec![]); + let on_beta = a.set(beta, "city", Some("Seattle"), vec![on_alpha.id]); + let on_unknown = a.set(unknown, "city", Some("Seattle"), vec![on_beta.id]); + + let (_dir, mut store) = open(); + for event in [&on_alpha, &on_beta, &on_unknown] { + deliver(&mut store, event); + } + let live = store.tag_claims().unwrap(); + + let calls = std::cell::Cell::new(0usize); + let lookup = |subject: &Hash| { + calls.set(calls.get() + 1); + match *subject { + s if s == alpha => Some("alpha".to_owned()), + s if s == beta => Some("beta".to_owned()), + _ => None, + } + }; + + // K=2 kind terms, the same term twice: the alpha subject matches, and the + // lookup runs at most once per subject (N=3), not once per term (2*N=6). + let hits = search_terms( + &live, + &[Term::SubjectKind("alpha"), Term::SubjectKind("alpha")], + &lookup, + ); + assert_eq!(shape(&hits), vec![(alpha, vec![on_alpha.id])]); + assert!( + calls.get() <= 3, + "subject_kind called {} times for 3 subjects x 2 kind terms", + calls.get() + ); + + // A second, distinct kind term in the AND set still adds no new lookups: + // no subject is both kinds, so the answer is empty, at <= N calls again. + calls.set(0); + let hits = search_terms( + &live, + &[ + Term::SubjectKind("alpha"), + Term::SubjectKind("beta"), + Term::SubjectKind("alpha"), + ], + &lookup, + ); + assert!(hits.is_empty()); + assert!( + calls.get() <= 3, + "subject_kind called {} times for 3 subjects x 3 kind terms", + calls.get() + ); +} + #[test] fn test_search_terms_without_kind_succeeds_even_when_subject_event_is_missing() { let mut author = Writer::new(1);