holographic-memory 0.6.3

Local-first search engine and agent memory: fast approximate nearest-neighbor and BM25 full-text search, encrypted stores, provable deletion, built on vector-symbolic architectures.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Copyright 2024-2026 WritersLogic Contributors
// SPDX-License-Identifier: Apache-2.0

use super::wand::{sum_bound, up, Bound};
use super::*;
use crate::core::documents::postings::PostingCursor;
use std::collections::BTreeMap;

struct TermState<'a> {
    cursor: PostingCursor<'a>,
    df: usize,
    live_count: usize,
    /// The largest frequency and least length bound over the whole list.
    maxima: (u32, u32),
}

pub(super) struct TopK {
    values: Vec<(f64, usize)>,
    k: usize,
    floor: f64,
}

impl TopK {
    pub(super) fn new(k: usize) -> Self {
        Self {
            values: Vec::new(),
            k,
            floor: 0.0,
        }
    }
    pub(super) fn floor(&self) -> f64 {
        self.floor
    }
    fn trim(&mut self, store: &DocumentStore) {
        super::super::top_ranked(&mut self.values, self.k, |a, b| {
            b.0.total_cmp(&a.0)
                .then_with(|| store.slot_order(a.1).cmp(&store.slot_order(b.1)))
        });
        if self.values.len() == self.k {
            self.floor = self.values.last().expect("nonempty top-k").0;
        }
    }
    pub(super) fn consider(&mut self, store: &DocumentStore, score: f64, slot: usize) {
        if self.k == 0 || score < self.floor {
            return;
        }
        self.values.push((score, slot));
        if self.values.len() == self.k * 2 {
            self.trim(store);
        }
    }
    pub(super) fn finish(mut self, store: &DocumentStore) -> Vec<LexicalHit> {
        if self.k == 0 {
            return Vec::new();
        }
        self.trim(store);
        self.values
            .into_iter()
            .map(|(score, slot)| {
                let (document, chunk_index) = store.slot_order(slot);
                LexicalHit {
                    document_id: document.to_owned(),
                    chunk_index,
                    score,
                }
            })
            .collect()
    }
}

fn conjunction(states: &mut [TermState<'_>], required: &[usize]) -> Option<usize> {
    let (&lead, others) = required.split_first().expect("nonempty conjunction");
    'align: loop {
        let target = states[lead].cursor.peek()?.0;
        for &index in others {
            let found = states[index].cursor.advance(target)?.0;
            if found > target {
                states[lead].cursor.advance(found)?;
                continue 'align;
            }
        }
        return Some(target);
    }
}

fn count_match(store: &DocumentStore, slot: usize, seen: &mut [u64], count: &mut usize) {
    if !store.multichunk_documents.is_empty() {
        let (document, _) = store.borrowed_chunk(slot);
        if document.chunks.len() > 1 {
            let first = store.slots[&document.id][0];
            let bit = 1 << (first % 64);
            if seen[first / 64] & bit != 0 {
                return;
            }
            seen[first / 64] |= bit;
        }
    }
    *count += 1;
}

/// The weight bounds of one positive clause of a form.
struct ClauseBound {
    bound: Bound,
    /// A bound of the clause's weight in any chunk.
    ceiling: f64,
    /// The bound of the last block of a one-word clause, by block index.
    cached: (usize, f64),
}

/// Bounds of the positive clauses, or `None` where a bound cannot be proven.
struct Pruning {
    clauses: Vec<Option<ClauseBound>>,
}

