Skip to main content

scanline_core/
lib.rs

1//! Scanline execution engine.
2//!
3//! Owns the query execution loop: it takes an aligned buffer from
4//! `scanline-storage`, splits it into per-core chunks, and drives the predicate
5//! kernels in `scanline-simd` across pinned worker threads.
6//!
7//! Phase 0 scaffold: the chunk scheduler and core-affinity pinning land in
8//! Phase 1. What exists here now is the single-threaded execution path, which is
9//! already enough to saturate a meaningful fraction of memory bandwidth and is
10//! the baseline every parallel result must beat.
11
12use scanline_simd::Backend;
13use scanline_storage::AlignedVec;
14
15/// Outcome of one scan, carrying enough context to be reported honestly.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct ScanResult {
18    /// Rows satisfying the predicate.
19    pub matches: usize,
20    /// Rows examined.
21    pub rows: usize,
22    /// Backend that actually executed.
23    pub backend: Backend,
24}
25
26impl ScanResult {
27    /// Bytes read from memory by this scan, assuming a dense `i64` column.
28    #[must_use]
29    pub const fn bytes_read(&self) -> usize {
30        self.rows * std::mem::size_of::<i64>()
31    }
32}
33
34/// Runs a greater-than predicate over an aligned `i64` buffer.
35#[must_use]
36pub fn scan_greater_than(data: &AlignedVec<i64>, target: i64) -> ScanResult {
37    let backend = scanline_simd::detect_backend();
38    ScanResult {
39        matches: scanline_simd::scan_greater_than_i64_with(data, target, backend),
40        rows: data.len(),
41        backend,
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn scan_over_aligned_buffer_counts_correctly() {
51        let mut buf = AlignedVec::<i64>::zeroed_cache_aligned(1000);
52        for (i, slot) in buf.iter_mut().enumerate() {
53            *slot = i as i64;
54        }
55
56        let result = scan_greater_than(&buf, 499);
57
58        assert_eq!(result.matches, 500);
59        assert_eq!(result.rows, 1000);
60        assert_eq!(result.bytes_read(), 8000);
61    }
62
63    #[test]
64    fn scan_over_empty_buffer_is_zero() {
65        let buf = AlignedVec::<i64>::zeroed_cache_aligned(0);
66        let result = scan_greater_than(&buf, 0);
67        assert_eq!(result.matches, 0);
68        assert_eq!(result.rows, 0);
69    }
70}