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

Take more advantage of the isize::MAX limit in Layout #129845

Merged
merged 1 commit into from
Sep 18, 2024

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Sep 1, 2024

Things like padding_needed_for are current implemented being super careful to handle things like Layout::size potentially being usize::MAX.

But now that #95295 has happened, that's no longer a concern. It's possible to add two Layout::sizes together without risking overflow now.

So take advantage of that to remove a bunch of checked math that's not actually needed. For example, the round-up-and-add-next-size in extend doesn't need any overflow checks at all, just the final check for compatibility with the alignment.

(And while I was doing that I made it all unstably const, because there's nothing in Layout that's fundamentally runtime-only.)

@rustbot
Copy link
Collaborator

rustbot commented Sep 1, 2024

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 1, 2024
@scottmcm
Copy link
Member Author

scottmcm commented Sep 1, 2024

@bors try @rust-timer queue

@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 Sep 1, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 1, 2024
Take more advantage of the `isize::MAX` limit in `Layout`

Things like `padding_needed_for` are current implemented being super careful to handle things like `Layout::size` potentially being `usize::MAX`.

But now that rust-lang#95295 has happened, that's no longer a concern.  It's possible to add two `Layout::size`s together without risking overflow now.

So this PR adds a wrapper type to allow doing that kind of thing in safe code while still telling LLVM it can't overflow.
@bors
Copy link
Contributor

bors commented Sep 1, 2024

⌛ Trying commit 3fcde40 with merge 3e6e03e...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 1, 2024

☀️ Try build successful - checks-actions
Build commit: 3e6e03e (3e6e03ebf4b270a06fb94686ff63919ccffd2157)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3e6e03e): 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.8% [0.2%, 2.5%] 65
Regressions ❌
(secondary)
1.1% [0.3%, 6.0%] 47
Improvements ✅
(primary)
-1.0% [-1.4%, -0.5%] 2
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.3%] 2
All ❌✅ (primary) 0.8% [-1.4%, 2.5%] 67

Max RSS (memory usage)

Results (primary 0.3%, secondary 0.1%)

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)
1.3% [0.4%, 12.0%] 46
Regressions ❌
(secondary)
1.0% [0.4%, 3.3%] 51
Improvements ✅
(primary)
-1.2% [-9.5%, -0.4%] 34
Improvements ✅
(secondary)
-0.9% [-2.3%, -0.4%] 48
All ❌✅ (primary) 0.3% [-9.5%, 12.0%] 80

Cycles

Results (primary 0.8%, secondary 0.6%)

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)
1.0% [0.4%, 3.8%] 105
Regressions ❌
(secondary)
1.0% [0.4%, 7.3%] 124
Improvements ✅
(primary)
-1.8% [-4.6%, -0.4%] 7
Improvements ✅
(secondary)
-1.2% [-5.6%, -0.4%] 33
All ❌✅ (primary) 0.8% [-4.6%, 3.8%] 112

Binary size

Results (primary 0.6%, secondary 1.6%)

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.7% [0.0%, 5.4%] 49
Regressions ❌
(secondary)
2.4% [1.5%, 3.4%] 11
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 9
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 5
All ❌✅ (primary) 0.6% [-0.1%, 5.4%] 58

Bootstrap: 789.43s -> 800.421s (1.39%)
Artifact size: 338.46 MiB -> 338.68 MiB (0.06%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 1, 2024
@scottmcm
Copy link
Member Author

scottmcm commented Sep 1, 2024

@bors try @rust-timer queue

@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 Sep 1, 2024
@bors
Copy link
Contributor

bors commented Sep 1, 2024

⌛ Trying commit 972ff01 with merge 9c711ef...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 1, 2024
Take more advantage of the `isize::MAX` limit in `Layout`

Things like `padding_needed_for` are current implemented being super careful to handle things like `Layout::size` potentially being `usize::MAX`.

But now that rust-lang#95295 has happened, that's no longer a concern.  It's possible to add two `Layout::size`s together without risking overflow now.

So this PR adds a wrapper type to allow doing that kind of thing in safe code while still telling LLVM it can't overflow.
@bors
Copy link
Contributor

bors commented Sep 1, 2024

☀️ Try build successful - checks-actions
Build commit: 9c711ef (9c711ef9097c622d73290d954556a9597f541f93)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9c711ef): 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)
1.0% [0.2%, 3.0%] 57
Regressions ❌
(secondary)
1.5% [0.3%, 6.2%] 28
Improvements ✅
(primary)
-1.4% [-1.4%, -1.4%] 1
Improvements ✅
(secondary)
-0.5% [-0.8%, -0.3%] 15
All ❌✅ (primary) 1.0% [-1.4%, 3.0%] 58

Max RSS (memory usage)

Results (primary -0.2%, secondary -0.6%)

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)
1.4% [0.4%, 11.2%] 29
Regressions ❌
(secondary)
0.7% [0.4%, 1.3%] 18
Improvements ✅
(primary)
-1.1% [-9.6%, -0.4%] 51
Improvements ✅
(secondary)
-0.9% [-3.5%, -0.4%] 77
All ❌✅ (primary) -0.2% [-9.6%, 11.2%] 80

Cycles

Results (primary 0.6%, secondary 0.4%)

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)
1.1% [0.4%, 3.3%] 67
Regressions ❌
(secondary)
1.4% [0.4%, 6.8%] 71
Improvements ✅
(primary)
-1.3% [-6.5%, -0.4%] 16
Improvements ✅
(secondary)
-0.9% [-5.6%, -0.4%] 52
All ❌✅ (primary) 0.6% [-6.5%, 3.3%] 83

Binary size

