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

Add invariant to Vec::pop that len < cap if pop successful #114370

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

krtab
Copy link
Contributor

@krtab krtab commented Aug 2, 2023

Fixes: #114334

@rustbot
Copy link
Collaborator

rustbot commented Aug 2, 2023

r? @Mark-Simulacrum

(rustbot has picked a reviewer for you, use r? to override)

@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 Aug 2, 2023
@mati865
Copy link
Contributor

mati865 commented Aug 2, 2023

Without codegen test this is likely to regress at some point.

@the8472
Copy link
Member

the8472 commented Aug 2, 2023

let's check the perf impact

@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 Aug 2, 2023
@bors
Copy link
Contributor

bors commented Aug 2, 2023

⌛ Trying commit 32a0bf62062fcc99d927e2611eeefe2f62bf96f4 with merge 09bf8a0a3be933b7f221bdc99f8fdfe5e8a0306e...

@bors
Copy link
Contributor

bors commented Aug 2, 2023

☀️ Try build successful - checks-actions
Build commit: 09bf8a0a3be933b7f221bdc99f8fdfe5e8a0306e (09bf8a0a3be933b7f221bdc99f8fdfe5e8a0306e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (09bf8a0a3be933b7f221bdc99f8fdfe5e8a0306e): comparison URL.

Overall result: ❌ regressions - no 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.

@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.6% [0.6%, 0.6%] 1
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [0.6%, 0.6%] 1

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)
5.5% [3.7%, 7.2%] 2
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
-2.9% [-3.0%, -2.8%] 2
Improvements ✅
(secondary)
-2.7% [-3.4%, -2.0%] 2
All ❌✅ (primary) 1.3% [-3.0%, 7.2%] 4

Cycles

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

Binary size

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.1% [0.0%, 0.7%] 17
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.5%, -0.0%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.5%, 0.7%] 21

