Skip to content

Commit dc10ab8

Browse files
authored
[ty] Use ThinVec for sub segments in PlaceExpr (#19470)
1 parent 7673d46 commit dc10ab8

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ strum_macros = { version = "0.27.0" }
166166
syn = { version = "2.0.55" }
167167
tempfile = { version = "3.9.0" }
168168
test-case = { version = "3.3.1" }
169+
thin-vec = { version = "0.2.14" }
169170
thiserror = { version = "2.0.0" }
170171
tikv-jemallocator = { version = "0.6.0" }
171172
toml = { version = "0.9.0" }

crates/ty_python_semantic/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ indexmap = { workspace = true }
3636
itertools = { workspace = true }
3737
ordermap = { workspace = true }
3838
salsa = { workspace = true, features = ["compact_str"] }
39+
thin-vec = { workspace = true }
3940
thiserror = { workspace = true }
4041
tracing = { workspace = true }
4142
rustc-hash = { workspace = true }

crates/ty_python_semantic/src/semantic_index/place.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ impl PlaceExprSubSegment {
4141
#[derive(Eq, PartialEq, Debug, get_size2::GetSize)]
4242
pub struct PlaceExpr {
4343
root_name: Name,
44-
sub_segments: SmallVec<[PlaceExprSubSegment; 1]>,
44+
#[get_size(size_fn=sub_segments_size)]
45+
sub_segments: thin_vec::ThinVec<PlaceExprSubSegment>,
46+
}
47+
48+
fn sub_segments_size(segments: &thin_vec::ThinVec<PlaceExprSubSegment>) -> usize {
49+
segments.capacity() * std::mem::size_of::<PlaceExprSubSegment>()
50+
+ segments
51+
.iter()
52+
.map(get_size2::GetSize::get_heap_size)
53+
.sum::<usize>()
4554
}
4655

4756
impl std::fmt::Display for PlaceExpr {
@@ -162,10 +171,10 @@ impl TryFrom<ast::ExprRef<'_>> for PlaceExpr {
162171
}
163172

164173
impl PlaceExpr {
165-
pub(crate) const fn name(name: Name) -> Self {
174+
pub(crate) fn name(name: Name) -> Self {
166175
Self {
167176
root_name: name,
168-
sub_segments: SmallVec::new_const(),
177+
sub_segments: thin_vec::ThinVec::new(),
169178
}
170179
}
171180

@@ -652,6 +661,8 @@ pub struct PlaceTable {
652661
impl PlaceTable {
653662
fn shrink_to_fit(&mut self) {
654663
self.places.shrink_to_fit();
664+
self.place_set
665+
.shrink_to_fit(|id| PlaceTable::hash_place_expr(&self.places[*id].expr));
655666
}
656667

657668
pub(crate) fn place_expr(&self, place_id: impl Into<ScopedPlaceId>) -> &PlaceExprWithFlags {
@@ -775,7 +786,7 @@ impl std::fmt::Debug for PlaceTable {
775786
pub(super) struct PlaceTableBuilder {
776787
table: PlaceTable,
777788

778-
associated_place_ids: IndexVec<ScopedPlaceId, Vec<ScopedPlaceId>>,
789+
associated_place_ids: IndexVec<ScopedPlaceId, SmallVec<[ScopedPlaceId; 4]>>,
779790
}
780791

781792
impl PlaceTableBuilder {
@@ -794,14 +805,17 @@ impl PlaceTableBuilder {
794805

795806
let id = self.table.places.push(symbol);
796807
entry.insert(id);
797-
let new_id = self.associated_place_ids.push(vec![]);
808+
let new_id = self.associated_place_ids.push(SmallVec::new_const());
798809
debug_assert_eq!(new_id, id);
799810
(id, true)
800811
}
801812
}
802813
}
803814

804-
pub(super) fn add_place(&mut self, place_expr: PlaceExprWithFlags) -> (ScopedPlaceId, bool) {
815+
pub(super) fn add_place(
816+
&mut self,
817+
mut place_expr: PlaceExprWithFlags,
818+
) -> (ScopedPlaceId, bool) {
805819
let hash = PlaceTable::hash_place_expr(&place_expr.expr);
806820
let entry = self.table.place_set.entry(
807821
hash,
@@ -812,9 +826,10 @@ impl PlaceTableBuilder {
812826
match entry {
813827
Entry::Occupied(entry) => (*entry.get(), false),
814828
Entry::Vacant(entry) => {
829+
place_expr.expr.sub_segments.shrink_to_fit();
815830
let id = self.table.places.push(place_expr);
816831
entry.insert(id);
817-
let new_id = self.associated_place_ids.push(vec![]);
832+
let new_id = self.associated_place_ids.push(SmallVec::new_const());
818833
debug_assert_eq!(new_id, id);
819834
for root in self.table.places[id].expr.root_exprs() {
820835
if let Some(root_id) = self.table.place_id_by_expr(root) {

0 commit comments

Comments
 (0)