zeppelin-embed 0.8.0

In-process vector, lexical, and hybrid search for macOS and Apple silicon
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
//! Bounded exact top-k selection.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use super::ScanCandidate;
use crate::lifecycle::{StoreError, stats::AccountedCounter};

#[derive(Clone, Copy, Debug, PartialEq)]
struct HeapCandidate(ScanCandidate);

impl Eq for HeapCandidate {}

impl Ord for HeapCandidate {
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .0
            .score
            .total_cmp(&self.0.score)
            .then_with(|| self.0.row_id.cmp(&other.0.row_id))
    }
}

impl PartialOrd for HeapCandidate {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// A bounded score collector whose capacity is fixed at construction.
///
/// The competitive heap stays fixed-capacity. Candidates tied with its score
/// boundary are retained separately and may allocate so a caller with document
/// identity can perform the only tie-discarding cut. Physical exact-k callers
/// still receive equal scores in ascending row-id order.
pub(crate) struct BoundedTopK {
    limit: usize,
    heap: BinaryHeap<HeapCandidate>,
    boundary: Vec<ScanCandidate>,
}

impl BoundedTopK {
    pub(crate) fn new(limit: usize) -> Self {
        Self {
            limit,
            heap: BinaryHeap::with_capacity(limit),
            boundary: Vec::new(),
        }
    }

    pub(crate) fn push(&mut self, candidate: ScanCandidate) {
        let result = self.push_with_reserve(candidate, |_| Ok::<_, std::convert::Infallible>(()));
        match result {
            Ok(()) => {}
            Err(never) => match never {},
        }
    }

    /// The caller owns the charge through consumption of the returned candidates.
    pub(crate) fn try_new(limit: usize, memory: &mut AccountedCounter) -> Result<Self, StoreError> {
        memory.set(candidate_bytes(limit)?)?;
        let mut heap = BinaryHeap::new();
        reserve_allocation(|| heap.try_reserve_exact(limit))
            .map_err(|_| allocation_error(limit))?;
        Ok(Self {
            limit,
            heap,
            boundary: Vec::new(),
        })
    }

    // Exact scans update this heap for every eligible row, including rejects.
    #[inline(always)]
    pub(crate) fn try_push(
        &mut self,
        candidate: ScanCandidate,
        memory: &mut AccountedCounter,
    ) -> Result<(), StoreError> {
        let heap_capacity = self.heap.capacity();
        self.push_with_reserve(candidate, |boundary| {
            if boundary.len() == boundary.capacity() {
                let capacity = boundary
                    .capacity()
                    .max(1)
                    .checked_mul(2)
                    .ok_or_else(|| allocation_error(usize::MAX))?;
                let total = heap_capacity
                    .checked_add(capacity)
                    .ok_or_else(|| allocation_error(usize::MAX))?;
                memory.set(candidate_bytes(total)?)?;
                reserve_allocation(|| boundary.try_reserve_exact(capacity - boundary.len()))
                    .map_err(|_| allocation_error(capacity))?;
            }
            Ok(())
        })
    }

    pub(crate) fn capacity(&self) -> usize {
        self.heap
            .capacity()
            .saturating_add(self.boundary.capacity())
    }

    pub(crate) fn try_into_sorted_with_ties(
        mut self,
        memory: &mut AccountedCounter,
    ) -> Result<(Vec<ScanCandidate>, usize), StoreError> {
        let len = self
            .heap
            .len()
            .checked_add(self.boundary.len())
            .ok_or_else(|| allocation_error(usize::MAX))?;
        // Reuse the tie allocation as the output; large tie groups commonly
        // already have room for the heap. Charge any growth before reserving.
        if len > self.boundary.capacity() {
            let capacity = self
                .heap
                .capacity()
                .checked_add(len)
                .ok_or_else(|| allocation_error(usize::MAX))?;
            memory.set(candidate_bytes(capacity)?)?;
            reserve_allocation(|| self.boundary.try_reserve_exact(len - self.boundary.len()))
                .map_err(|_| allocation_error(len))?;
        }
        let peak_capacity = self.capacity();
        let Self {
            heap, mut boundary, ..
        } = self;
        boundary.extend(heap.iter().map(|candidate| candidate.0));
        drop(heap);
        memory.set(candidate_bytes(boundary.capacity())?)?;
        sort_candidates(&mut boundary);
        Ok((boundary, peak_capacity))
    }

