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

Make TypedArena::alloc_from_iter like DroplessArena::alloc_from_iter. #116327

Closed
wants to merge 2 commits into from

Conversation

nnethercote
Copy link
Contributor

Although they do similar things, they are currently implemented differently, for no particular reason.

r? @cjgillot

…er`.

`TypedArena` and `DroplessArena` both have an `alloc_from_impl` function, but
they are implemented in completely different ways.

- `TypedArena` uses the `IterExt` trait, which by default (a) collects the
  iterator's elements into a temporary `SmallVec`, (b) allocates memory, and
  (c) bulk-copies (and consumes) the elements into the arena memory. There are
  also specializations for `array::IntoIter`/`Vec`/`SmallVec` that
  avoid copying into a temporary `SmallVec`.

- `DroplessArena` has two ways of doing it:
  - For known-sized iterators, it allocates memory and then copies items
    in one at a time.
 -  Otherwise, it works much like the default case for `TypedArena`, collecting
    into a temporary `SmallVec` and then bulk-copying.

Performance-wise, they are very similar. `DroplessArena` is a little
more general, because it automatically works with any known-sized
iterator type, while `TypedArena`'s approach requires specialization on
a per-type basis.

This commit removes `IterExt` and moves
`DroplessArena::{write,alloc}_from_iter` so they can be used from
`TypedArena`. This reduces the amount of code in this file.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 2, 2023
@nnethercote
Copy link
Contributor Author

I'm not expecting any performance changes.

@bors try @rust-timer queue

cc @Zoxc

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 2, 2023
@bors
Copy link
Contributor

bors commented Oct 2, 2023

⌛ Trying commit 2c40e82 with merge 9f6a230...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 2, 2023
Make `TypedArena::alloc_from_iter` like `DroplessArena::alloc_from_iter`.

Although they do similar things, they are currently implemented differently, for no particular reason.

r? `@cjgillot`
@Zoxc
Copy link
Contributor

Zoxc commented Oct 2, 2023

They can't use the same code as the allocated slice is not initialized when Iterator::next is called, which means TypedArena could drop uninitialized memory if Iterator::next unwinds. alloc_raw_slice should probably be unsafe to make that more clear.

You could look into reverting #78569 if you want this to be cleaner.

@bors
Copy link
Contributor

bors commented Oct 2, 2023

☀️ Try build successful - checks-actions
Build commit: 9f6a230 (9f6a2301d7b07859dbfd7b834abefc36ba018d05)

@rust-timer

This comment has been minimized.

@nnethercote
Copy link
Contributor Author

They can't use the same code as the allocated slice is not initialized when Iterator::next is called, which means TypedArena could drop uninitialized memory if Iterator::next unwinds.

Are you sure? ArenaChunk::storage is a NonNull<[MaybeUninit<T>]>. AIUI, if a panic occurs during the iteration the already-written elements won't be dropped, which is a memory leak but not a memory safety issue. Much like the example from the docs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9f6a230): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
7.4% [6.9%, 8.0%] 6
Improvements ✅
(primary)
-0.5% [-0.7%, -0.4%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-0.7%, -0.4%] 2

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.6%, 0.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.3% [-1.3%, -1.3%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 626.835s -> 626.335s (-0.08%)
Artifact size: 273.31 MiB -> 273.34 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 2, 2023
@Zoxc
Copy link
Contributor

Zoxc commented Oct 2, 2023

The TypedArena Drop impl will always destroy the elements allocated by alloc_raw_slice.

@nnethercote
Copy link
Contributor Author

You could look into reverting #78569 if you want this to be cleaner.

Good suggestion. I've done this in #116370. The performance wins in #78569 were basically non-existent at the time and my measurements from today were much the same.

@nnethercote
Copy link
Contributor Author

See #116370 for a better approach.

@nnethercote nnethercote closed this Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants