-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: develop
Are you sure you want to change the base?
Conversation
match capacity.checked_mul(2) { | ||
Some(cap) => cap, | ||
None => capacity, | ||
} |
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.
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 { |
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.
It's not clear to me what we gain by removing the generic parameter here.
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.
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
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.
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.
#[inline] | ||
#[allow(clippy::cast_ptr_alignment)] | ||
fn is_default(buf: core::ptr::NonNull<u8>) -> bool { | ||
core::ptr::eq(buf.as_ptr(), &DEFAULT_U8) | ||
} |
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.
I don't understand the purpose of this refactor either.
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.
To use outside of generic context
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.
To try and reduce code bloat again?
struct Grow { | ||
buf: core::ptr::NonNull<u8>, | ||
old_capacity: usize, | ||
new_capacity: usize, | ||
alignment: usize, | ||
len: usize, | ||
type_size: usize, | ||
} |
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.
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.
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.
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
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.
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.
#[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"); | ||
} |
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.
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.
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.
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.
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.
This PR makes following changes:
Fixes #32