    #[inline(always)]
    fn push_with_reserve<E>(
        &mut self,
        candidate: ScanCandidate,
        mut reserve: impl FnMut(&mut Vec<ScanCandidate>) -> Result<(), E>,
    ) -> Result<(), E> {
        if self.limit == 0 {
            return Ok(());
        }
        let candidate = HeapCandidate(candidate);
        if self.heap.len() < self.limit {
            self.heap.push(candidate);
            return Ok(());
        }
        let Some(worst_score) = self.heap.peek().map(|worst| worst.0.score) else {
            self.heap.push(candidate);
            return Ok(());
        };
        match candidate.0.score.total_cmp(&worst_score) {
            Ordering::Less => {}
            Ordering::Equal => {
                reserve(&mut self.boundary)?;
                self.boundary.push(candidate.0);
            }
            Ordering::Greater => {
                let Some(evicted) = self.heap.pop() else {
                    self.heap.push(candidate);
                    return Ok(());
                };
                self.heap.push(candidate);
                let Some(new_worst_score) = self.heap.peek().map(|worst| worst.0.score) else {
                    self.boundary.clear();
                    return Ok(());
                };
                if evicted.0.score.total_cmp(&new_worst_score).is_eq() {
                    reserve(&mut self.boundary)?;
                    self.boundary.push(evicted.0);
                }
                self.boundary
                    .retain(|tied| tied.score.total_cmp(&new_worst_score).is_eq());
            }
        }
        Ok(())
    }

    pub(crate) fn into_sorted(self) -> Vec<ScanCandidate> {
        let limit = self.limit;
        let mut candidates = self.into_sorted_with_ties();
        candidates.truncate(limit);
        candidates
    }

    pub(crate) fn into_sorted_with_ties(self) -> Vec<ScanCandidate> {
        let mut candidates = self
            .heap
            .into_vec()
            .into_iter()
            .map(|candidate| candidate.0)
            .collect::<Vec<_>>();
        candidates.extend(self.boundary);
        candidates.sort_unstable_by(|left, right| {
            right
                .score
                .total_cmp(&left.score)
                .then_with(|| left.row_id.cmp(&right.row_id))
        });
        candidates
    }
}

/// Exact scans know the complete eligible count. When it fits in 2k slots,
/// one unordered buffer avoids heap maintenance and still uses O(k) capacity.
pub(crate) enum ExactTopK {
    Heap(BoundedTopK),
    Unordered {
        limit: usize,
        candidates: Vec<ScanCandidate>,
    },
}

impl ExactTopK {
    pub(crate) fn try_new(
        limit: usize,
        eligible: usize,
        memory: &mut AccountedCounter,
    ) -> Result<Self, StoreError> {
        let limit = limit.min(eligible);
        if eligible <= limit.saturating_mul(2) {
            memory.set(candidate_bytes(eligible)?)?;
            let mut candidates = Vec::new();
            reserve_allocation(|| candidates.try_reserve_exact(eligible))
                .map_err(|_| allocation_error(eligible))?;
            Ok(Self::Unordered { limit, candidates })
        } else {
            Ok(Self::Heap(BoundedTopK::try_new(limit, memory)?))
        }
    }

    // Keep the per-row update inline when serial scans, worker partitions and
    // the final merge share this collector. Outlining adds a call to every row.
    #[inline(always)]
    pub(crate) fn try_push(
        &mut self,
        candidate: ScanCandidate,
        memory: &mut AccountedCounter,
    ) -> Result<(), StoreError> {
        match self {
            Self::Heap(heap) => heap.try_push(candidate, memory),
            Self::Unordered { candidates, .. } => {
                if candidates.len() == candidates.capacity() {
                    return Err(allocation_error(candidates.len().saturating_add(1)));
                }
                candidates.push(candidate);
                Ok(())
            }
        }
    }

    pub(crate) fn try_into_sorted_with_ties(
        self,
        memory: &mut AccountedCounter,
    ) -> Result<(Vec<ScanCandidate>, usize), StoreError> {
        match self {
            Self::Heap(heap) => heap.try_into_sorted_with_ties(memory),
            Self::Unordered {
                limit,
                mut candidates,
            } => {
                if limit == 0 {
                    candidates.clear();
                } else if candidates.len() > limit {
                    let (_, boundary, _) = candidates
                        .select_nth_unstable_by(limit - 1, |left, right| {
                            right.score.total_cmp(&left.score)
                        });
                    let score = boundary.score;
                    candidates.retain(|candidate| !candidate.score.total_cmp(&score).is_lt());
                }
                sort_candidates(&mut candidates);
                let capacity = candidates.capacity();
                Ok((candidates, capacity))
            }
        }
    }
}

fn sort_candidates(candidates: &mut [ScanCandidate]) {
    candidates.sort_unstable_by(|left, right| {
        right
            .score
            .total_cmp(&left.score)
            .then_with(|| left.row_id.cmp(&right.row_id))
    });
}

fn reserve_allocation(
    operation: impl FnOnce() -> Result<(), std::collections::TryReserveError>,
) -> Result<(), std::collections::TryReserveError> {
    #[cfg(feature = "allocation-audit")]
    {
        crate::allocation_audit::attributed(operation)
    }
    #[cfg(not(feature = "allocation-audit"))]
    {
        operation()
    }
}

fn candidate_bytes(capacity: usize) -> Result<usize, StoreError> {
    capacity
        .checked_mul(std::mem::size_of::<ScanCandidate>())
        .ok_or_else(|| allocation_error(usize::MAX))
}

fn allocation_error(capacity: usize) -> StoreError {
    StoreError::AllocationFailed {
        needed: capacity.saturating_mul(std::mem::size_of::<ScanCandidate>()) as u64,
        component: "exact scan collector",
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::indexing_slicing)]
mod tests {
    use rand::seq::SliceRandom;

    use super::BoundedTopK;
    use crate::scan::ScanCandidate;

    #[test]
    fn astra_04_exact_collector_matches_sort_at_both_capacity_strategies() {
        use crate::lifecycle::stats::{AccountedCounter, Accounting, AllocationComponent};
        use std::sync::Arc;
        for k in [0_usize, 1, 8, 24, 47, 48, 64] {
            let mut oracle = (0..48)
                .map(|row_id| ScanCandidate {
                    row_id,
                    score: ((row_id * 17) % 7) as f32,
                })
                .collect::<Vec<_>>();
            oracle
                .sort_unstable_by(|a, b| b.score.total_cmp(&a.score).then(a.row_id.cmp(&b.row_id)));
            if k == 0 {
                oracle.clear();
            } else if let Some(score) = oracle.get(k - 1).map(|candidate| candidate.score) {
                oracle.retain(|candidate| !candidate.score.total_cmp(&score).is_lt());
            }
            for reverse in [false, true] {
                let accounting = Arc::new(Accounting::new(u64::MAX, u64::MAX));
                let mut memory = AccountedCounter::new(&accounting, AllocationComponent::Temporary)
                    .expect("counter");
                let mut collector =
                    super::ExactTopK::try_new(k, 48, &mut memory).expect("collector");
                for pos in 0..48 {
                    let row_id = if reverse { 47 - pos } else { pos };
                    collector
                        .try_push(
                            ScanCandidate {
                                row_id,
                                score: ((row_id * 17) % 7) as f32,
                            },
                            &mut memory,
                        )
                        .expect("candidate");
                }
                let (actual, _) = collector
                    .try_into_sorted_with_ties(&mut memory)
                    .expect("drain");
                assert_eq!(actual, oracle);
                assert_eq!(
                    memory.bytes(),
                    (actual.capacity() * std::mem::size_of::<ScanCandidate>()) as u64
                );
                drop(actual);
                drop(memory);
                assert_eq!(accounting.audit().expect("released").temporary_bytes, 0);
            }
        }
    }

