-
Notifications
You must be signed in to change notification settings - Fork 193
Further trait reorganization #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frankmcsherry
merged 2 commits into
TimelyDataflow:master
from
frankmcsherry:further_reorganization
Nov 23, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,11 +52,12 @@ pub mod ord_neu; | |
pub use self::ord::OrdValSpine as ValSpine; | ||
pub use self::ord::OrdKeySpine as KeySpine; | ||
|
||
use std::ops::{Add, Sub}; | ||
use std::convert::{TryInto, TryFrom}; | ||
|
||
use timely::container::columnation::{Columnation, TimelyStack}; | ||
use lattice::Lattice; | ||
use difference::Semigroup; | ||
use trace::layers::BatchContainer; | ||
use trace::layers::ordered::OrdOffset; | ||
|
||
/// A type that names constituent update types. | ||
pub trait Update { | ||
|
@@ -167,3 +168,251 @@ impl<T: Columnation> RetainFrom<T> for TimelyStack<T> { | |
}) | ||
} | ||
} | ||
|
||
/// Trait for types used as offsets into an ordered layer. | ||
/// This is usually `usize`, but `u32` can also be used in applications | ||
/// where huge batches do not occur to reduce metadata size. | ||
pub trait OrdOffset: Copy + PartialEq + Add<Output=Self> + Sub<Output=Self> + TryFrom<usize> + TryInto<usize> | ||
{} | ||
|
||
impl<O> OrdOffset for O | ||
where | ||
O: Copy + PartialEq + Add<Output=Self> + Sub<Output=Self> + TryFrom<usize> + TryInto<usize>, | ||
{} | ||
|
||
pub use self::containers::{BatchContainer, SliceContainer}; | ||
|
||
/// Containers for data that resemble `Vec<T>`, with leaner implementations. | ||
pub mod containers { | ||
|
||
use timely::container::columnation::{Columnation, TimelyStack}; | ||
|
||
use std::borrow::{Borrow, ToOwned}; | ||
|
||
/// A general-purpose container resembling `Vec<T>`. | ||
pub trait BatchContainer: Default { | ||
/// The type of contained item. | ||
/// | ||
/// The container only supplies references to the item, so it needn't be sized. | ||
type Item: ?Sized; | ||
/// Inserts an owned item. | ||
fn push(&mut self, item: <Self::Item as ToOwned>::Owned) where Self::Item: ToOwned; | ||
/// Inserts a borrowed item. | ||
fn copy(&mut self, item: &Self::Item); | ||
/// Extends from a slice of items. | ||
fn copy_slice(&mut self, slice: &[<Self::Item as ToOwned>::Owned]) where Self::Item: ToOwned; | ||
/// Extends from a range of items in another`Self`. | ||
fn copy_range(&mut self, other: &Self, start: usize, end: usize); | ||
/// Creates a new container with sufficient capacity. | ||
fn with_capacity(size: usize) -> Self; | ||
/// Reserves additional capacity. | ||
fn reserve(&mut self, additional: usize); | ||
/// Creates a new container with sufficient capacity. | ||
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self; | ||
|
||
/// Reference to the element at this position. | ||
fn index(&self, index: usize) -> &Self::Item; | ||
/// Number of contained elements | ||
fn len(&self) -> usize; | ||
/// Returns the last item if the container is non-empty. | ||
fn last(&self) -> Option<&Self::Item> { | ||
if self.len() > 0 { | ||
Some(self.index(self.len()-1)) | ||
} | ||
else { | ||
None | ||
} | ||
} | ||
|
||
/// Reports the number of elements satisfing the predicate. | ||
/// | ||
/// This methods *relies strongly* on the assumption that the predicate | ||
/// stays false once it becomes false, a joint property of the predicate | ||
/// and the layout of `Self. This allows `advance` to use exponential search to | ||
/// count the number of elements in time logarithmic in the result. | ||
fn advance<F: Fn(&Self::Item)->bool>(&self, start: usize, end: usize, function: F) -> usize { | ||
|
||
let small_limit = 8; | ||
|
||
// Exponential seach if the answer isn't within `small_limit`. | ||
if end > start + small_limit && function(self.index(start + small_limit)) { | ||
|
||
// start with no advance | ||
let mut index = small_limit + 1; | ||
if start + index < end && function(self.index(start + index)) { | ||
|
||
// advance in exponentially growing steps. | ||
let mut step = 1; | ||
while start + index + step < end && function(self.index(start + index + step)) { | ||
index += step; | ||
step = step << 1; | ||
} | ||
|
||
// advance in exponentially shrinking steps. | ||
step = step >> 1; | ||
while step > 0 { | ||
if start + index + step < end && function(self.index(start + index + step)) { | ||
index += step; | ||
} | ||
step = step >> 1; | ||
} | ||
|
||
index += 1; | ||
} | ||
|
||
index | ||
} | ||
else { | ||
let limit = std::cmp::min(end, start + small_limit); | ||
(start .. limit).filter(|x| function(self.index(*x))).count() | ||
} | ||
} | ||
} | ||
|
||
// All `T: Clone` also implement `ToOwned<Owned = T>`, but without the constraint Rust | ||
// struggles to understand why the owned type must be `T` (i.e. the one blanket impl). | ||
impl<T: Clone + ToOwned<Owned = T>> BatchContainer for Vec<T> { | ||
type Item = T; | ||
fn push(&mut self, item: T) { | ||
self.push(item); | ||
} | ||
fn copy(&mut self, item: &T) { | ||
self.push(item.clone()); | ||
} | ||
fn copy_slice(&mut self, slice: &[T]) where T: Sized { | ||
self.extend_from_slice(slice); | ||
} | ||
fn copy_range(&mut self, other: &Self, start: usize, end: usize) { | ||
self.extend_from_slice(&other[start .. end]); | ||
} | ||
fn with_capacity(size: usize) -> Self { | ||
Vec::with_capacity(size) | ||
} | ||
fn reserve(&mut self, additional: usize) { | ||
self.reserve(additional); | ||
} | ||
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self { | ||
Vec::with_capacity(cont1.len() + cont2.len()) | ||
} | ||
fn index(&self, index: usize) -> &Self::Item { | ||
&self[index] | ||
} | ||
fn len(&self) -> usize { | ||
self[..].len() | ||
} | ||
} | ||
|
||
// The `ToOwned` requirement exists to satisfy `self.reserve_items`, who must for now | ||
// be presented with the actual contained type, rather than a type that borrows into it. | ||
impl<T: Columnation + ToOwned<Owned = T>> BatchContainer for TimelyStack<T> { | ||
type Item = T; | ||
fn push(&mut self, item: <Self::Item as ToOwned>::Owned) where Self::Item: ToOwned { | ||
self.copy(item.borrow()); | ||
} | ||
fn copy(&mut self, item: &T) { | ||
self.copy(item); | ||
} | ||
fn copy_slice(&mut self, slice: &[<Self::Item as ToOwned>::Owned]) where Self::Item: ToOwned { | ||
self.reserve_items(slice.iter()); | ||
for item in slice.iter() { | ||
self.copy(item); | ||
} | ||
} | ||
fn copy_range(&mut self, other: &Self, start: usize, end: usize) { | ||
let slice = &other[start .. end]; | ||
self.reserve_items(slice.iter()); | ||
for item in slice.iter() { | ||
self.copy(item); | ||
} | ||
} | ||
fn with_capacity(size: usize) -> Self { | ||
Self::with_capacity(size) | ||
} | ||
fn reserve(&mut self, _additional: usize) { | ||
} | ||
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self { | ||
let mut new = Self::default(); | ||
new.reserve_regions(std::iter::once(cont1).chain(std::iter::once(cont2))); | ||
new | ||
} | ||
fn index(&self, index: usize) -> &Self::Item { | ||
&self[index] | ||
} | ||
fn len(&self) -> usize { | ||
self[..].len() | ||
} | ||
} | ||
|
||
/// A container that accepts slices `[B::Item]`. | ||
pub struct SliceContainer<B> { | ||
/// Offsets that bound each contained slice. | ||
/// | ||
/// The length will be one greater than the number of contained slices, | ||
/// starting with zero and ending with `self.inner.len()`. | ||
pub offsets: Vec<usize>, | ||
/// An inner container for sequences of `B` that dereferences to a slice. | ||
pub inner: Vec<B>, | ||
} | ||
|
||
impl<B: Default> BatchContainer for SliceContainer<B> | ||
where | ||
B: Clone + Sized, | ||
[B]: ToOwned<Owned = Vec<B>>, | ||
{ | ||
type Item = [B]; | ||
fn push(&mut self, item: Vec<B>) where Self::Item: ToOwned { | ||
for x in item.into_iter() { | ||
self.inner.push(x); | ||
} | ||
self.offsets.push(self.inner.len()); | ||
} | ||
fn copy(&mut self, item: &Self::Item) { | ||
for x in item.iter() { | ||
self.inner.copy(x); | ||
} | ||
self.offsets.push(self.inner.len()); | ||
} | ||
fn copy_slice(&mut self, slice: &[Vec<B>]) where Self::Item: ToOwned { | ||
for item in slice { | ||
self.copy(item); | ||
} | ||
} | ||
fn copy_range(&mut self, other: &Self, start: usize, end: usize) { | ||
for index in start .. end { | ||
self.copy(other.index(index)); | ||
} | ||
} | ||
fn with_capacity(size: usize) -> Self { | ||
Self { | ||
offsets: Vec::with_capacity(size), | ||
inner: Vec::with_capacity(size), | ||
} | ||
} | ||
fn reserve(&mut self, _additional: usize) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to @antiguru: we could have a slightly better implementation here to pre-size the local part of self, but I can't recall the available apis out of my head. |
||
} | ||
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self { | ||
Self { | ||
offsets: Vec::with_capacity(cont1.offsets.len() + cont2.offsets.len()), | ||
inner: Vec::with_capacity(cont1.inner.len() + cont2.inner.len()), | ||
} | ||
} | ||
fn index(&self, index: usize) -> &Self::Item { | ||
let lower = self.offsets[index]; | ||
let upper = self.offsets[index+1]; | ||
&self.inner[lower .. upper] | ||
} | ||
fn len(&self) -> usize { | ||
self.offsets.len() - 1 | ||
} | ||
} | ||
|
||
/// Default implementation introduces a first offset. | ||
impl<B: Default> Default for SliceContainer<B> { | ||
fn default() -> Self { | ||
Self { | ||
offsets: vec![0], | ||
inner: Default::default(), | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should revisit the type requirements on region and timely stack!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! I'm not sure if it really helps here, but e.g. if I have a bunch of
Vec<u8>
and you are able to only maintain[u8]
shaped data, that could help. Or we could conclude that this type does not do that, and not be too stressed! :D