Results (primary 0.6%, secondary 1.6%)

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.7% [0.0%, 5.5%] 50
Regressions ❌
(secondary)
2.4% [1.5%, 3.4%] 11
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 9
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 5
All ❌✅ (primary) 0.6% [-0.1%, 5.5%] 59

Bootstrap: 787.22s -> 796.262s (1.15%)
Artifact size: 338.38 MiB -> 338.74 MiB (0.11%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 2, 2024
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

scottmcm commented Sep 2, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@scottmcm scottmcm added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 2, 2024
@bors
Copy link
Contributor

bors commented Sep 2, 2024

☀️ Try build successful - checks-actions
Build commit: c8e94c4 (c8e94c4117a6137fc79bbc3baa14733caf2fda8c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c8e94c4): 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)
3.4% [0.4%, 8.4%] 3
Improvements ✅
(primary)
-0.3% [-0.4%, -0.3%] 5
Improvements ✅
(secondary)
-0.7% [-1.3%, -0.3%] 4
All ❌✅ (primary) -0.3% [-0.4%, -0.3%] 5

Max RSS (memory usage)

Results (primary -0.1%, secondary -0.9%)

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)
1.5% [0.4%, 8.1%] 22
Regressions ❌
(secondary)
0.8% [0.4%, 1.6%] 12
Improvements ✅
(primary)
-0.8% [-3.0%, -0.4%] 53
Improvements ✅
(secondary)
-1.0% [-3.0%, -0.4%] 117
All ❌✅ (primary) -0.1% [-3.0%, 8.1%] 75

Cycles

Results (primary -0.5%, secondary -0.4%)

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)
1.1% [0.4%, 2.5%] 8
Regressions ❌
(secondary)
1.1% [0.4%, 4.3%] 33
Improvements ✅
(primary)
-0.8% [-1.9%, -0.4%] 52
Improvements ✅
(secondary)
-0.9% [-3.4%, -0.4%] 105
All ❌✅ (primary) -0.5% [-1.9%, 2.5%] 60

Binary size

Results (primary -0.0%)

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.8% [0.8%, 0.8%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.9% [-0.9%, -0.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.9%, 0.8%] 2

Bootstrap: 751.284s -> 755.684s (0.59%)
Artifact size: 338.27 MiB -> 338.31 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 2, 2024
@scottmcm
Copy link
Member Author

scottmcm commented Sep 2, 2024

Ah, there we go. #129845 (comment) looks great.

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 2, 2024
@scottmcm scottmcm marked this pull request as ready for review September 2, 2024 20:00
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 2, 2024
Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

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

two comments, which you may optionally address

// The safe constructor is called here to enforce the isize size limit.
Layout::from_size_alignment(new_size, self.align)
pub const fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
if let Some(new_size) = self.size.checked_add(next.size) {
Copy link
Member

Choose a reason for hiding this comment

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

This can't overflow, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

True!

/// because the original size is at most `isize::MAX`.
#[inline]
const fn size_rounded_up_to_custom_align(&self, align: Alignment) -> usize {
let len = self.size;
Copy link
Member

Choose a reason for hiding this comment

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

this variable with a slighty confusing name is used once, can we just remove it and call it "size" instead of "len" everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I'd left it because it was in the old code, but apparently I then refactored enough that the diff doesn't notice that, so might as well just remove it as you say.

@Noratrieb Noratrieb added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 15, 2024
@scottmcm
Copy link
Member Author

@bors r=Noratrieb

@bors
Copy link
Contributor

bors commented Sep 18, 2024

📌 Commit b824dc4 has been approved by Noratrieb

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 18, 2024
Things like `padding_needed_for` are current implemented being super careful to handle things like `Layout::size` potentially being `usize::MAX`.

But now that 95295 has happened, that's no longer a concern.  It's possible to add two `Layout::size`s together without risking overflow now.

So take advantage of that to remove a bunch of checked math that's not actually needed.  For example, the round-up-and-add-next-size in `extend` doesn't need any overflow checks at all, just the final check for compatibility with the alignment.

(And while I was doing that I made it all unstably const, because there's nothing in `Layout` that's fundamentally runtime-only.)
@scottmcm
Copy link
Member Author

Diff fixing the PR comments: https://github.com/rust-lang/rust/compare/7ccc088c808f0ba169824dd9f6cdd83000a8b8ac..18ca8bf8ee77deb13967620b1f4829f22c16dae1

(multiple pushes because apparently I'm bad at updating comments)

@bors r=Noratrieb

@bors
Copy link
Contributor

bors commented Sep 18, 2024

📌 Commit 18ca8bf has been approved by Noratrieb

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 18, 2024

⌛ Testing commit 18ca8bf with merge f68c28b...

@bors
Copy link
Contributor

bors commented Sep 18, 2024

☀️ Test successful - checks-actions
Approved by: Noratrieb
Pushing f68c28b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 18, 2024
@bors bors merged commit f68c28b into rust-lang:master Sep 18, 2024
7 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 18, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f68c28b): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Max RSS (memory usage)

Results (primary -0.9%, secondary -2.1%)

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
Improvements ✅
(primary)
-0.9% [-0.9%, -0.9%] 1
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) -0.9% [-0.9%, -0.9%] 1

Cycles

Results (secondary 2.6%)

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)
2.6% [2.0%, 3.1%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.0%)

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.3% [0.1%, 1.1%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.5%, -0.0%] 17
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.5%, 1.1%] 22

Bootstrap: 767.306s -> 765.816s (-0.19%)
Artifact size: 341.25 MiB -> 341.29 MiB (0.01%)

@rustbot rustbot removed the perf-regression Performance regression. label Sep 18, 2024
@scottmcm scottmcm deleted the redo-layout branch September 18, 2024 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library 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