Skip to content

Conversation

@fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Oct 19, 2025

Example:

const X: &'static [u32] = {
    let mut v = Vec::with_capacity(6);
    let mut x = 1;
    while x < 42 {
        v.push(x);
        x *= 2;
    }
    assert!(v.len() == 6);
    v.const_make_global()
};

assert_eq!([1, 2, 4, 8, 16, 32], X);

Oh this is fun...

  • We split out the implementation of Global such that it calls intrinsics::const_allocate and intrinsics::const_deallocate during compile time. This is achieved using const_eval_select
  • This allows us to impl const Allocator for Global
  • We then constify everything necessary for Vec::with_capacity and Vec::push.
  • Added Vec::const_make_global to leak and intern the final value via intrinsics::const_make_global. If we see any pointer in the final value of a const that did not call const_make_global, we error as implemented in add const_make_global; err for const_allocate ptrs if didn't call #143595.

r? @rust-lang/wg-const-eval

To-do for me:

  • Assess the rustdoc impact of additional bounds in the method
  • Increase test coverage I think this is enough for an unstable feature.

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 20, 2025
@fee1-dead fee1-dead marked this pull request as draft October 20, 2025 00:00
@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 Oct 20, 2025
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk assigned oli-obk and unassigned RalfJung Oct 20, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

We used to have the plan to have a dedicated compile-time allocator type... this is an interesting alternative. Given that it also involves the library surface, we should probably involve t-libs-api.

Why have you picked Vec as the first type for this? I think it'd make more sense for Box to go first since that is the most primitive type for heap allocations.

View changes since this review

Comment on lines 377 to 382
let mut offset = 0;
while offset < size {
offset += 1;
// SAFETY: the pointer returned by `const_allocate` is valid to write to.
ptr.add(offset).write(0)
}
Copy link
Member

Choose a reason for hiding this comment

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

It would be much more efficient to use write_bytes here.

/// `Vec<T>` created during compile time.
#[unstable(feature = "const_heap", issue = "79597")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const fn const_leak(mut self) -> &'static [T] {
Copy link
Member

Choose a reason for hiding this comment

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

This is conceptually quite different from leak which IMO should be reflected in the name, so I would indeed prefer const_make_global.

/// `Vec<T>` created during compile time.
#[unstable(feature = "const_heap", issue = "79597")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const fn const_leak(mut self) -> &'static [T] {
Copy link
Member

Choose a reason for hiding this comment

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

This needs a where T: Freeze for soundness.

}

/// Leaks the `Vec<T>` to be interned statically. This mut be done for all
/// `Vec<T>` created during compile time.
Copy link
Member

@RalfJung RalfJung Oct 20, 2025

Choose a reason for hiding this comment

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

It doesn't need to be done for all Vec created during compile-time -- only for those that you want to carry over to runtime. You can use intermediate Vec as scratch space or so during compile time that you never call this method on.

@fee1-dead
Copy link
Member Author

I think it'd make more sense for Box to go first since that is the most primitive type for heap allocations.

That will either require us doing a const_eval_select to do box_new in runtime and something else in compile time, or to constify box_new. I chose Vec because I thought it would be much more useful than Box.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 26, 2025

@bors try @rust-timer queue
This affects core pieces of vec and alloc, let's make sure everything gets optimized well

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 26, 2025
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 26, 2025
@rust-bors
Copy link
Contributor

rust-bors bot commented Oct 26, 2025

☀️ Try build successful (CI)
Build commit: b3f7ade (b3f7ade975cf0d5d02740da43e496064c977dfb4, parent: f977dfc388ea39c9886b7f8c49abce26e6918df6)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b3f7ade): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.3%, 0.3%] 1
Regressions ❌
(secondary)
0.2% [0.1%, 0.2%] 3
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-0.6%, 0.3%] 2

Max RSS (memory usage)

