Skip to content
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

im-util: Re-introduce ArrayVec for limit adapter buffering #44

Merged
merged 1 commit into from
Feb 3, 2024

Conversation

jplatte
Copy link
Owner

@jplatte jplatte commented Jan 31, 2024

No description provided.

@jplatte jplatte requested a review from Hywan January 31, 2024 17:37
Copy link
Collaborator

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of this, but I can live with it.

Can we please rename LimitBuf to ArrayBuffer and SortBuf to VecBuffer? It becomes generic and can be re-used by other stream adapters.
If we go that path, we can also renaùe VectorDiffContainerStreamLimitBuf to VectorDiffContainerStreamArrayBuffer and VectorDiffContainerStreamSortBuf to VectorDiffContainerStreamVecBuffer.

I've made a couple of suggestions during the review.

Comment on lines +44 to +51
/// [`VectorDiffContainer`]s' `LimitBuf`.
type VectorDiffContainerStreamLimitBuf<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::LimitBuf;

/// Type alias for extracting the buffer type from a stream of
/// [`VectorDiffContainer`]s' `SortBuf`.
type VectorDiffContainerStreamSortBuf<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::SortBuf;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// [`VectorDiffContainer`]s' `LimitBuf`.
type VectorDiffContainerStreamLimitBuf<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::LimitBuf;
/// Type alias for extracting the buffer type from a stream of
/// [`VectorDiffContainer`]s' `SortBuf`.
type VectorDiffContainerStreamSortBuf<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::SortBuf;
/// [`VectorDiffContainer`]s' `ArrayBuffer`.
type VectorDiffContainerStreamArrayBuffer<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::ArrayBuffer;
/// Type alias for extracting the buffer type from a stream of
/// [`VectorDiffContainer`]s' `VecBuffer`.
type VectorDiffContainerStreamVecBuffer<S> =
<<S as Stream>::Item as VectorDiffContainerOps<VectorDiffContainerStreamElement<S>>>::VecBuffer;

Comment on lines +7 to +8
type LimitBuf: Default;
type SortBuf: Default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type LimitBuf: Default;
type SortBuf: Default;
type ArrayBuffer: Default;
type VecBuffer: Default;

make_diffs: impl FnMut(VectorDiff<T>) -> ArrayVec<VectorDiff<T>, 2>,
) -> Option<Self>;

fn pop_from_limit_buf(buffer: &mut Self::LimitBuf) -> Option<Self>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn pop_from_limit_buf(buffer: &mut Self::LimitBuf) -> Option<Self>;
fn pop_from_array_buffer(buffer: &mut Self::ArrayBuffer) -> Option<Self>;


fn pop_from_limit_buf(buffer: &mut Self::LimitBuf) -> Option<Self>;

fn push_into_sort_buf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn push_into_sort_buf(
fn push_into_vec_buffer(

make_diffs: impl FnMut(VectorDiff<T>) -> SmallVec<[VectorDiff<T>; 2]>,
) -> Option<Self>;

fn pop_from_buffer(buffer: &mut Self::Buffer) -> Option<Self>;
fn pop_from_sort_buf(buffer: &mut Self::SortBuf) -> Option<Self>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn pop_from_sort_buf(buffer: &mut Self::SortBuf) -> Option<Self>;
fn pop_from_vec_buffer(buffer: &mut Self::VecBuffer) -> Option<Self>;

@@ -12,21 +14,30 @@ pub trait VectorDiffContainerOps<T>: Sized {
f: impl FnMut(VectorDiff<T>) -> Option<VectorDiff<U>>,
) -> Option<VectorDiffContainerFamilyMember<Self::Family, U>>;

fn push_into_buffer(
fn push_into_limit_buf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn push_into_limit_buf(
fn push_into_array_buffer(

@jplatte
Copy link
Owner Author

jplatte commented Feb 1, 2024

I am not a fan of this, but I can live with it.

What do you not like? The bit of code duplication in ops.rs? Having two dependencies for similar tasks?

Can we please rename LimitBuf to ArrayBuffer and SortBuf to VecBuffer? It becomes generic and can be re-used by other stream adapters.

I think something like that should only be done once another adapter is actually written that uses the same types. Note that SortBuf is already more general than the current SortBy adapter, because I think we'll add Sort and maybe also SortByKey soon and they'll clearly need the exact same buffering logic.

@Hywan
Copy link
Collaborator

Hywan commented Feb 3, 2024

What do you not like? The bit of code duplication in ops.rs? Having two dependencies for similar tasks?

Yes, the code “duplication” and the two dependencies. I understand why you prefer a static array: it statically asserts that we won't “overflow”/overgrow the buffer. I wish SmallVec could have an inline-only type (in this case, it would strictly be like ArrayVec). I just believe that having a single type would simplify the API of VectorDiffContainerOps. Right now, it seems confusing for a first sight. It's also possible to use both at the same time in the same Stream, which sounds a bit confusing too. What do you think?

It's OK to not agree. In this case, the patch is fine and I'm OK to approve it too.

@jplatte
Copy link
Owner Author

jplatte commented Feb 3, 2024

Time for ArrayVec to be added to the standard library x)

There is actually the tinyvec crate as an alternative that provides both an ArrayVec and TinyVec (like SmallVec), but unfortunately it requires the inner type to implement Default.

Do you think it would make sense to feature-gate the individual adapters (or group of adapters like Filter + FilterMap, Sort + SortBy + SortByKey) in the next breaking-change release? It wouldn't make a difference for the Matrix Rust SDK, but this PR doesn't make a difference for it either (I found both arrayvec and smallvec are already in the dependency graph anyways).

@jplatte jplatte merged commit 5698966 into main Feb 3, 2024
6 checks passed
@jplatte jplatte deleted the jplatte/limit-sort-buf branch February 3, 2024 18:56
@Hywan
Copy link
Collaborator

Hywan commented Feb 5, 2024

I don't believe it makes sense to feature-flag them for the moment. We don't have a lot of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants