|
3 | 3 | //! generic parameters. See also the `Generics` type and the `generics_of` query
|
4 | 4 | //! in rustc.
|
5 | 5 |
|
6 |
| -use std::ops; |
| 6 | +use std::{ops, sync::LazyLock}; |
7 | 7 |
|
8 | 8 | use either::Either;
|
9 | 9 | use hir_expand::{
|
@@ -412,7 +412,7 @@ impl GenericParams {
|
412 | 412 | );
|
413 | 413 | }
|
414 | 414 | let generics = generic_params.finish(types_map, &mut types_source_maps);
|
415 |
| - (Arc::new(generics), Some(Arc::new(types_source_maps))) |
| 415 | + (generics, Some(Arc::new(types_source_maps))) |
416 | 416 | }
|
417 | 417 | }
|
418 | 418 | GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics(db, id, enabled_params),
|
@@ -686,19 +686,32 @@ impl GenericParamsCollector {
|
686 | 686 | self,
|
687 | 687 | mut generics_types_map: TypesMap,
|
688 | 688 | generics_types_source_map: &mut TypesSourceMap,
|
689 |
| - ) -> GenericParams { |
| 689 | + ) -> Arc<GenericParams> { |
690 | 690 | let Self { mut lifetimes, mut type_or_consts, mut where_predicates } = self;
|
| 691 | + |
| 692 | + if lifetimes.is_empty() && type_or_consts.is_empty() && where_predicates.is_empty() { |
| 693 | + static EMPTY: LazyLock<Arc<GenericParams>> = LazyLock::new(|| { |
| 694 | + Arc::new(GenericParams { |
| 695 | + lifetimes: Arena::new(), |
| 696 | + type_or_consts: Arena::new(), |
| 697 | + where_predicates: Box::default(), |
| 698 | + types_map: TypesMap::default(), |
| 699 | + }) |
| 700 | + }); |
| 701 | + return Arc::clone(&EMPTY); |
| 702 | + } |
| 703 | + |
691 | 704 | lifetimes.shrink_to_fit();
|
692 | 705 | type_or_consts.shrink_to_fit();
|
693 | 706 | where_predicates.shrink_to_fit();
|
694 | 707 | generics_types_map.shrink_to_fit();
|
695 | 708 | generics_types_source_map.shrink_to_fit();
|
696 |
| - GenericParams { |
| 709 | + Arc::new(GenericParams { |
697 | 710 | type_or_consts,
|
698 | 711 | lifetimes,
|
699 | 712 | where_predicates: where_predicates.into_boxed_slice(),
|
700 | 713 | types_map: generics_types_map,
|
701 |
| - } |
| 714 | + }) |
702 | 715 | }
|
703 | 716 | }
|
704 | 717 |
|
|
0 commit comments