-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add Vec visualization to understand capacity #76066
Changes from 8 commits
9c1415e
ea337c6
f2bd46b
53af7b8
a919516
55ad7f2
44e9b7f
6900744
79918c1
62ba5b1
702662e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,24 @@ use crate::raw_vec::RawVec; | |
/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`] | ||
/// whenever possible to specify how big the vector is expected to get. | ||
/// | ||
/// A vector containing the elements `'a'` and `'b'` with capacity 4 can be visualized as: | ||
/// ```text | ||
/// Stack ptr capacity len | ||
/// +--------+--------+--------+ | ||
/// | 0x0123 | 4 | 2 | | ||
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.
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. This is intentionally different, see #76066 (comment). 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. @jyn514 That comment suggests to use a different layout than the real layout. The 'real' layout is (RawVec, len), which is effectively a (ptr, cap, len). Changing it to (ptr, len, cap) as I suggest does make it different from the actual layout, just like the8472 asked for. 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. Weird, I didn't notice this, I thought I used |
||
/// +--------+--------+--------+ | ||
LukasKalbertodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | | ||
/// v | ||
/// Heap +--------+--------+--------+--------+ | ||
/// | 1 | 2 | uninit | uninit | | ||
pickfire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// +--------+--------+--------+--------+ | ||
pickfire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ``` | ||
/// | ||
/// - **uninit** represents memory that is not initialized, see [`MaybeUninit`]. | ||
/// - Note: the ABI is not stable and `Vec` makes no guarantees about its memory | ||
/// layout (including the order of fields). See [the section about | ||
/// guarantees](#guarantees). | ||
Comment on lines
+218
to
+220
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. The layout of the part on the heap is pretty strict. Let's make clear it's only the Vec object itself (the (ptr, size, cap) part) that's unspecified. A mention of 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. Maybe you can suggest a change here? |
||
/// | ||
/// # Guarantees | ||
/// | ||
/// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees | ||
|
@@ -287,6 +305,7 @@ use crate::raw_vec::RawVec; | |
/// [`insert`]: Vec::insert | ||
/// [`reserve`]: Vec::reserve | ||
/// [owned slice]: Box | ||
/// [`MaybeUninit`]: core::mem::MaybeUninit | ||
pickfire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_type")] | ||
pub struct Vec<T> { | ||
|
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.
"Stack" is not correct. Those three fields don't have to live on the stack, e.g.
Box<Vec<T>>
.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.
Well,
Box<Vec<T>>
is not good, I believe we already mentioned this somewhere in the book,Vec
itself is already sort of a smart pointer, no point wrapping box around. But in this case it is talking aboutVec<T>
which it is on the stack. I think we don't need to add note on this since this is aBox
thing, if we want to add it should be inBox
.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.
A vec can also live inside some other struct that lives on the heap, e.g. an
Arc
. Maybe just call it "struct"?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.
But doesn't
struct
andheap
when used together feels weird?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.
Maybe just remove "Stack". We simply don't know where those three values live. We just know that the lower box lives on the heap.
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.
How about
Stack/Heap
? Since it could be either stack or heap.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.
Try 'Layout' instead, maybe?
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.
Maybe something like "The Vec object itself" and "The data on the heap".
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.
Layout isn't clear where it will be.
Too long. Also having "object" word itself can confuse a lot of people. "The data on the heap" is incorrect, since it could be in the stack too.