From b85c0190c561a7ef408a28a1b59e083a3e28e5d1 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sun, 4 Jan 2026 20:54:03 +0100 Subject: [PATCH] [ty] Use upstream `GetSize` implementation for `OrderMap` and `OrderSet` (#22374) --- Cargo.lock | 1 - Cargo.toml | 1 + crates/ruff_memory_usage/Cargo.toml | 1 - crates/ruff_memory_usage/src/lib.rs | 14 -------------- crates/ty_python_semantic/src/types.rs | 7 +------ crates/ty_python_semantic/src/types/generics.rs | 8 +------- 6 files changed, 3 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 223701757a..e994474ef3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3246,7 +3246,6 @@ name = "ruff_memory_usage" version = "0.0.0" dependencies = [ "get-size2", - "ordermap", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f85194d486..cca70fbdc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,7 @@ get-size2 = { version = "0.7.3", features = [ "smallvec", "hashbrown", "compact-str", + "ordermap" ] } getrandom = { version = "0.3.1" } glob = { version = "0.3.1" } diff --git a/crates/ruff_memory_usage/Cargo.toml b/crates/ruff_memory_usage/Cargo.toml index fd6b408303..1b87de4bbf 100644 --- a/crates/ruff_memory_usage/Cargo.toml +++ b/crates/ruff_memory_usage/Cargo.toml @@ -12,7 +12,6 @@ license = { workspace = true } [dependencies] get-size2 = { workspace = true } -ordermap = { workspace = true } [lints] workspace = true diff --git a/crates/ruff_memory_usage/src/lib.rs b/crates/ruff_memory_usage/src/lib.rs index 81444d5197..bc35b49483 100644 --- a/crates/ruff_memory_usage/src/lib.rs +++ b/crates/ruff_memory_usage/src/lib.rs @@ -1,7 +1,6 @@ use std::cell::RefCell; use get_size2::{GetSize, StandardTracker}; -use ordermap::{OrderMap, OrderSet}; thread_local! { pub static TRACKER: RefCell>= const { RefCell::new(None) }; @@ -42,16 +41,3 @@ pub fn heap_size(value: &T) -> usize { } }) } - -/// An implementation of [`GetSize::get_heap_size`] for [`OrderSet`]. -pub fn order_set_heap_size(set: &OrderSet) -> usize { - (set.capacity() * T::get_stack_size()) + set.iter().map(heap_size).sum::() -} - -/// An implementation of [`GetSize::get_heap_size`] for [`OrderMap`]. -pub fn order_map_heap_size(map: &OrderMap) -> usize { - (map.capacity() * (K::get_stack_size() + V::get_stack_size())) - + (map.iter()) - .map(|(k, v)| heap_size(k) + heap_size(v)) - .sum::() -} diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index c8d1c00280..72cf147a06 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -14364,7 +14364,7 @@ impl KnownUnion { } } -#[salsa::interned(debug, heap_size=IntersectionType::heap_size)] +#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)] pub struct IntersectionType<'db> { /// The intersection type includes only values in all of these types. #[returns(ref)] @@ -14653,11 +14653,6 @@ impl<'db> IntersectionType<'db> { pub(crate) fn is_simple_negation(self, db: &'db dyn Db) -> bool { self.positive(db).is_empty() && self.negative(db).len() == 1 } - - fn heap_size((positive, negative): &(FxOrderSet>, FxOrderSet>)) -> usize { - ruff_memory_usage::order_set_heap_size(positive) - + ruff_memory_usage::order_set_heap_size(negative) - } } /// # Ordering diff --git a/crates/ty_python_semantic/src/types/generics.rs b/crates/ty_python_semantic/src/types/generics.rs index 1117096086..f6058de4b5 100644 --- a/crates/ty_python_semantic/src/types/generics.rs +++ b/crates/ty_python_semantic/src/types/generics.rs @@ -202,7 +202,7 @@ impl<'a, 'db> InferableTypeVars<'a, 'db> { /// # Ordering /// Ordering is based on the context's salsa-assigned id and not on its values. /// The id may change between runs, or when the context was garbage collected and recreated. -#[salsa::interned(debug, constructor=new_internal, heap_size=GenericContext::heap_size)] +#[salsa::interned(debug, constructor=new_internal, heap_size=ruff_memory_usage::heap_size)] #[derive(PartialOrd, Ord)] pub struct GenericContext<'db> { #[returns(ref)] @@ -689,12 +689,6 @@ impl<'db> GenericContext<'db> { Self::from_typevar_instances(db, variables) } - - fn heap_size( - (variables,): &(FxOrderMap, BoundTypeVarInstance<'db>>,), - ) -> usize { - ruff_memory_usage::order_map_heap_size(variables) - } } fn inferable_typevars_cycle_initial<'db>(