    #[test]
    fn astra_04_collector_charges_capacity_before_allocation_and_through_drain() {
        use crate::lifecycle::{
            StoreError,
            stats::{AccountedCounter, Accounting, AllocationComponent},
        };
        use std::sync::Arc;
        let unit = std::mem::size_of::<ScanCandidate>() as u64;
        let accounting = Arc::new(Accounting::new(u64::MAX, 3 * unit));
        let mut memory =
            AccountedCounter::new(&accounting, AllocationComponent::Temporary).expect("counter");
        let mut collector = BoundedTopK::try_new(2, &mut memory).expect("heap");
        assert_eq!(accounting.audit().expect("audit").temporary_bytes, 2 * unit);
        for row_id in 0..2 {
            collector
                .try_push(ScanCandidate { row_id, score: 1.0 }, &mut memory)
                .expect("heap row");
        }
        let deny = || {
            collector.try_push(
                ScanCandidate {
                    row_id: 2,
                    score: 1.0,
                },
                &mut memory,
            )
        };
        #[cfg(feature = "allocation-audit")]
        let (error, audit) = crate::allocation_audit::audit_engine_path(deny);
        #[cfg(not(feature = "allocation-audit"))]
        let error = {
            let mut deny = deny;
            deny()
        };
        assert!(matches!(
            error,
            Err(StoreError::BudgetExceeded {
                component: "temporary",
                ..
            })
        ));
        #[cfg(feature = "allocation-audit")]
        assert_eq!(
            audit.allocations, 0,
            "deny tie capacity before calling allocator"
        );
        assert_eq!(collector.capacity(), 2);
        assert_eq!(memory.bytes(), 2 * unit);
        drop(collector);
        drop(memory);
        assert_eq!(accounting.audit().expect("released").temporary_bytes, 0);

        let accounting = Arc::new(Accounting::new(u64::MAX, u64::MAX));
        let mut memory =
            AccountedCounter::new(&accounting, AllocationComponent::Temporary).expect("counter");
        let build = || {
            let mut collector = BoundedTopK::try_new(2, &mut memory).expect("heap");
            for row_id in 0..19 {
                collector
                    .try_push(ScanCandidate { row_id, score: 1.0 }, &mut memory)
                    .expect("arbitrary ties");
                assert_eq!(memory.bytes(), collector.capacity() as u64 * unit);
            }
            collector
                .try_into_sorted_with_ties(&mut memory)
                .expect("drain")
        };
        #[cfg(feature = "allocation-audit")]
        let ((candidates, _), audit) = crate::allocation_audit::audit_engine_path(build);
        #[cfg(not(feature = "allocation-audit"))]
        let (candidates, _) = {
            let mut build = build;
            build()
        };
        #[cfg(feature = "allocation-audit")]
        {
            assert_eq!(audit.unattributed_bytes, 0);
            assert!(audit.attributed_bytes > 0);
        }
        assert_eq!(candidates.len(), 19);
        assert_eq!(memory.bytes(), candidates.capacity() as u64 * unit);
        assert_eq!(
            accounting.audit().expect("output alive").temporary_bytes,
            memory.bytes()
        );
        drop(candidates);
        drop(memory);
        assert_eq!(
            accounting.audit().expect("final release").temporary_bytes,
            0
        );
    }

    #[test]
    fn bounded_top_k_retains_every_candidate_tied_with_the_kth_score() {
        for order in [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0], [2, 4, 0, 3, 1]] {
            let mut with_ties = BoundedTopK::new(2);
            let mut exact = BoundedTopK::new(2);
            for row_id in order {
                let score = [5.0, 4.0, 4.0, 4.0, 3.0][row_id];
                let candidate = ScanCandidate { row_id, score };
                with_ties.push(candidate);
                exact.push(candidate);
            }

            assert_eq!(
                with_ties
                    .into_sorted_with_ties()
                    .into_iter()
                    .map(|candidate| candidate.row_id)
                    .collect::<Vec<_>>(),
                [0, 1, 2, 3]
            );
            assert_eq!(
                exact
                    .into_sorted()
                    .into_iter()
                    .map(|candidate| candidate.row_id)
                    .collect::<Vec<_>>(),
                [0, 1]
            );
        }
    }

    #[test]
    fn seeded_boundary_tie_set_is_invariant_under_input_permutation() {
        let candidates = (0..48)
            .map(|row_id| ScanCandidate {
                row_id,
                score: ((row_id * 17) % 7) as f32,
            })
            .collect::<Vec<_>>();
        let k = 8;
        let mut expected_with_ties = candidates.clone();
        expected_with_ties.sort_unstable_by(|left, right| {
            right
                .score
                .total_cmp(&left.score)
                .then_with(|| left.row_id.cmp(&right.row_id))
        });
        crate::scan::truncate_to_k_with_score_ties(&mut expected_with_ties, k, |candidate| {
            candidate.score
        });
        let expected_exact = expected_with_ties
            .iter()
            .copied()
            .take(k)
            .collect::<Vec<_>>();
        let mut random = crate::test_support::seeded_rng(
            "scan::topk::tests::seeded_boundary_tie_set_is_invariant_under_input_permutation",
        );

        for _ in 0..128 {
            let mut input = candidates.clone();
            input.shuffle(&mut random);
            let mut with_ties = BoundedTopK::new(k);
            let mut exact = BoundedTopK::new(k);
            for candidate in input {
                with_ties.push(candidate);
                exact.push(candidate);
            }
            assert_eq!(with_ties.into_sorted_with_ties(), expected_with_ties);
            assert_eq!(exact.into_sorted(), expected_exact);
        }
    }
}