Skip to content

[ty] Remove excess capacity from more Salsa cached collections#25411

Merged
MichaReiser merged 6 commits into
mainfrom
micha/shrink-collections
May 28, 2026
Merged

[ty] Remove excess capacity from more Salsa cached collections#25411
MichaReiser merged 6 commits into
mainfrom
micha/shrink-collections

Conversation

@MichaReiser

@MichaReiser MichaReiser commented May 27, 2026

Copy link
Copy Markdown
Member

Summary

This PR drops container excess capacities in more places for Salsa cached queries by using a boxed slice or calling shrink_to_fit.

This PR also removes a few unnecessary shrink_to_fit calls. For maps, calling shrink_to_fit is only necessary after extending or when elements were removed. A map that was built by only using insert or entry never shrinks its capacity. This helps improve performance a little.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 27, 2026
@MichaReiser MichaReiser force-pushed the micha/shrink-collections branch from d83258c to feee433 Compare May 27, 2026 07:02
@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 91.94%. The percentage of expected errors that received a diagnostic held steady at 87.09%. The number of fully passing files held steady at 92/134.

@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 46.88MB 46.81MB -0.14% (66.50kB) ⬇️
trio 113.98MB 113.79MB -0.16% (191.79kB) ⬇️
sphinx 268.00MB 267.47MB -0.20% (548.43kB) ⬇️
prefect 723.46MB 722.18MB -0.18% (1.28MB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
when_constraint_set_assignable_to_owned_impl 314.95kB 261.90kB -16.84% (53.05kB) ⬇️
assignable_solutions_impl 35.28kB 26.21kB -25.71% (9.07kB) ⬇️
InferableTypeVarsInner 26.73kB 22.94kB -14.18% (3.79kB) ⬇️
StaticClassLiteral<'db>::own_fields_ 7.63kB 7.04kB -7.68% (600.00B) ⬇️

trio

Name Old New Diff Outcome
when_constraint_set_assignable_to_owned_impl 787.43kB 630.57kB -19.92% (156.85kB) ⬇️
assignable_solutions_impl 85.04kB 66.93kB -21.30% (18.11kB) ⬇️
InferableTypeVarsInner 74.48kB 64.66kB -13.19% (9.82kB) ⬇️
StaticClassLiteral<'db>::fields_inner_ 13.92kB 9.35kB -32.83% (4.57kB) ⬇️
StaticClassLiteral<'db>::own_fields_ 8.46kB 6.35kB -24.93% (2.11kB) ⬇️
semantic_index 30.11MB 30.11MB -0.00% (264.00B) ⬇️
class_mro_literals 476.00B 412.00B -13.45% (64.00B) ⬇️

sphinx

Name Old New Diff Outcome
when_constraint_set_assignable_to_owned_impl 2.90MB 2.45MB -15.53% (461.85kB) ⬇️
assignable_solutions_impl 357.12kB 289.12kB -19.04% (68.00kB) ⬇️
InferableTypeVarsInner 88.06kB 76.73kB -12.86% (11.33kB) ⬇️
StaticClassLiteral<'db>::own_fields_ 36.72kB 31.80kB -13.40% (4.92kB) ⬇️
StaticClassLiteral<'db>::fields_inner_ 7.85kB 6.09kB -22.39% (1.76kB) ⬇️
semantic_index 61.89MB 61.89MB -0.00% (528.00B) ⬇️
class_mro_literals 316.00B 260.00B -17.72% (56.00B) ⬇️

prefect

Name Old New Diff Outcome
when_constraint_set_assignable_to_owned_impl 6.02MB 4.95MB -17.74% (1.07MB) ⬇️
assignable_solutions_impl 582.89kB 471.31kB -19.14% (111.58kB) ⬇️
StaticClassLiteral<'db>::own_fields_ 166.24kB 133.78kB -19.53% (32.46kB) ⬇️
StaticClassLiteral<'db>::fields_inner_ 118.89kB 88.18kB -25.83% (30.70kB) ⬇️
InferableTypeVarsInner 211.08kB 185.84kB -11.95% (25.23kB) ⬇️
ClassType<'db>::abstract_methods_ 152.86kB 142.36kB -6.87% (10.50kB) ⬇️
semantic_index 186.57MB 186.57MB -0.00% (2.89kB) ⬇️
FunctionType<'db>::last_definition_raw_signature_ 5.92MB 5.92MB -0.05% (2.81kB) ⬇️
FunctionType<'db>::signature_ 4.53MB 4.52MB -0.04% (2.03kB) ⬇️
FunctionType<'db>::last_definition_signature_ 834.09kB 832.06kB -0.24% (2.03kB) ⬇️
class_mro_literals 6.96kB 6.05kB -13.07% (932.00B) ⬇️

@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@MichaReiser MichaReiser force-pushed the micha/shrink-collections branch from af6bd74 to ad53af8 Compare May 27, 2026 08:31
}

pub(super) fn finish(mut self) -> AstIds {
self.uses_map.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a HashMap and we only append to it using insert.

place_tables.shrink_to_fit();
use_def_maps.shrink_to_fit();
ast_ids.shrink_to_fit();
self.definitions_by_node.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hash map, insertion only by using entry and insert

use_def_maps.shrink_to_fit();
ast_ids.shrink_to_fit();
self.definitions_by_node.shrink_to_fit();
self.statements_by_node.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hash map, insertion only by using insert

self.definitions_by_node.shrink_to_fit();
self.statements_by_node.shrink_to_fit();
self.enclosing_lambda_statements.shrink_to_fit();
self.collections_by_use.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, insertion with insert

self.statements_by_node.shrink_to_fit();
self.enclosing_lambda_statements.shrink_to_fit();
self.collections_by_use.shrink_to_fit();
self.uses_by_collection.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, insertion using entry


self.imported_modules.shrink_to_fit();
self.scope_ids_by_scope.shrink_to_fit();
self.scopes_by_node.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, insertion exclusively using insert

self.imported_modules.shrink_to_fit();
self.scope_ids_by_scope.shrink_to_fit();
self.scopes_by_node.shrink_to_fit();
self.generator_functions.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hashset, insertion exclusively using insert

self.reachable_symbol_definitions.shrink_to_fit();
self.reachable_member_definitions.shrink_to_fit();
self.bindings_by_use.shrink_to_fit();
self.multi_bindings_by_use.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, insertion with entry

self.bindings_by_use.shrink_to_fit();
self.multi_bindings_by_use.shrink_to_fit();
self.range_reachability.shrink_to_fit();
self.declarations_by_binding.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, insertion with insert

interned_ids_by_definition.insert(definition, interned_id);
}

interned_ids_by_definition.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, Uses insert

interned_ids_by_binding.insert(binding, interned_id);
}

interned_ids_by_binding.shrink_to_fit();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

HashMap, uses insert

@MichaReiser MichaReiser marked this pull request as ready for review May 27, 2026 11:15
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh May 27, 2026 11:15
@MichaReiser MichaReiser changed the title [ty] Shrink more Salsa cached collections [ty] Remove excess capacity from more Salsa cached collections May 27, 2026

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes sense to me, though it's worth noting that the pydantic and multithreaded benchmarks are showing 1-2% regressions

Comment thread crates/ty_ide/src/symbols.rs Outdated
Comment on lines 465 to 469
fn sorted_list(db: &dyn Db) -> Vec<Module<'_>> {
let mut modules = list_modules(db);
let mut modules = list_modules(db).to_vec();
modules.sort_by(|m1, m2| m1.name(db).cmp(m2.name(db)));
modules
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there ever a reason for wanting an unsorted list of modules? We could consider sorting the list of modules before returning the boxed slice from the cached list_modules function, and getting rid of this additional (uncached) function

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, import completions doesn't bother about sorting and all modules applies a global sorting.

Comment thread crates/ty_python_semantic/src/types/class/static_literal.rs
Comment thread crates/ty_python_semantic/src/types/enums.rs
@MichaReiser MichaReiser force-pushed the micha/shrink-collections branch from 350c084 to e57cc69 Compare May 28, 2026 07:00
@MichaReiser MichaReiser enabled auto-merge (squash) May 28, 2026 07:01
@MichaReiser MichaReiser changed the title [ty] Remove excess capacity from more Salsa cached collections [ty] Remove excess capacity from more Salsa cached containers May 28, 2026
@MichaReiser MichaReiser changed the title [ty] Remove excess capacity from more Salsa cached containers [ty] Remove excess capacity from more Salsa cached collections May 28, 2026
@MichaReiser MichaReiser merged commit e2e1e64 into main May 28, 2026
58 checks passed
@MichaReiser MichaReiser deleted the micha/shrink-collections branch May 28, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants