Skip to content

Comments

[ty] Shrink size of AstNodeRef#20028

Merged
ibraheemdev merged 2 commits intomainfrom
ibraheem/ast-node-ref-size
Aug 22, 2025
Merged

[ty] Shrink size of AstNodeRef#20028
ibraheemdev merged 2 commits intomainfrom
ibraheem/ast-node-ref-size

Conversation

@ibraheemdev
Copy link
Member

Summary

Removes the module_ptr field from AstNodeRef in release mode, and change NodeIndex to a NonZeroU32 to reduce the size of Option<AstNodeRef<_>> fields.

I believe CI runs in debug mode, so this won't show up in the memory report, but this reduces memory by ~2% in release mode.

@ibraheemdev ibraheemdev added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Aug 21, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 21, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Aug 21, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@ibraheemdev ibraheemdev force-pushed the ibraheem/ast-node-ref-size branch from 2d4f502 to 049b9cc Compare August 21, 2025 18:51
@ibraheemdev ibraheemdev force-pushed the ibraheem/ast-node-ref-size branch from 049b9cc to a73cbf9 Compare August 21, 2025 19:01
@github-actions
Copy link
Contributor

github-actions bot commented Aug 21, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Can you try to get some numbers from a real world project when using a release build? I'm curious how much this reduces the memory usage for Expression and Definition (of which we have many of)

@ibraheemdev ibraheemdev force-pushed the ibraheem/ast-node-ref-size branch from 1eec1e9 to 56dba8e Compare August 22, 2025 17:57
@ibraheemdev ibraheemdev force-pushed the ibraheem/ast-node-ref-size branch from 56dba8e to 9285d64 Compare August 22, 2025 20:50
@ibraheemdev
Copy link
Member Author

Here's the diff from on a release build of prefect. The effect on Definition and Expression is quite significant.

 Found 29228 diagnostics
 =======SALSA STRUCTS=======
-`Definition`                                       metadata=19.83MB  fields=39.66MB  count=495746
-`Expression`                                       metadata=12.75MB  fields=14.87MB  count=265605
+`Definition`                                       metadata=19.83MB  fields=27.76MB  count=495746
+`Expression`                                       metadata=12.75MB  fields=6.37MB   count=265605
 `StringLiteralType`                                metadata=3.24MB   fields=5.88MB   count=57943
 `IntersectionType`                                 metadata=1.99MB   fields=4.60MB   count=35545
 `File`                                             metadata=1.69MB   fields=4.52MB   count=26449
@@ -24,12 +24,12 @@
 `FileModule`                                       metadata=0.11MB   fields=0.29MB   count=1992
 `GenericAlias`                                     metadata=0.91MB   fields=0.26MB   count=16249
 `ModuleNameIngredient`                             metadata=0.24MB   fields=0.23MB   count=4344
-`Unpack`                                           metadata=0.17MB   fields=0.20MB   count=4223
 `ModuleLiteralType`                                metadata=0.44MB   fields=0.17MB   count=8470
 `TypeVarInstance`                                  metadata=0.12MB   fields=0.16MB   count=2176
 `StarImportPlaceholderPredicate`                   metadata=0.22MB   fields=0.16MB   count=7889
 `BoundMethodType`                                  metadata=0.36MB   fields=0.16MB   count=6485
 `GenericContext`                                   metadata=0.10MB   fields=0.15MB   count=1822
+`Unpack`                                           metadata=0.17MB   fields=0.14MB   count=4223
 `BoundTypeVarInstance`                             metadata=0.25MB   fields=0.07MB   count=4513
 `PatternPredicate`                                 metadata=0.02MB   fields=0.07MB   count=768
 `Project`                                          metadata=0.00MB   fields=0.06MB   count=1
@@ -51,7 +51,7 @@
 `module_type_symbols::interned_arguments`          metadata=0.00MB   fields=0.00MB   count=1
 =======SALSA QUERIES=======
 `semantic_index -> ty_python_semantic::semantic_index::SemanticIndex`
-    metadata=21.75MB  fields=409.71MB count=6117
+    metadata=21.75MB  fields=407.52MB count=6117
 `parsed_module -> ruff_db::parsed::ParsedModule`
     metadata=0.47MB   fields=284.40MB count=6126
 `use_def_map -> alloc::sync::Arc<ty_python_semantic::semantic_index::use_def::UseDefMap>`
@@ -157,8 +157,8 @@
 `TypeVarInstance < 'db >::lazy_constraints_ -> core::option::Option<ty_python_semantic::types::TypeVarBoundOrConstraints>`
     metadata=0.00MB   fields=0.00MB   count=3
 =======SALSA SUMMARY=======
-TOTAL MEMORY USAGE: 1420.38MB
+TOTAL MEMORY USAGE: 1397.73MB
     struct metadata = 71.31MB
-    struct fields = 93.10MB
+    struct fields = 72.63MB
     memo metadata = 213.48MB
-    memo fields = 1042.49MB
+    memo fields = 1040.30MB

@ibraheemdev ibraheemdev merged commit 7abc417 into main Aug 22, 2025
35 checks passed
@ibraheemdev ibraheemdev deleted the ibraheem/ast-node-ref-size branch August 22, 2025 21:03
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
## Summary

Removes the `module_ptr` field from `AstNodeRef` in release mode, and
change `NodeIndex` to a `NonZeroU32` to reduce the size of
`Option<AstNodeRef<_>>` fields.

I believe CI runs in debug mode, so this won't show up in the memory
report, but this reduces memory by ~2% in release mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants