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_leak()
};

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_leak to leak 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. (should we name the Vec method const_make_global as well?)

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

To-do for me:

  • Assess the rustdoc impact of additional bounds in the method
  • Increase test coverage

@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

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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 28, 2025
@fee1-dead fee1-dead marked this pull request as draft October 28, 2025 21:52
@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 28, 2025
@fee1-dead
Copy link
Member Author

fee1-dead commented Nov 3, 2025

Yeah, rustdoc output is non-ideal. It shows all the where A: Allocator bounds. #148434 could probably make this better?

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-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

6 participants