Skip to content

Commit d14149d

Browse files
[ty] Compact retained definition kinds (#25610)
## Summary Several common `DefinitionKind` variants retain separate references to child AST nodes even though their parent node already retains those children. Lambda parameter definitions also retain a `usize` index. Retain parent nodes for annotated assignments, import-from submodules, for statements, comprehensions, and with items, recovering their child nodes through the parent when needed. Store lambda parameter indices as `u32`. On oaiproto, this reduces retained memory by 1,175,760 bytes (0.289%).
1 parent 7d747eb commit d14149d

3 files changed

Lines changed: 84 additions & 90 deletions

File tree

crates/ty_python_core/src/builder.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,12 +1250,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
12501250
self.add_standalone_type_expression(&ann_assign.annotation);
12511251
let assignment = self.add_definition(
12521252
place_id,
1253-
AnnotatedAssignmentDefinitionNodeRef {
1254-
node: ann_assign,
1255-
annotation: &ann_assign.annotation,
1256-
value: ann_assign.value.as_deref(),
1257-
target: expr,
1258-
},
1253+
AnnotatedAssignmentDefinitionNodeRef { node: ann_assign },
12591254
);
12601255

12611256
if let Some(value) = ann_assign.value.as_deref() {
@@ -1274,9 +1269,8 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
12741269
place_id,
12751270
ForStmtDefinitionNodeRef {
12761271
unpack,
1277-
iterable: &node.iter,
1272+
node,
12781273
target: expr,
1279-
is_async: node.is_async,
12801274
},
12811275
);
12821276
}
@@ -1295,10 +1289,9 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
12951289
place_id,
12961290
ComprehensionDefinitionNodeRef {
12971291
unpack,
1298-
iterable: &node.iter,
1292+
node,
12991293
target: expr,
13001294
first,
1301-
is_async: node.is_async,
13021295
},
13031296
);
13041297
}
@@ -1311,7 +1304,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
13111304
place_id,
13121305
WithItemDefinitionNodeRef {
13131306
unpack,
1314-
context_expr: &item.context_expr,
1307+
item,
13151308
target: expr,
13161309
is_async,
13171310
},
@@ -2741,7 +2734,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
27412734
// Record whether this is equivalent to `from . import ...`
27422735
is_self_import = module_name == thispackage;
27432736

2744-
if let Some(module_node) = &node.module
2737+
if node.module.is_some()
27452738
&& let Some(relative_submodule) = module_name.relative_to(&thispackage)
27462739
&& let Some(direct_submodule) = relative_submodule.components().next()
27472740
&& !self.seen_submodule_imports.contains(direct_submodule)
@@ -2764,11 +2757,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
27642757
};
27652758
self.add_definition(
27662759
symbol.into(),
2767-
ImportFromSubmoduleDefinitionNodeRef {
2768-
node,
2769-
module: module_node,
2770-
module_index,
2771-
},
2760+
ImportFromSubmoduleDefinitionNodeRef { node, module_index },
27722761
);
27732762
}
27742763
}

0 commit comments

Comments
 (0)