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

Support allocating iterators with arenas #59533

Merged
merged 3 commits into from
Apr 2, 2019
Merged

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 29, 2019

Split out from #57173.

r? @michaelwoerister

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 29, 2019
@rust-highfive

This comment has been minimized.

src/libarena/lib.rs Outdated Show resolved Hide resolved
@@ -161,21 +186,60 @@ impl<T> TypedArena<T> {
where
T: Copy,
{
unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment would be good here.

@Zoxc Zoxc force-pushed the arena-slices branch 2 times, most recently from 29cef27 to 3c51be5 Compare March 29, 2019 19:40
@Zoxc Zoxc changed the title Support allocating iterators with TypedArena Support allocating iterators with arenas Mar 29, 2019
bors added a commit that referenced this pull request Mar 31, 2019
Introduce an arena type which may be used to allocate a list of types with destructors

You can also specify that you want deserializers for `&'tcx [T]` and `&'tcx T` for a type in the list, which will allocate those using the arena.

Based on #59517 and #59533. Look at the last commit for the interesting changes.

An alternative to #56448. cc @michaelwoerister @eddyb

r? @oli-obk
assert!(len != 0);

if !self.can_allocate(len) {
self.grow(len);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a debug_assert!(self.can_allocate(len)) here, just to be sure.

}
match size_hint {
(min, Some(max)) if min == max => {
if min == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a let len = min; binding here? I think that makes it more readable.

Copy link
Contributor

Choose a reason for hiding this comment

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

@michaelwoerister that seems like an odd use of let bindings... perhaps a comment instead?

Copy link
Member

Choose a reason for hiding this comment

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

I'd do both actually, as in:

// In this branch we know the exact length
let len = min;

let len = vec.len();
let start_ptr = self.alloc_raw_slice(len);
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
mem::forget(vec.drain());
Copy link
Member

Choose a reason for hiding this comment

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

From looking at the implementation of SmallVec, it looks like this does the right thing, but I think it would be clearer to directly call vec.set_len(0) here.

@@ -189,6 +257,7 @@ impl<T> TypedArena<T> {
if let Some(last_chunk) = chunks.last_mut() {
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
let currently_used_cap = used_bytes / mem::size_of::<T>();
last_chunk.entries = currently_used_cap;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this was an existing bug, right? Good find!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There previously wasn't a way to end up with unused memory at the end of a chunk for types with destructors, so not an existing bug.

Copy link
Member

Choose a reason for hiding this comment

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

No? It looks like a similar case can also happen already in alloc_slice:

self.grow(slice.len());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That requires Copy types, so it can't trigger it.

@michaelwoerister
Copy link
Member

Looking good now, thanks @Zoxc!

@bors r+

@bors
Copy link
Contributor

bors commented Apr 2, 2019

📌 Commit 59ff059 has been approved by michaelwoerister

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2019
Centril added a commit to Centril/rust that referenced this pull request Apr 2, 2019
bors added a commit that referenced this pull request Apr 2, 2019
Rollup of 8 pull requests

Successful merges:

 - #59262 (Remove duplicated code from Iterator::{ne, lt, le, gt, ge})
 - #59286 (Refactor async fn return type lowering)
 - #59444 (Implement useful steps_between for all integers)
 - #59452 (Speed up rustdoc run a bit)
 - #59533 (Support allocating iterators with arenas)
 - #59585 (Fixes for shallow borrows)
 - #59607 (Renames `EvalErrorKind` to `InterpError`)
 - #59613 (SGX target: convert a bunch of panics to aborts)

Failed merges:

 - #59630 (Shrink `mir::Statement`.)

r? @ghost
@bors bors merged commit 59ff059 into rust-lang:master Apr 2, 2019
@Zoxc Zoxc deleted the arena-slices branch April 2, 2019 19:01
bors added a commit that referenced this pull request Apr 12, 2019
Introduce an arena type which may be used to allocate a list of types with destructors

You can also specify that you want deserializers for `&'tcx [T]` and `&'tcx T` for a type in the list, which will allocate those using the arena.

Based on #59517 and #59533. Look at the last commit for the interesting changes.

An alternative to #56448. cc @michaelwoerister @eddyb

r? @oli-obk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants