rust/compiler
bors 750d6f8545 Auto merge of #97585 - lqd:const-alloc-intern, r=RalfJung
CTFE interning: don't walk allocations that don't need it

The interning of const allocations visits the mplace looking for references to intern. Walking big aggregates like big static arrays can be costly, so we only do it if the allocation we're interning contains references or interior mutability.

Walking ZSTs was avoided before, and this optimization is now applied to cases where there are no references/relocations either.

---

While initially looking at this in the context of #93215, I've been testing with smaller allocations than the 16GB one in that issue, and with different init/uninit patterns (esp. via padding).

In that example, by default, `eval_to_allocation_raw` is the heaviest query followed by `incr_comp_serialize_result_cache`. So I'll show numbers when incremental compilation is disabled, to focus on the const allocations themselves at 95% of the compilation time, at bigger array sizes on these minimal examples like `static ARRAY: [u64; LEN] = [0; LEN];`.

That is a close construction to parts of the `ctfe-stress-test-5` benchmark, which has const allocations in the megabytes, while most crates usually have way smaller ones. This PR will have the most impact in these situations, as the walk during the interning starts to dominate the runtime.

Unicode crates (some of which are present in our benchmarks) like `ucd`, `encoding_rs`, etc come to mind as having bigger than usual allocations as well, because of big tables of code points (in the hundreds of KB, so still an order of magnitude or 2 less than the stress test).

In a check build, for a single static array shown above, from 100 to 10^9 u64s (for lengths in powers of ten), the constant factors are lowered:

