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

Introduce try_reserve friends #33

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

DoumanAsh
Copy link
Contributor

@DoumanAsh DoumanAsh commented Dec 3, 2021

This PR makes following changes:

  1. Extract allocation/reallocation into separate non-generic code
  2. Make allocation panic safe
  3. Introduce try_reserve/try_reserve_exact

Fixes #32

Comment on lines +25 to +28
match capacity.checked_mul(2) {
Some(cap) => cap,
None => capacity,
}
Copy link
Owner

Choose a reason for hiding this comment

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

This is a good catch

@@ -31,13 +34,13 @@ pub fn max_align<T>() -> usize {
core::cmp::max(align_t, header_align)
}

pub fn make_layout<T>(capacity: usize, alignment: usize) -> alloc::alloc::Layout {
pub fn make_layout(capacity: usize, alignment: usize, type_size: usize) -> alloc::alloc::Layout {
Copy link
Owner

Choose a reason for hiding this comment

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

It's not clear to me what we gain by removing the generic parameter here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It removes number of instantiations for function, reducing compile times.
If you're curious std library does the same https://github.com/rust-lang/rust/blob/207c80f105282245d93024c95ac408c622f70114/library/alloc/src/raw_vec.rs#L442-L445

By extracting as much code out of generics, you improve compile times automatically equivalent to number of possible T in your code

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm.

Alright, we can explore this later.

This PR should focus solely on adding the two new associated functions and the tests.

If we want to work on reducing code bloat, we should first get some metrics set up and then we'll know if our refactors are actually doing anything or not.

Comment on lines +80 to +84
#[inline]
#[allow(clippy::cast_ptr_alignment)]
fn is_default(buf: core::ptr::NonNull<u8>) -> bool {
core::ptr::eq(buf.as_ptr(), &DEFAULT_U8)
}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't understand the purpose of this refactor either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To use outside of generic context

Copy link
Owner

Choose a reason for hiding this comment

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

To try and reduce code bloat again?

Comment on lines +92 to +99
struct Grow {
buf: core::ptr::NonNull<u8>,
old_capacity: usize,
new_capacity: usize,
alignment: usize,
len: usize,
type_size: usize,
}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't get why we have a Grow struct that also contains a type_size.

I'm not sure I understand this refactoring in general.

We should basically just update fn grow() to return a Option<()> instead of what we're doing now which is invoking handle_alloc_error.

To this end, the current usages just need to match on grow()'s new return type and invoke handle_alloc_error there and then the try_reserve_* associated functions can match on the None and return the Result type.

This should be largely good enough because any other allocation failures (for example, our layout is invalid) would be caused by bugs in the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This Grow struct here is to just to make sure not to make mistake when passing arguments to grow function as I moved it outside of Vec as part of refactoring.

This is ofc your choice if you do not need refactoring to remove unneeded generic code.
As you said it would be simply done by refactoring original grow function

Copy link
Owner

Choose a reason for hiding this comment

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

To me it seems silly to create a struct just to give it an impl with a single associated function.

This pattern, imo, fits better for DropGuard-like types.

Comment on lines +1266 to +1270
#[test]
fn minivec_should_fail_try_reserve_impossible() {
let mut buf = minivec::mini_vec![0; 512];
buf.try_reserve(usize::max_value()).expect_err("FAIL");
}
Copy link
Owner

Choose a reason for hiding this comment

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

This test is incomplete.

We've tested failure which is good but we need to test the success arm as well.

I also don't see try_reserve_exact() here either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

This is good but we also need to comment back in the tests in vec.rs as well. Seems like there's a good bit of tests the stdlib has there.

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.

Implement try_reserve()
2 participants