impl Pruning {
    fn new(
        query: &LexicalQuery,
        states: &[TermState<'_>],
        indices: &[Vec<Option<usize>>],
        idfs: &[[f64; 2]],
        form: usize,
        average: f64,
        bm25: Bm25,
    ) -> Option<Self> {
        let mut clauses = Vec::with_capacity(query.clauses.len());
        let mut global = 0.0;
        for ((clause, indices), idf) in query.clauses.iter().zip(indices).zip(idfs) {
            if clause.occur == Occur::MustNot {
                clauses.push(None);
                continue;
            }
            let bound = Bound::new(idf[form], average, bm25)?;
            let ceiling = match indices
                .iter()
                .map(|index| index.map(|i| states[i].maxima))
                .collect::<Option<Vec<_>>>()
            {
                Some(maxima) => {
                    let (frequency, words) = combine(maxima.into_iter());
                    bound.at(frequency, words)
                }
                None => 0.0,
            };
            global = up(global + bound.global);
            clauses.push(Some(ClauseBound {
                bound,
                ceiling,
                cached: (usize::MAX, 0.0),
            }));
        }
        sum_bound(global, query.clauses.len())
            .is_finite()
            .then_some(Self { clauses })
    }

    /// A bound of the score of the chunk in `slot`, from the blocks holding it.
    fn at(
        &mut self,
        states: &mut [TermState<'_>],
        indices: &[Vec<Option<usize>>],
        slot: usize,
    ) -> f64 {
        let mut sum = 0.0;
        for (clause, indices) in self.clauses.iter_mut().zip(indices) {
            let Some(clause) = clause else { continue };
            let value = if let [Some(i)] = indices[..] {
                let state = &mut states[i];
                if state.cursor.locate(slot) {
                    let block = state.cursor.block_index();
                    if clause.cached.0 != block {
                        let (frequency, words) = state.cursor.block_maximum();
                        clause.cached = (block, clause.bound.at(frequency, words));
                    }
                    clause.cached.1
                } else {
                    0.0
                }
            } else {
                let mut maxima = Vec::with_capacity(indices.len());
                for index in indices {
                    match index {
                        Some(i) if states[*i].cursor.locate(slot) => {
                            maxima.push(states[*i].cursor.block_maximum());
                        }
                        _ => break,
                    }
                }
                if maxima.len() == indices.len() {
                    let (frequency, words) = combine(maxima.into_iter());
                    clause.bound.at(frequency, words)
                } else {
                    0.0
                }
            };
            sum = up(sum + value);
        }
        sum_bound(sum, self.clauses.len())
    }

    /// A bound of the score of any chunk in the block `lead` stands on, from that block's
    /// bounds and the bounds of the other lists' blocks over the same slots.
    fn over_lead_block(
        &self,
        states: &[TermState<'_>],
        indices: &[Vec<Option<usize>>],
        lead: usize,
    ) -> f64 {
        let cursor = &states[lead].cursor;
        let (Some((first, _)), Some(last)) = (cursor.peek(), cursor.block_end()) else {
            return 0.0;
        };
        let lead_maximum = cursor.block_maximum();
        let mut sum = 0.0;
        for (clause, indices) in self.clauses.iter().zip(indices) {
            let Some(clause) = clause else { continue };
            let maxima = indices.iter().map(|index| match index {
                Some(i) if *i == lead => lead_maximum,
                Some(i) => states[*i]
                    .cursor
                    .range_maximum(first, last)
                    .unwrap_or((0, u32::MAX)),
                None => (0, u32::MAX),
            });
            let (frequency, words) = combine(maxima);
            sum = up(sum + clause.bound.at(frequency, words));
        }
        sum_bound(sum, self.clauses.len())
    }
}

/// The frequency and length bounds a phrase of terms with the given bounds obeys: it occurs
/// at most as often as its rarest term in a chunk at least as long as every term's bound.
fn combine(maxima: impl Iterator<Item = (u32, u32)>) -> (u32, u32) {
    maxima.fold((u32::MAX, 0), |(frequency, words), (f, w)| {
        (frequency.min(f), words.max(w))
    })
}

fn positional_frequency(
    positions: &crate::core::documents::positions::Positions,
    indices: &[Option<usize>],
    states: &[TermState<'_>],
    exact: bool,
) -> u32 {
    let lead = indices
        .iter()
        .enumerate()
        .min_by_key(|(_, index)| {
            states[index.expect("present phrase term")]
                .cursor
                .peek()
                .expect("phrase candidate")
                .1
        })
        .expect("nonempty phrase")
        .0;
    let term_index = |index: Option<usize>| {
        states[index.expect("present phrase term")]
            .cursor
            .term_index()
    };
    let lead_positions = positions.iter(term_index(indices[lead]));
    if indices.len() == 2 {
        let mut cursors =
            [indices[0], indices[1]].map(|index| positions.iter(term_index(index)).peekable());
        return match_positions(lead_positions, lead, &mut cursors, exact);
    }
    if indices.len() == 3 {
        let mut cursors = [indices[0], indices[1], indices[2]]
            .map(|index| positions.iter(term_index(index)).peekable());
        return match_positions(lead_positions, lead, &mut cursors, exact);
    }
    if indices.len() == 4 {
        let mut cursors = [indices[0], indices[1], indices[2], indices[3]]
            .map(|index| positions.iter(term_index(index)).peekable());
        return match_positions(lead_positions, lead, &mut cursors, exact);
    }
    let mut cursors: Vec<_> = indices
        .iter()
        .map(|&index| positions.iter(term_index(index)).peekable())
        .collect();
    match_positions(lead_positions, lead, &mut cursors, exact)
}

fn match_positions<I: Iterator<Item = u32>>(
    lead_positions: I,
    lead: usize,
    cursors: &mut [std::iter::Peekable<I>],
    exact: bool,
) -> u32 {
    let mut count = 0;
    for position in lead_positions {
        let Some(start) = position.checked_sub(lead as u32) else {
            continue;
        };
        let matched = cursors.iter_mut().enumerate().all(|(offset, iter)| {
            let Some(target) = start.checked_add(offset as u32) else {
                return false;
            };
            while iter.peek().is_some_and(|&position| position < target) {
                iter.next();
            }
            iter.peek() == Some(&target)
        });
        count += u32::from(matched);
        if matched && !exact {
            break;
        }
    }
    count
}

pub(super) fn document_at_a_time(
    store: &DocumentStore,
    query: &LexicalQuery,
    options: &LexicalOptions,
    bm25: Bm25,
) -> Result<LexicalResults> {
    let mut top = TopK::new(options.k);
    let mut count = 0;
    let mut seen = if options.count && store.chunk_count != store.documents.len() {
        vec![0u64; store.slot_count.div_ceil(64)]
    } else {
        Vec::new()
    };
    if !query.has_positive() {
        return Ok(LexicalResults {
            hits: Vec::new(),
            count: options.count.then_some(0),
        });
    }
    let idfs = if options.k > 0 {
        clause_idfs(store, query)
    } else {
        Vec::new()
    };
    let average = (store.word_count as f64 / (store.chunk_count as f64).max(1.0)).max(1.0);
    let single_phrase = query.clauses.len() == 1 && query.clauses[0].phrase;
    let phrases_ranked = !options.count && query.clauses.iter().any(|c| c.phrase);
    for form in 0..2 {
        let mut by_word = BTreeMap::new();
        let mut states = Vec::new();
        for clause in &query.clauses {
            for word in &clause.words[form] {
                if by_word.contains_key(word.as_str()) {
                    continue;
                }
                let index = store.postings[form].get(word).map(|list| {
                    let index = states.len();
                    states.push(TermState {
                        cursor: list.iter(),
                        df: list.len(),
                        live_count: list.positive_len(),
                        maxima: list.maxima(),
                    });
                    index
                });
                by_word.insert(word.as_str(), index);
            }
        }
        let indices: Vec<Vec<Option<usize>>> = query
            .clauses
            .iter()
            .map(|clause| {
                clause.words[form]
                    .iter()
                    .map(|w| by_word[w.as_str()])
                    .collect()
            })
            .collect();
        let mut required = Vec::new();
        let mut positive = BTreeSet::new();
        let mut missing_required = false;
        for (clause, indices) in query.clauses.iter().zip(&indices) {
            for &index in indices {
                if clause.occur == Occur::Must || single_phrase {
                    match index {
                        Some(index) => required.push(index),
                        None => missing_required = true,
                    }
                }
                if clause.occur != Occur::MustNot {
                    if let Some(index) = index {
                        positive.insert(index);
                    }
                }
            }
        }
        if missing_required {
            continue;
        }
        required.sort_unstable();
        required.dedup();
        required.sort_unstable_by_key(|&i| states[i].df);
        let single_required_term = !single_phrase
            && required.len() == 1
            && query
                .clauses
                .iter()
                .filter(|c| c.occur == Occur::Must)
                .all(|c| !c.phrase)
            && query.clauses.iter().all(|c| c.occur != Occur::MustNot);
        if options.k == 0
            && options.count
            && store.chunk_count == store.documents.len()
            && (single_required_term
                || (query.clauses.len() == 1
                    && !query.clauses[0].phrase
                    && query.clauses[0].occur != Occur::MustNot))
        {
            count += if single_required_term {
                states[required[0]].live_count
            } else {
                positive
                    .iter()
                    .map(|&i| states[i].live_count)
                    .sum::<usize>()
            };
            continue;
        }
        let term_plan =
            (!required.is_empty() && query.clauses.iter().all(|c| !c.phrase)).then(|| {
                let mut excluded = Vec::new();
                let mut scored = Vec::new();
                for (c, (clause, indices)) in query.clauses.iter().zip(&indices).enumerate() {
                    if let Some(index) = indices[0] {
                        if clause.occur == Occur::MustNot {
                            excluded.push(index);
                        } else if options.k > 0 {
                            scored.push((index, idfs[c][form]));
                        }
                    }
                }
                (excluded, scored)
            });
        // Ranked evaluation skips a chunk, or a whole block of the leading list, whose score
        // bound is below the floor of the top-k; counting still visits every match.
        let mut pruning = if options.k > 0 {
            Pruning::new(query, &states, &indices, &idfs, form, average, bm25)
        } else {
            None
        };
        let mut frequencies = vec![0; query.clauses.len()];
        let mut pending: Vec<Option<u32>> = vec![None; query.clauses.len()];
        // Optional words not yet required of every ranked candidate, by clause.
        let mut optional: Vec<(usize, usize)> = query
            .clauses
            .iter()
            .zip(&indices)
            .enumerate()
            .filter_map(|(c, (clause, indices))| match indices[..] {
                [Some(i)] if clause.occur == Occur::Should && !single_phrase => Some((c, i)),
                _ => None,
            })
            .collect();
        let mut checked_floor = 0.0;
        loop {
            // Once the floor exceeds every clause's ceiling but one optional word's, a chunk
            // without that word cannot rank: the word joins the conjunction (MaxScore's
            // essential lists), entering at the lead's position so no candidate repeats.
            if let (Some(pruning), Some(&lead), false) =
                (pruning.as_ref(), required.first(), options.count)
            {
                let floor = top.floor();
                if floor > checked_floor && !optional.is_empty() {
                    checked_floor = floor;
                    let mut promoted = false;
                    optional.retain(|&(c, i)| {
                        if required.contains(&i) {
                            return false;
                        }
                        let others = pruning
                            .clauses
                            .iter()
                            .enumerate()
                            .filter(|&(other, _)| other != c)
                            .filter_map(|(_, clause)| clause.as_ref())
                            .fold(0.0, |sum, clause| up(sum + clause.ceiling));
                        if sum_bound(others, query.clauses.len()) >= floor {
                            return true;
                        }
                        if let Some((at, _)) = states[lead].cursor.peek() {
                            states[i].cursor.advance(at);
                        }
                        required.push(i);
                        promoted = true;
                        false
                    });
                    if promoted {
                        required.sort_unstable_by_key(|&i| states[i].df);
                    }
                }
            }
            if let (Some(pruning), Some(&lead), false) =
                (pruning.as_ref(), required.first(), options.count)
            {
                while states[lead].cursor.at_block_start()
                    && pruning.over_lead_block(&states, &indices, lead) < top.floor()
                {
                    let end = states[lead].cursor.block_end().expect("block start");
                    let Some(next) = end.checked_add(1) else {
                        break;
                    };
                    states[lead].cursor.advance(next);
                }
            }
            let candidate = if required.is_empty() {
                positive
                    .iter()
                    .filter_map(|&i| states[i].cursor.peek().map(|p| p.0))
                    .min()
            } else {
                conjunction(&mut states, &required)
            };
            let Some(slot) = candidate else {
                break;
            };
            if let Some((excluded, scored)) = &term_plan {
                let matched = required
                    .iter()
                    .all(|&i| states[i].cursor.peek().is_some_and(|p| p.1 > 0))
                    && !excluded.iter().any(|&i| {
                        states[i]
                            .cursor
                            .advance(slot)
                            .is_some_and(|p| p.0 == slot && p.1 > 0)
                    });
                if matched {
                    if options.count {
                        count_match(store, slot, &mut seen, &mut count);
                    }
                    if options.k > 0 {
                        let norm = bm25.norm(f64::from(store.slot_word_count(slot)), average);
                        let mut score = 0.0;
                        for &(i, idf) in scored {
                            if let Some((at, frequency)) = states[i].cursor.advance(slot) {
                                if at == slot && frequency > 0 {
                                    score += bm25.weight(idf, f64::from(frequency), norm);
                                }
                            }
                        }
                        ensure!(
                            score.is_finite(),
                            "lexical score is nonfinite; reduce BM25 parameters"
                        );
                        top.consider(store, score, slot);
                    }
                }
                states[required[0]].cursor.next();
                continue;
            }
            // A candidate's bound costs about what scoring its words costs, so it is checked
            // only where it can spare a phrase's position check.
            let skip = match pruning.as_mut() {
                Some(pruning) if phrases_ranked && top.floor() > 0.0 => {
                    pruning.at(&mut states, &indices, slot) < top.floor()
                }
                _ => false,
            };
            let mut scorable = true;
            if !skip {
                let mut phrases = false;
                for (((clause, indices), frequency_value), pending) in query
                    .clauses
                    .iter()
                    .zip(&indices)
                    .zip(&mut frequencies)
                    .zip(&mut pending)
                {
                    *pending = None;
                    if options.k == 0
                        && !required.is_empty()
                        && !single_phrase
                        && clause.occur == Occur::Should
                    {
                        *frequency_value = 0;
                        continue;
                    }
                    if clause.phrase {
                        let mut least = u32::MAX;
                        let possible = indices.iter().all(|&i| {
                            i.is_some_and(|i| {
                                states[i]
                                    .cursor
                                    .advance(slot)
                                    .filter(|p| p.0 == slot && p.1 > 0)
                                    .inspect(|p| least = least.min(p.1))
                                    .is_some()
                            })
                        });
                        *frequency_value = 0;
                        if possible {
                            *pending = Some(least);
                            phrases = true;
                        }
                    } else {
                        *frequency_value = indices[0]
                            .and_then(|i| states[i].cursor.advance(slot))
                            .filter(|p| p.0 == slot)
                            .map_or(0, |p| p.1);
                    }
                }
                if phrases {
                    let positions = store.slot_positions(slot);
                    // The positions of a phrase are counted only where the count can
                    // change the ranking; otherwise its first occurrence settles the match.
                    let exact = options.k > 0
                        && match pruning.as_ref() {
                            Some(pruning) => {
                                let words = store.slot_word_count(slot);
                                let norm = bm25.norm(f64::from(words), average);
                                let mut sum = 0.0;
                                for ((&frequency, pending), bound) in
                                    frequencies.iter().zip(&pending).zip(&pruning.clauses)
                                {
                                    let Some(bound) = bound else { continue };
                                    let value = match pending {
                                        Some(least) => bound.bound.at(*least, words),
                                        None if frequency > 0 => {
                                            bm25.weight(bound.bound.idf, f64::from(frequency), norm)
                                        }
                                        None => 0.0,
                                    };
                                    sum = up(sum + value);
                                }
                                sum_bound(sum, query.clauses.len()) >= top.floor()
                            }
                            None => true,
                        };
                    scorable = exact;
                    // Below the floor and uncounted, the phrase's positions are not read.
                    let needed = exact || options.count;
                    for (((clause, frequency_value), pending), indices) in query
                        .clauses
                        .iter()
                        .zip(&mut frequencies)
                        .zip(&pending)
                        .zip(&indices)
                    {
                        if pending.is_some() && needed {
                            *frequency_value = positional_frequency(
                                positions,
                                indices,
                                &states,
                                exact && clause.occur != Occur::MustNot,
                            );
                        }
                    }
                }
                if matches(query, &frequencies) {
                    if options.count {
                        count_match(store, slot, &mut seen, &mut count);
                    }
                    if options.k > 0 && scorable {
                        top.consider(
                            store,
                            score(
                                query,
                                &frequencies,
                                &idfs,
                                form,
                                bm25.norm(f64::from(store.slot_word_count(slot)), average),
                                bm25,
                            )?,
                            slot,
                        );
                    }
                }
            }
            if let Some(&lead) = required.first() {
                states[lead].cursor.next();
            } else {
                for &index in &positive {
                    if states[index].cursor.peek().is_some_and(|p| p.0 == slot) {
                        states[index].cursor.next();
                    }
                }
            }
        }
    }
    Ok(LexicalResults {
        hits: top.finish(store),
        count: options.count.then_some(count),
    })
}

pub(super) fn count_union(store: &DocumentStore, query: &LexicalQuery) -> usize {
    count_terms(
        store,
        std::array::from_fn(|form| {
            let words: BTreeSet<&str> = query
                .clauses
                .iter()
                .map(|c| c.words[form][0].as_str())
                .collect();
            words.into_iter().collect()
        }),
    )
}

/// The number of documents with a chunk holding one of `words[form]` in that form's postings
/// at a positive frequency: the documents an unfiltered disjunction of the words scores.
pub(in crate::core::documents) fn count_terms(
    store: &DocumentStore,
    words: [Vec<&str>; 2],
) -> usize {
    if words.iter().all(|words| words.len() <= 1) && store.chunk_count == store.documents.len() {
        // One chunk per document and at most one list per form, whose slots are disjoint.
        return (0..2)
            .map(|form| {
                words[form]
                    .first()
                    .and_then(|word| store.postings[form].get(word))
                    .map_or(0, |list| list.positive_len())
            })
            .sum();
    }
    let mut matched = vec![0u64; store.slot_count.div_ceil(64)];
    for (form, words) in words.iter().enumerate() {
        for word in words {
            if let Some(list) = store.postings[form].get(word) {
                list.mark_slots(&mut matched);
            }
        }
    }
    let mut count: usize = matched.iter().map(|word| word.count_ones() as usize).sum();
    if !store.multichunk_documents.is_empty() {
        // A document counts once: each further matching chunk of one is taken back.
        // Sized once: at most one entry per matched chunk of a multi-chunk document.
        let mut seen = crate::core::hash::FastSet::with_capacity_and_hasher(
            count.min(store.multichunk_documents.len()),
            Default::default(),
        );
        for (index, &bits) in matched.iter().enumerate() {
            let mut bits = bits;
            while bits != 0 {
                let slot = index * 64 + bits.trailing_zeros() as usize;
                bits &= bits - 1;
                let (document, _) = store.borrowed_chunk(slot);
                if document.chunks.len() > 1 && !seen.insert(std::ptr::from_ref(document)) {
                    count -= 1;
                }
            }
        }
    }
    count
}