Results (primary 0.4%, secondary -0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.7% [1.3%, 4.5%] 5
Regressions ❌
(secondary)
3.7% [3.7%, 3.7%] 1
Improvements ✅
(primary)
-2.0% [-3.3%, -0.4%] 5
Improvements ✅
(secondary)
-2.3% [-3.1%, -1.6%] 2
All ❌✅ (primary) 0.4% [-3.3%, 4.5%] 10

Cycles

Results (primary 3.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Binary size

Results (primary 0.3%, secondary 0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.4% [0.0%, 1.5%] 33
Regressions ❌
(secondary)
0.6% [0.0%, 1.0%] 66
Improvements ✅
(primary)
-0.2% [-0.5%, -0.0%] 5
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) 0.3% [-0.5%, 1.5%] 38

Bootstrap: 473.751s -> 473.82s (0.01%)
Artifact size: 390.43 MiB -> 390.55 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 27, 2025
@fee1-dead fee1-dead marked this pull request as ready for review October 28, 2025 21:51
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 28, 2025
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead
Copy link
Member Author

I don't understand what is going on with the miri differences, @RalfJung what should I change here? Bless the test? Or change something so that the test still retains the original message?

@bors
Copy link
Collaborator

bors commented Dec 30, 2025

☔ The latest upstream changes (presumably #147247) made this pull request unmergeable. Please resolve the merge conflicts.

@RalfJung
Copy link
Member

Ah dang, I fixed the ICE but forgot to fix the non-determinism...

@rustbot
Copy link
Collaborator

rustbot commented Jan 2, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@fee1-dead
Copy link
Member Author

blessed tests

@bors r=oli-obk

@bors
Copy link
Collaborator

bors commented Jan 6, 2026

📌 Commit a913065 has been approved by oli-obk

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 Jan 6, 2026
bors added a commit that referenced this pull request Jan 6, 2026
`Vec::push` in consts MVP

Example:

```rust
const X: &'static [u32] = {
    let mut v = Vec::with_capacity(6);
    let mut x = 1;
    while x < 42 {
        v.push(x);
        x *= 2;
    }
    assert!(v.len() == 6);
    v.const_make_global()
};

assert_eq!([1, 2, 4, 8, 16, 32], X);
```

Oh this is fun...

* We split out the implementation of `Global` such that it calls `intrinsics::const_allocate` and `intrinsics::const_deallocate` during compile time. This is achieved using `const_eval_select`
* This allows us to `impl const Allocator for Global`
* We then constify everything necessary for `Vec::with_capacity` and `Vec::push`.
* Added `Vec::const_make_global` to leak and intern the final value via `intrinsics::const_make_global`. If we see any pointer in the final value of a `const` that did not call `const_make_global`, we error as implemented in #143595.

r? `@rust-lang/wg-const-eval`

To-do for me:
* [x] Assess the rustdoc impact of additional bounds in the method
* [x] ~~Increase test coverage~~ I think this is enough for an unstable feature.
@bors
Copy link
Collaborator

bors commented Jan 6, 2026

⌛ Testing commit a913065 with merge 7d003da...

@Zalathar
Copy link
Member

Zalathar commented Jan 6, 2026

I think a runner has stalled out, unfortunately. 😿

@bors retry

@bors
Copy link
Collaborator

bors commented Jan 6, 2026

⌛ Testing commit a913065 with merge 74fd751...

@bors
Copy link
Collaborator

bors commented Jan 6, 2026

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 74fd751 to main...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 6, 2026
@bors bors merged commit 74fd751 into rust-lang:main Jan 6, 2026
12 checks passed
@rustbot rustbot added this to the 1.94.0 milestone Jan 6, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing da476f1 (parent) -> 74fd751 (this PR)

Test differences

Show 430 test diffs

Stage 1

  • vec::const_heap: [missing] -> pass (J0)
  • [ui] tests/ui/consts/const-eval/heap/vec-not-made-global.rs: [missing] -> pass (J2)

Stage 2

  • [ui] tests/ui/consts/const-eval/heap/vec-not-made-global.rs: [missing] -> pass (J1)
  • vec::const_heap: [missing] -> pass (J3)

Additionally, 426 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 74fd7516dac6e7be0e561dddb9bee4e860598f07 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-linux: 6373.7s -> 8931.5s (+40.1%)
  2. aarch64-apple: 11247.2s -> 7470.7s (-33.6%)
  3. dist-apple-various: 3980.8s -> 5047.7s (+26.8%)
  4. pr-check-1: 2004.3s -> 1688.1s (-15.8%)
  5. x86_64-rust-for-linux: 3167.0s -> 2727.7s (-13.9%)
  6. x86_64-gnu-gcc: 3766.3s -> 3341.1s (-11.3%)
  7. dist-x86_64-apple: 7340.5s -> 8154.1s (+11.1%)
  8. pr-check-2: 2597.9s -> 2312.5s (-11.0%)
  9. x86_64-gnu-tools: 3614.8s -> 3247.6s (-10.2%)
  10. arm-android: 6405.1s -> 5765.4s (-10.0%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (74fd751): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.3%, 0.4%] 3
Regressions ❌
(secondary)
0.2% [0.1%, 0.4%] 5
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-1.8% [-1.9%, -1.7%] 2
All ❌✅ (primary) 0.2% [-0.2%, 0.4%] 4

Max RSS (memory usage)

Results (primary -0.3%, secondary -2.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.9% [0.8%, 3.0%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.7% [-4.7%, -4.7%] 1
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -0.3% [-4.7%, 3.0%] 3

Cycles

Results (secondary -2.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.4%, -1.5%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.9%, secondary 0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.0% [0.4%, 1.6%] 10
Regressions ❌
(secondary)
0.7% [0.0%, 0.9%] 51
Improvements ✅
(primary)
-0.1% [-0.1%, -0.1%] 1
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 1
All ❌✅ (primary) 0.9% [-0.1%, 1.6%] 11

Bootstrap: 473.133s -> 473.921s (0.17%)
Artifact size: 390.77 MiB -> 390.82 MiB (0.01%)

@fee1-dead
Copy link
Member Author

Could just be noise, and that we're probably just doing more work with more items for allocation/vectors work. there's no cycles regressions so I think this is fine

@Mark-Simulacrum
Copy link
Member

Looking at a cranelift profile it loosely looks like we're spending more time in LLVM, maybe because of the extra indirection though the const_eval_select? Though I don't see obvious signs of this in godbolt diffs.

In either case, marking as triaged as further investigation doesn't seem warranted given magnitude and general desire for this feature.

@Mark-Simulacrum Mark-Simulacrum added the perf-regression-triaged The performance regression has been triaged. label Jan 12, 2026
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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.

9 participants