Bootstrap: 649.933s -> 651.065s (0.17%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 3, 2023
@scottmcm
Copy link
Member

scottmcm commented Aug 3, 2023

r? @scottmcm

I agree with @mati865 that this (like all assumes) should have a codegen test demonstrating that it enables some fruitful optimization that didn't happen without it.

@rustbot author

(That way when LLVM gets smarter or the code around it changes, people can try removing them again -- like in #111447 -- and the codegen test ensures that doesn't regress things.)

@rustbot rustbot assigned scottmcm and unassigned Mark-Simulacrum Aug 3, 2023
@rustbot rustbot 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 Aug 3, 2023
@krtab
Copy link
Contributor Author

krtab commented Aug 4, 2023

I've added a codegen test on the LLVM IR. I was unsure whether to add it as an LLVM-IR codegen or assembly codegen test. I chose the first because it felt more portable, but the second would have been closer to the spirit ("it should compile to a noop"). Let me know if you want me to change.

@scottmcm scottmcm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. O-NixOS Operating system: NixOS, https://nixos.org/ and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. O-NixOS Operating system: NixOS, https://nixos.org/ labels Aug 15, 2023
// CHECK-NEXT: getelementptr
// CHECK-NEXT: load
// CHECK-NEXT: icmp
// CHECK-NEXT: tail call
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, the way this is written it feels a bit too specific, in that lots of little changes could make it fail (like bb3 getting a different name), but also that this tail call could be to something that reallocates and it'd pass.

Can you maybe focus it in a bit on what the critical part is?

Perhaps have another function in here showing that push normally can call __rust_realloc, but that this one doesn't?

A possible starting point:

#[no_mangle]
// CHECK-LABEL: @noop(
pub fn noop(v: &mut Vec<u8>) {
    // CHECK-NOT: __rust_alloc
    // CHECK-NOT: __rust_realloc
    // CHECK-NOT: call
    // CHECK: tail call void @llvm.assume
    // CHECK-NOT: __rust_alloc
    // CHECK-NOT: __rust_realloc
    // CHECK-NOT: call
    if let Some(x) = v.pop() {
        v.push(x)
    }
}

#[no_mangle]
// CHECK-LABEL: @push_byte(
pub fn push_byte(v: &mut Vec<u8>) {
    // CHECK: call {{.+}} @__rust_alloc(
    // CHECK: call {{.+}} @__rust_realloc(
    v.push(3);
}

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, maybe it'll have to look at reserve_for_push instead of the allocation functions directly...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi,
I see your point but wouldn't it then be better to go for a codegen test on the assembly itself which is empty?

Copy link
Member

Choose a reason for hiding this comment

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

No, because everything should be codegen tests where possible -- random permutations of temporary assembly registers and differing calling conventions make assembly tests something that's a last resort.

@scottmcm
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Oct 16, 2023

📌 Commit 0bcac8a has been approved by scottmcm

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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2023
@bors
Copy link
Contributor

bors commented Oct 16, 2023

⌛ Testing commit 0bcac8a with merge 49691b1...

@bors
Copy link
Contributor

bors commented Oct 16, 2023

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 49691b1 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 16, 2023
@bors bors merged commit 49691b1 into rust-lang:master Oct 16, 2023
12 checks passed
@rustbot rustbot added this to the 1.75.0 milestone Oct 16, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (49691b1): 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.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.4% [-0.4%, -0.4%] 1

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)
1.6% [1.3%, 1.8%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.6% [1.3%, 1.8%] 2

Cycles

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

Binary size

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.0% [0.0%, 0.2%] 20
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.9%, -0.0%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.9%, 0.2%] 24

Bootstrap: 626.726s -> 627.292s (0.09%)
Artifact size: 305.62 MiB -> 305.57 MiB (-0.02%)

bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this pull request Oct 17, 2023
54: Pull upstream master 2023 10 17 r=pietroalbini a=Veykril

* rust-lang/rust#116196
* rust-lang/rust#116824
* rust-lang/rust#116822
* rust-lang/rust#116477
* rust-lang/rust#116826
* rust-lang/rust#116820
  * rust-lang/rust#116811
  * rust-lang/rust#116808
  * rust-lang/rust#116805
  * rust-lang/rust#116800
  * rust-lang/rust#116798
  * rust-lang/rust#116754
* rust-lang/rust#114370
* rust-lang/rust#116804
  * rust-lang/rust#116802
  * rust-lang/rust#116790
  * rust-lang/rust#116786
  * rust-lang/rust#116709
  * rust-lang/rust#116430
  * rust-lang/rust#116257
  * rust-lang/rust#114157
* rust-lang/rust#116731
* rust-lang/rust#116550
* rust-lang/rust#114330
* rust-lang/rust#116724
* rust-lang/rust#116782
  * rust-lang/rust#116776
  * rust-lang/rust#115955
  * rust-lang/rust#115196
* rust-lang/rust#116775
* rust-lang/rust#114589
* rust-lang/rust#113747
* rust-lang/rust#116772
  * rust-lang/rust#116771
  * rust-lang/rust#116760
  * rust-lang/rust#116755
  * rust-lang/rust#116732
  * rust-lang/rust#116522
  * rust-lang/rust#116341
  * rust-lang/rust#116172
* rust-lang/rust#110604
* rust-lang/rust#110729
* rust-lang/rust#116527
* rust-lang/rust#116688
* rust-lang/rust#116757
  * rust-lang/rust#116753
  * rust-lang/rust#116748
  * rust-lang/rust#116741
  * rust-lang/rust#116594
* rust-lang/rust#116691
* rust-lang/rust#116643
* rust-lang/rust#116683
* rust-lang/rust#116635
* rust-lang/rust#115515
* rust-lang/rust#116742
  * rust-lang/rust#116661
  * rust-lang/rust#116576
  * rust-lang/rust#116540
* rust-lang/rust#116352
* rust-lang/rust#116737
  * rust-lang/rust#116730
  * rust-lang/rust#116723
  * rust-lang/rust#116715
  * rust-lang/rust#116603
  * rust-lang/rust#116591
  * rust-lang/rust#115439
* rust-lang/rust#116264
* rust-lang/rust#116727
  * rust-lang/rust#116704
  * rust-lang/rust#116696
  * rust-lang/rust#116695
  * rust-lang/rust#116644
  * rust-lang/rust#116630
* rust-lang/rust#116728
  * rust-lang/rust#116689
  * rust-lang/rust#116679
  * rust-lang/rust#116618
  * rust-lang/rust#116577
  * rust-lang/rust#115653
* rust-lang/rust#116702
* rust-lang/rust#116015
* rust-lang/rust#115822
* rust-lang/rust#116407
* rust-lang/rust#115719
* rust-lang/rust#115524
* rust-lang/rust#116705
* rust-lang/rust#116645
* rust-lang/rust#116233
* rust-lang/rust#115108
* rust-lang/rust#116670
* rust-lang/rust#116676
* rust-lang/rust#116666



Co-authored-by: Benoît du Garreau <bdgdlm@outlook.com>
Co-authored-by: Colin Finck <colin@reactos.org>
Co-authored-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Co-authored-by: Joshua Liebow-Feeser <joshlf@users.noreply.github.com>
Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
Co-authored-by: Trevor Gross <tmgross@umich.edu>
Co-authored-by: Evan Merlock <evan@merlock.dev>
Co-authored-by: joboet <jonasboettiger@icloud.com>
Co-authored-by: Ralf Jung <post@ralfj.de>
Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
Co-authored-by: onur-ozkan <work@onurozkan.dev>
Co-authored-by: Nicholas Nethercote <n.nethercote@gmail.com>
Co-authored-by: The 8472 <git@infinite-source.de>
Co-authored-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Co-authored-by: reez12g <reez12g@gmail.com>
Co-authored-by: Jakub Beránek <berykubik@gmail.com>
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 26, 2024
…cap, r=<try>

Add invariant to VecDeque::pop_* that len < cap if pop successful

Similar to rust-lang#114370 for VecDeque instead of Vec.

I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370).

This is my first time with codegen tests, I based the test on what was done for Vec.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 27, 2024
…e_cap, r=Nilstrieb

Add invariant to VecDeque::pop_* that len < cap if pop successful

Similar to rust-lang#114370 for VecDeque instead of Vec.

I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370).

This is my first time with codegen tests, I based the test on what was done for Vec.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 8, 2024
…e_cap, r=Nilstrieb

Add invariant to VecDeque::pop_* that len < cap if pop successful

Similar to rust-lang#114370 for VecDeque instead of Vec.

I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370).

This is my first time with codegen tests, I based the test on what was done for Vec.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 8, 2024
Rollup merge of rust-lang#123089 - Philippe-Cholet:vecdeque_pop_assume_cap, r=Nilstrieb

Add invariant to VecDeque::pop_* that len < cap if pop successful

Similar to rust-lang#114370 for VecDeque instead of Vec.

I initially come from rust-itertools/itertools#899 where we noticed that `pop_front;push_back;` was slower than expected so `@scottmcm` suggested I file an issue which lead to https://internals.rust-lang.org/t/vecdeque-pop-front-push-back/20483 where **kornel** mentionned rust-lang#114334 (fixed by rust-lang#114370).

This is my first time with codegen tests, I based the test on what was done for Vec.
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.

Missed optimization opportunity when Vec::push follows Vec::pop
8 participants