Skip to content

Commit fd13bdf

Browse files
committed
vec fixes for huonw
1 parent b53406f commit fd13bdf

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

src/doc/tarpl/vec-alloc.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ like the standard library as much as possible, so we'll just kill the whole
6161
program.
6262

6363
We said we don't want to use intrinsics, so doing *exactly* what `std` does is
64-
out. `std::rt::util::abort` actually exists, but it takes a message to print,
65-
which will probably allocate. Also it's still unstable. Instead, we'll call
66-
`std::process::exit` with some random number.
64+
out. Instead, we'll call `std::process::exit` with some random number.
6765

6866
```rust
6967
fn oom() {
@@ -78,7 +76,7 @@ if cap == 0:
7876
allocate()
7977
cap = 1
8078
else:
81-
reallocate
79+
reallocate()
8280
cap *= 2
8381
```
8482

@@ -109,7 +107,7 @@ the same location in memory, the operations need to be done to the same value,
109107
and they can't just be merged afterwards.
110108

111109
When you use GEP inbounds, you are specifically telling LLVM that the offsets
112-
you're about to do are within the bounds of a single allocated entity. The
110+
you're about to do are within the bounds of a single "allocated" entity. The
113111
ultimate payoff being that LLVM can assume that if two pointers are known to
114112
point to two disjoint objects, all the offsets of those pointers are *also*
115113
known to not alias (because you won't just end up in some random place in
@@ -162,7 +160,8 @@ elements. This is a runtime no-op because every element takes up no space,
162160
and it's fine to pretend that there's infinite zero-sized types allocated
163161
at `0x01`. No allocator will ever allocate that address, because they won't
164162
allocate `0x00` and they generally allocate to some minimal alignment higher
165-
than a byte.
163+
than a byte. Also generally the whole first page of memory is
164+
protected from being allocated anyway (a whole 4k, on many platforms).
166165

167166
However what about for positive-sized types? That one's a bit trickier. In
168167
principle, you can argue that offsetting by 0 gives LLVM no information: either

src/doc/tarpl/vec-drain.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl<T> Vec<T> {
8383
pub fn into_iter(self) -> IntoIter<T> {
8484
unsafe {
8585
let iter = RawValIter::new(&self);
86+
8687
let buf = ptr::read(&self.buf);
8788
mem::forget(self);
8889
@@ -112,7 +113,7 @@ pub struct Drain<'a, T: 'a> {
112113
113114
impl<'a, T> Iterator for Drain<'a, T> {
114115
type Item = T;
115-
fn next(&mut self) -> Option<T> { self.iter.next_back() }
116+
fn next(&mut self) -> Option<T> { self.iter.next() }
116117
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
117118
}
118119

src/doc/tarpl/vec-layout.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
% Layout
22

3-
First off, we need to come up with the struct layout. Naively we want this
4-
design:
3+
First off, we need to come up with the struct layout. A Vec has three parts:
4+
a pointer to the allocation, the size of the allocation, and the number of
5+
elements that have been initialized.
6+
7+
Naively, this means we just want this design:
58

69
```rust
710
pub struct Vec<T> {

src/doc/tarpl/vec-raw.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
% RawVec
22

33
We've actually reached an interesting situation here: we've duplicated the logic
4-
for specifying a buffer and freeing its memory. Now that we've implemented it
5-
and identified *actual* logic duplication, this is a good time to perform some
6-
logic compression.
4+
for specifying a buffer and freeing its memory in Vec and IntoIter. Now that
5+
we've implemented it and identified *actual* logic duplication, this is a good
6+
time to perform some logic compression.
77

88
We're going to abstract out the `(ptr, cap)` pair and give them the logic for
99
allocating, growing, and freeing:
@@ -64,7 +64,7 @@ impl<T> Drop for RawVec<T> {
6464
}
6565
```
6666

67-
And change vec as follows:
67+
And change Vec as follows:
6868

6969
```rust,ignore
7070
pub struct Vec<T> {

src/doc/tarpl/vec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bit nicer or efficient because intrinsics are permanently unstable. Although
1212
many intrinsics *do* become stabilized elsewhere (`std::ptr` and `str::mem`
1313
consist of many intrinsics).
1414

15-
Ultimately this means out implementation may not take advantage of all
15+
Ultimately this means our implementation may not take advantage of all
1616
possible optimizations, though it will be by no means *naive*. We will
1717
definitely get into the weeds over nitty-gritty details, even
1818
when the problem doesn't *really* merit it.

0 commit comments

Comments
 (0)