(log scales for easier comparisons)
![plot_log](https://user-images.githubusercontent.com/247183/171422958-16f1ea19-3ed4-4643-812c-1c7c60a97e19.png)

(linear scale for absolute diff at higher Ns)
![plot_linear](https://user-images.githubusercontent.com/247183/171401886-2a869a4d-5cd5-47d3-9a5f-8ce34b7a6917.png)

For one of the alternatives of that issue
```rust
const ROWS: usize = 100_000;
const COLS: usize = 10_000;

static TWODARRAY: [[u128; COLS]; ROWS] = [[0; COLS]; ROWS];
```

we can see a similar reduction of around 3x (from 38s to 12s or so).

For the same size, the slowest case IIRC is when there are uninitialized bytes e.g. via padding

```rust
const ROWS: usize = 100_000;
const COLS: usize = 10_000;

static TWODARRAY: [[(u64, u8); COLS]; ROWS] = [[(0, 0); COLS]; ROWS];
```
then interning/walking does not dominate anymore (but means there is likely still some interesting work left to do here).

Compile times in this case rise up quite a bit, and avoiding interning walks has less impact: around 23%, from 730s on master to 568s with this PR.
2022-07-02 17:05:13 +00:00
..
rustc Rollup merge of #97385 - oli-obk:smir-tool-lib, r=pnkfelix 2022-06-14 07:47:24 +09:00
rustc_apfloat Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_arena Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_ast Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_ast_lowering Factor out hir::Node::Binding 2022-07-01 10:04:19 -05:00
rustc_ast_passes Auto merge of #97842 - notriddle:notriddle/tuple-docs, r=jsha,GuillaumeGomez 2022-06-16 11:13:30 +00:00
rustc_ast_pretty Fix pretty printing of empty type bound lists in where-clause 2022-06-16 17:24:50 -07:00
rustc_attr Remove rustc_deprecated diagnostics 2022-06-14 19:46:13 -04:00
rustc_borrowck Rollup merge of #98766 - lcnr:mir-visit-pass_by_value, r=oli-obk 2022-07-02 12:23:41 +05:30
rustc_builtin_macros Change Ty::Tuple to Ty::Unit. 2022-07-01 15:19:49 +10:00
rustc_codegen_cranelift Recover when failing to normalize closure signature. 2022-06-30 21:45:29 +02:00
rustc_codegen_gcc Auto merge of #98098 - bjorn3:archive_refactor, r=michaelwoerister 2022-06-21 16:24:56 +00:00
rustc_codegen_llvm llvm-wrapper: adapt for LLVMConstExtractValue removal 2022-06-30 12:47:34 +00:00
rustc_codegen_ssa Auto merge of #91743 - cjgillot:enable_mir_inlining_inline_all, r=oli-obk 2022-07-02 11:24:17 +00:00
rustc_const_eval Auto merge of #97585 - lqd:const-alloc-intern, r=RalfJung 2022-07-02 17:05:13 +00:00
rustc_data_structures Auto merge of #98558 - nnethercote:smallvec-1.8.1, r=lqd 2022-06-29 09:11:29 +00:00
rustc_driver Fix rustdoc argument error 2022-06-27 10:43:51 +02:00
rustc_error_codes Rollup merge of #97423 - m-ou-se:memory-ordering-intrinsics, r=tmiasko 2022-06-29 10:28:18 +05:30
rustc_error_messages Migrate some rustc_borrowck diagnostics to SessionDiagnostic 2022-06-28 22:41:56 +00:00
rustc_errors Rollup merge of #98695 - tshepang:or-pattern, r=compiler-errors 2022-06-30 19:55:55 +02:00
rustc_expand Rollup merge of #98668 - TaKO8Ki:avoid-many-&str-to-string-conversions, r=Dylan-DPC 2022-06-29 20:35:07 +02:00
rustc_feature Move/rename lazy::Sync{OnceCell,Lazy} to sync::{Once,Lazy}Lock 2022-06-16 19:54:42 +04:00
rustc_fs_util Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_graphviz Fully stabilize NLL 2022-06-03 17:16:41 -04:00
rustc_hir Factor out hir::Node::Binding 2022-07-01 10:04:19 -05:00
rustc_hir_pretty Factor out hir::Node::Binding 2022-07-01 10:04:19 -05:00
rustc_incremental Auto merge of #98153 - nnethercote:fix-MissingDoc-quadratic-behaviour, r=cjgillot 2022-06-18 09:57:00 +00:00
rustc_index Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_infer Only label place where type is needed if span is meaningful 2022-07-01 17:38:34 +00:00
rustc_interface Auto merge of #97235 - nbdd0121:unwind, r=Amanieu 2022-07-02 14:06:27 +00:00
rustc_lexer fix typo in comment 2022-06-28 19:59:09 +05:30
rustc_lint fix grammar in useless doc comment lint 2022-06-30 16:17:38 -04:00
rustc_lint_defs Auto merge of #97235 - nbdd0121:unwind, r=Amanieu 2022-07-02 14:06:27 +00:00
rustc_llvm llvm-wrapper: adapt for LLVMConstExtractValue removal 2022-06-30 12:47:34 +00:00
rustc_log clippy::complexity fixes 2022-05-26 13:14:24 +02:00
rustc_macros macros: use typed identifiers in subdiag derive 2022-06-24 09:08:25 +01:00
rustc_metadata Auto merge of #97235 - nbdd0121:unwind, r=Amanieu 2022-07-02 14:06:27 +00:00
rustc_middle Auto merge of #97585 - lqd:const-alloc-intern, r=RalfJung 2022-07-02 17:05:13 +00:00
rustc_mir_build Rollup merge of #98639 - camsteffen:no-node-binding, r=compiler-errors 2022-07-02 12:23:38 +05:30
rustc_mir_dataflow cleanup mir visitor for rustc::pass_by_value 2022-07-01 16:21:21 +02:00
rustc_mir_transform Auto merge of #97235 - nbdd0121:unwind, r=Amanieu 2022-07-02 14:06:27 +00:00
rustc_monomorphize Auto merge of #91743 - cjgillot:enable_mir_inlining_inline_all, r=oli-obk 2022-07-02 11:24:17 +00:00
rustc_parse Rollup merge of #98668 - TaKO8Ki:avoid-many-&str-to-string-conversions, r=Dylan-DPC 2022-06-29 20:35:07 +02:00
rustc_parse_format Make rustc_parse_format compile on stable 2022-05-03 11:26:58 +02:00
rustc_passes Auto merge of #98402 - cjgillot:undead, r=michaelwoerister 2022-07-01 14:43:15 +00:00
rustc_plugin_impl remove currently unused deps 2022-06-13 22:20:51 +03:00
rustc_privacy Auto merge of #98542 - jackh726:coinductive-wf, r=oli-obk 2022-06-29 03:22:47 +00:00
rustc_query_impl Auto merge of #98106 - cjgillot:split-definitions, r=michaelwoerister 2022-06-17 10:00:11 +00:00
rustc_query_system Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_resolve Rollup merge of #98677 - lyming2007:issue-98492-fix, r=lcnr 2022-06-30 19:55:53 +02:00
rustc_save_analysis Factor out hir::Node::Binding 2022-07-01 10:04:19 -05:00
rustc_serialize Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_session various: add rustc_lint_diagnostics to diag fns 2022-06-27 08:32:06 +01:00
rustc_smir Rustfmt 2022-06-02 10:29:00 +00:00
rustc_span Auto merge of #98376 - nnethercote:improve-derive-PartialEq, r=petrochenkov 2022-06-29 00:20:57 +00:00
rustc_symbol_mangling Rollup merge of #98067 - klensy:compiler-deps2, r=Dylan-DPC 2022-06-15 12:02:02 +09:00
rustc_target rustc_target: Some more tests and fixes for linker arguments 2022-06-25 21:57:08 +03:00
rustc_trait_selection Move Sized check before first error is created 2022-07-01 17:38:34 +00:00
rustc_traits Auto merge of #98558 - nnethercote:smallvec-1.8.1, r=lqd 2022-06-29 09:11:29 +00:00
rustc_ty_utils Recover when failing to normalize closure signature. 2022-06-30 21:45:29 +02:00
rustc_type_ir Update smallvec to 1.8.1. 2022-06-27 08:48:55 +10:00
rustc_typeck Rollup merge of #98639 - camsteffen:no-node-binding, r=compiler-errors 2022-07-02 12:23:38 +05:30