diff options
| author | Chayim Refael Friedman <chayimfr@gmail.com> | 2024-10-18 16:38:20 +0300 |
|---|---|---|
| committer | Chayim Refael Friedman <chayimfr@gmail.com> | 2024-10-25 06:15:06 +0300 |
| commit | fc5bce925c827f9836591c9bea4ca06587430cfe (patch) | |
| tree | b8f3a9d3b01a8f3caaf6e6df4632debbab0ba117 | |
| parent | ab81593ed8783edd01215fbd2ba4955a98ff02fc (diff) | |
| download | rust-fc5bce925c827f9836591c9bea4ca06587430cfe.tar.gz rust-fc5bce925c827f9836591c9bea4ca06587430cfe.zip | |
Reuse empty `GenericParams`
This saves back 15mb that went for typeref source maps.
| -rw-r--r-- | src/tools/rust-analyzer/crates/hir-def/src/generics.rs | 23 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs | 2 |
2 files changed, 19 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-def/src/generics.rs b/src/tools/rust-analyzer/crates/hir-def/src/generics.rs index f30a2edda90..c4f871c9682 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/generics.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/generics.rs @@ -3,7 +3,7 @@ //! generic parameters. See also the `Generics` type and the `generics_of` query //! in rustc. -use std::ops; +use std::{ops, sync::LazyLock}; use either::Either; use hir_expand::{ @@ -412,7 +412,7 @@ impl GenericParams { ); } let generics = generic_params.finish(types_map, &mut types_source_maps); - (Arc::new(generics), Some(Arc::new(types_source_maps))) + (generics, Some(Arc::new(types_source_maps))) } } GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics(db, id, enabled_params), @@ -686,19 +686,32 @@ impl GenericParamsCollector { self, mut generics_types_map: TypesMap, generics_types_source_map: &mut TypesSourceMap, - ) -> GenericParams { + ) -> Arc<GenericParams> { let Self { mut lifetimes, mut type_or_consts, mut where_predicates } = self; + + if lifetimes.is_empty() && type_or_consts.is_empty() && where_predicates.is_empty() { + static EMPTY: LazyLock<Arc<GenericParams>> = LazyLock::new(|| { + Arc::new(GenericParams { + lifetimes: Arena::new(), + type_or_consts: Arena::new(), + where_predicates: Box::default(), + types_map: TypesMap::default(), + }) + }); + return Arc::clone(&EMPTY); + } + lifetimes.shrink_to_fit(); type_or_consts.shrink_to_fit(); where_predicates.shrink_to_fit(); generics_types_map.shrink_to_fit(); generics_types_source_map.shrink_to_fit(); - GenericParams { + Arc::new(GenericParams { type_or_consts, lifetimes, where_predicates: where_predicates.into_boxed_slice(), types_map: generics_types_map, - } + }) } } diff --git a/src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs b/src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs index c42c849c3b9..35060356878 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs @@ -895,7 +895,7 @@ impl<'a> Ctx<'a> { generics.fill(&body_ctx, node, add_param_attrs); let generics = generics.finish(types_map, &mut types_source_map); - (Arc::new(generics), types_source_map) + (generics, types_source_map) } fn lower_type_bounds( |
