Skip to main content

rudb_vector/
buffer.rs

1//! The run of values behind a flat vector, and the seam the buffer manager arrives through.
2//!
3//! `spec/engine/03-data-plane.md` section 3.8. Today every vector owns its payload and every scan
4//! allocates, which is the right place to start and is not where this ends. At layer three the scan
5//! reads a page out of the buffer manager and the vector wants to point into that page rather than
6//! copy out of it, and the copy it avoids is the largest single copy in the system, because it is
7//! every byte of every column every query reads.
8//!
9//! The type that supports both is one enum holding either an owned run or a borrowed one, and the
10//! part that has to be decided correctly the first time is how the borrow is expressed, because that
11//! shows up in every signature that mentions a vector.
12//!
13//! # Why the pin is a handle and not a lifetime
14//!
15//! The obvious way to express a borrow in Rust is a lifetime parameter, and it is the wrong one
16//! here. A lifetime on [`Buffer`] is a lifetime on [`Data`](crate::Data), which is a lifetime on
17//! [`Vector`](crate::Vector), which is a lifetime on [`Chunk`](crate::Chunk), which is a lifetime on
18//! every operator's state, on every trait object in the pipeline, and on every queue a chunk is put
19//! into for another thread to pick up. The scheduler is exactly that last thing, so the borrow would
20//! have to outlive a hand off between threads that the compiler has no way to see the end of. The
21//! two ways out of that are unsafe code and a copy at the boundary, and the copy at the boundary is
22//! the thing the borrow existed to avoid.
23//!
24//! So the pin is a [`Pin`], a reference counted handle the buffer holds, and the page stays alive
25//! because the handle is alive rather than because a region ends. It costs one atomic increment per
26//! vector construction, which is not measurable next to reading the page it is protecting, and the
27//! ownership story stays uniform: a chunk is `Send`, always, whatever its columns are pointing at.
28//!
29//! # Why it lands now with one variant
30//!
31//! There is no buffer manager, so there is nothing to borrow from, and writing the borrowed variant
32//! now would be writing an interface against an imaginary caller. What lands now is the enum, with
33//! only the owned variant in it, so that adding the second variant at layer three is a change inside
34//! this crate rather than a change to every signature in the workspace. The reader side of that
35//! migration is already done by the [`Deref`] below: everything outside this crate reads a slice, and
36//! a slice is what both variants will hand back.
37//!
38//! The writer side is [`Buffer::to_mut`], which is the one function that has to grow a case. A write
39//! through a borrowed buffer has to copy the page into an owned run first, which is what `Cow` does
40//! and for the same reason, and having the call site named now means that day is a change to one
41//! function rather than a search for every `push`.
42
43use std::any::Any;
44use std::ops::Deref;
45use std::sync::Arc;
46
47/// What keeps a page alive for as long as a buffer points into it.
48///
49/// Opaque on purpose. The vector does not know what a page is and has no business looking inside
50/// one, it only has to hold the thing that stops the page being evicted, and the buffer manager at
51/// layer three decides what that thing is. The bounds are the load bearing part and they are here
52/// now: `Send` and `Sync`, because a chunk carrying one of these crosses a thread boundary every
53/// time the scheduler moves a pipeline, and `'static`, because a handle with a lifetime on it would
54/// have put the lifetime back on [`Vector`](crate::Vector) by another route.
55pub type Pin = Arc<dyn Any + Send + Sync>;
56
57/// A run of values of one physical type.
58///
59/// Derefs to a slice, which is how every reader in the workspace gets at it, so a reader does not
60/// know or care which variant it is holding. Writers go through [`Self::push`] and
61/// [`Self::to_mut`].
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct Buffer<T> {
64    store: Store<T>,
65}
66
67/// Where the values actually are.
68///
69/// One variant today. The second is a run inside a page, carrying the [`Pin`] that keeps the page
70/// where it is, and it arrives with the buffer manager because that is the only thing that can hand
71/// one out. It is an enum with one arm rather than a newtype around `Vec<T>` because the shape is
72/// what the rest of the workspace is being compiled against, and a newtype that turns into an enum
73/// later is the flag day this exists to avoid.
74#[derive(Debug, Clone, PartialEq, Eq)]
75enum Store<T> {
76    /// The vector owns the values.
77    Owned(Vec<T>),
78}
79
80impl<T> Buffer<T> {
81    /// An empty buffer.
82    #[must_use]
83    pub fn new() -> Self {
84        Self { store: Store::Owned(Vec::new()) }
85    }
86
87    /// An empty buffer with room for `capacity` values.
88    #[must_use]
89    pub fn with_capacity(capacity: usize) -> Self {
90        Self { store: Store::Owned(Vec::with_capacity(capacity)) }
91    }
92
93    /// A buffer owning `values`.
94    #[must_use]
95    pub fn from_vec(values: Vec<T>) -> Self {
96        Self { store: Store::Owned(values) }
97    }
98
99    /// The values.
100    #[must_use]
101    #[inline]
102    pub fn as_slice(&self) -> &[T] {
103        match &self.store {
104            Store::Owned(values) => values,
105        }
106    }
107
108    /// The values as an owned run, copying only if they were not owned already.
109    ///
110    /// Free today because everything is owned. The day it is not, this is one of the two places that
111    /// can copy a page, and it is spelled as a method rather than a field access so that it is
112    /// greppable when that day comes.
113    #[must_use]
114    pub fn into_vec(self) -> Vec<T> {
115        match self.store {
116            Store::Owned(values) => values,
117        }
118    }
119
120    /// The values, writable, copying them out of the page first if they are not owned.
121    ///
122    /// The copy on write point, and the only one. Everything that mutates a buffer goes through
123    /// here, so the borrowed variant needs a case in this function and in nothing else.
124    #[inline]
125    pub fn to_mut(&mut self) -> &mut Vec<T> {
126        match &mut self.store {
127            Store::Owned(values) => values,
128        }
129    }
130
131    /// Appends one value.
132    #[inline]
133    pub fn push(&mut self, value: T) {
134        self.to_mut().push(value);
135    }
136
137    /// Room for `additional` more values, taken in one allocation.
138    pub fn reserve(&mut self, additional: usize) {
139        self.to_mut().reserve(additional);
140    }
141}
142
143impl<T: Clone> Buffer<T> {
144    /// Appends a run of values.
145    pub fn extend_from_slice(&mut self, values: &[T]) {
146        self.to_mut().extend_from_slice(values);
147    }
148}
149
150impl<T> Default for Buffer<T> {
151    fn default() -> Self {
152        Self::new()
153    }
154}
155
156impl<T> Deref for Buffer<T> {
157    type Target = [T];
158
159    #[inline]
160    fn deref(&self) -> &[T] {
161        self.as_slice()
162    }
163}
164
165impl<T> From<Vec<T>> for Buffer<T> {
166    fn from(values: Vec<T>) -> Self {
167        Self::from_vec(values)
168    }
169}
170
171impl<T> FromIterator<T> for Buffer<T> {
172    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
173        Self::from_vec(iter.into_iter().collect())
174    }
175}
176
177impl<T> Extend<T> for Buffer<T> {
178    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
179        self.to_mut().extend(iter);
180    }
181}
182
183impl<T> IntoIterator for Buffer<T> {
184    type Item = T;
185    type IntoIter = std::vec::IntoIter<T>;
186
187    fn into_iter(self) -> Self::IntoIter {
188        self.into_vec().into_iter()
189    }
190}
191
192#[cfg(test)]
193mod tests {
194    use std::sync::Arc;
195
196    use super::{Buffer, Pin};
197
198    #[test]
199    fn a_buffer_reads_back_as_a_slice() {
200        let buffer: Buffer<i32> = vec![1, 2, 3].into();
201        assert_eq!(buffer.as_slice(), &[1, 2, 3]);
202        assert_eq!(buffer.len(), 3);
203        assert_eq!(buffer[1], 2);
204        assert_eq!(buffer.iter().sum::<i32>(), 6);
205        assert_eq!(buffer.clone().into_vec(), vec![1, 2, 3]);
206    }
207
208    #[test]
209    fn writing_goes_through_one_function() {
210        let mut buffer = Buffer::with_capacity(4);
211        buffer.push(1u8);
212        buffer.extend_from_slice(&[2, 3]);
213        buffer.extend([4u8]);
214        buffer.to_mut().sort_unstable_by(|a, b| b.cmp(a));
215        assert_eq!(buffer.as_slice(), &[4, 3, 2, 1]);
216    }
217
218    #[test]
219    fn an_empty_buffer_is_the_default_and_collects_like_a_vector() {
220        assert!(Buffer::<u64>::default().is_empty());
221        assert!(Buffer::<u64>::new().is_empty());
222        let collected: Buffer<u64> = (0..4).collect();
223        assert_eq!(collected.as_slice(), &[0, 1, 2, 3]);
224        assert_eq!(collected.into_iter().count(), 4);
225    }
226
227    /// The property the whole section 3.8 decision is about. A buffer of any payload can be sent to
228    /// another thread without a lifetime being involved, and so can a pin, which is what makes a
229    /// chunk `Send` once the borrowed variant exists. Asserted rather than assumed, because a pin
230    /// that was an `Rc` would compile everywhere else and fail here.
231    #[test]
232    fn a_buffer_and_a_pin_both_cross_a_thread_boundary() {
233        const fn assert_send<T: Send>() {}
234        assert_send::<Buffer<i64>>();
235        assert_send::<Pin>();
236        let pin: Pin = Arc::new(vec![0u8; 8]);
237        let buffer: Buffer<i64> = vec![7; 2].into();
238        let handle = std::thread::spawn(move || (buffer.len(), Arc::strong_count(&pin)));
239        assert_eq!(handle.join().expect("the thread"), (2, 1));
240    }
241}