Skip to content

Commit 21ac16d

Browse files
authored
[ty] Avoid overcounting shared memory usage (#19773)
## Summary Use a global tracker to avoid double counting `Arc` instances.
1 parent 745742e commit 21ac16d

File tree

30 files changed

+167
-163
lines changed

30 files changed

+167
-163
lines changed

Cargo.lock

Lines changed: 68 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ruff_graph = { path = "crates/ruff_graph" }
2323
ruff_index = { path = "crates/ruff_index" }
2424
ruff_linter = { path = "crates/ruff_linter" }
2525
ruff_macros = { path = "crates/ruff_macros" }
26+
ruff_memory_usage = { path = "crates/ruff_memory_usage" }
2627
ruff_notebook = { path = "crates/ruff_notebook" }
2728
ruff_options_metadata = { path = "crates/ruff_options_metadata" }
2829
ruff_python_ast = { path = "crates/ruff_python_ast" }
@@ -84,7 +85,7 @@ etcetera = { version = "0.10.0" }
8485
fern = { version = "0.7.0" }
8586
filetime = { version = "0.2.23" }
8687
getrandom = { version = "0.3.1" }
87-
get-size2 = { version = "0.6.0", features = [
88+
get-size2 = { version = "0.6.2", features = [
8889
"derive",
8990
"smallvec",
9091
"hashbrown",
@@ -142,7 +143,7 @@ regex-automata = { version = "0.4.9" }
142143
rustc-hash = { version = "2.0.0" }
143144
rustc-stable-hash = { version = "0.1.2" }
144145
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
145-
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "d66fe331d546216132ace503512b94d5c68d2c50", default-features = false, features = [
146+
salsa = { git = "https://github.com/salsa-rs/salsa.git", rev = "b121ee46c4483ba74c19e933a3522bd548eb7343", default-features = false, features = [
146147
"compact_str",
147148
"macros",
148149
"salsa_unstable",

crates/ruff_db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ license = { workspace = true }
1414
ruff_annotate_snippets = { workspace = true }
1515
ruff_cache = { workspace = true, optional = true }
1616
ruff_diagnostics = { workspace = true }
17+
ruff_memory_usage = { workspace = true }
1718
ruff_notebook = { workspace = true }
1819
ruff_python_ast = { workspace = true, features = ["get-size"] }
1920
ruff_python_parser = { workspace = true }

crates/ruff_db/src/parsed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::source::source_text;
2121
/// reflected in the changed AST offsets.
2222
/// The other reason is that Ruff's AST doesn't implement `Eq` which Salsa requires
2323
/// for determining if a query result is unchanged.
24-
#[salsa::tracked(returns(ref), no_eq, heap_size=get_size2::heap_size)]
24+
#[salsa::tracked(returns(ref), no_eq, heap_size=ruff_memory_usage::heap_size)]
2525
pub fn parsed_module(db: &dyn Db, file: File) -> ParsedModule {
2626
let _span = tracing::trace_span!("parsed_module", ?file).entered();
2727

crates/ruff_db/src/source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::Db;
99
use crate::files::{File, FilePath};
1010

1111
/// Reads the source text of a python text file (must be valid UTF8) or notebook.
12-
#[salsa::tracked(heap_size=get_size2::heap_size)]
12+
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
1313
pub fn source_text(db: &dyn Db, file: File) -> SourceText {
1414
let path = file.path(db);
1515
let _span = tracing::trace_span!("source_text", file = %path).entered();
@@ -157,7 +157,7 @@ pub enum SourceTextError {
157157
}
158158

159159
/// Computes the [`LineIndex`] for `file`.
160-
#[salsa::tracked(heap_size=get_size2::heap_size)]
160+
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
161161
pub fn line_index(db: &dyn Db, file: File) -> LineIndex {
162162
let _span = tracing::trace_span!("line_index", ?file).entered();
163163

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "ruff_memory_usage"
3+
version = "0.0.0"
4+
publish = false
5+
authors = { workspace = true }
6+
edition = { workspace = true }
7+
rust-version = { workspace = true }
8+
homepage = { workspace = true }
9+
documentation = { workspace = true }
10+
repository = { workspace = true }
11+
license = { workspace = true }
12+
13+
[dependencies]
14+
get-size2 = { workspace = true }
15+
16+
[lints]
17+
workspace = true
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::sync::{LazyLock, Mutex};
2+
3+
use get_size2::{GetSize, StandardTracker};
4+
5+
/// Returns the memory usage of the provided object, using a global tracker to avoid
6+
/// double-counting shared objects.
7+
pub fn heap_size<T: GetSize>(value: &T) -> usize {
8+
static TRACKER: LazyLock<Mutex<StandardTracker>> =
9+
LazyLock::new(|| Mutex::new(StandardTracker::new()));
10+
11+
value
12+
.get_heap_size_with_tracker(&mut *TRACKER.lock().unwrap())
13+
.0
14+
}

crates/ty_project/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ license.workspace = true
1515
ruff_cache = { workspace = true }
1616
ruff_db = { workspace = true, features = ["cache", "serde"] }
1717
ruff_macros = { workspace = true }
18+
ruff_memory_usage = { workspace = true }
1819
ruff_options_metadata = { workspace = true }
1920
ruff_python_ast = { workspace = true, features = ["serde"] }
2021
ruff_python_formatter = { workspace = true, optional = true }

crates/ty_project/src/db.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ impl ProjectDatabase {
127127
/// Returns a [`SalsaMemoryDump`] that can be use to dump Salsa memory usage information
128128
/// to the CLI after a typechecker run.
129129
pub fn salsa_memory_dump(&self) -> SalsaMemoryDump {
130-
let salsa_db = self as &dyn salsa::Database;
131-
132-
let mut ingredients = salsa_db.structs_info();
133-
let mut memos = salsa_db.queries_info().into_iter().collect::<Vec<_>>();
130+
let memory_usage = <dyn salsa::Database>::memory_usage(self);
131+
let mut ingredients = memory_usage.structs;
132+
let mut memos = memory_usage.queries.into_iter().collect::<Vec<_>>();
134133

135134
ingredients.sort_by_key(|ingredient| cmp::Reverse(ingredient.size_of_fields()));
136135
memos.sort_by_key(|(_, memo)| cmp::Reverse(memo.size_of_fields()));
@@ -140,12 +139,14 @@ impl ProjectDatabase {
140139
for ingredient in &ingredients {
141140
total_metadata += ingredient.size_of_metadata();
142141
total_fields += ingredient.size_of_fields();
142+
total_fields += ingredient.heap_size_of_fields().unwrap_or(0);
143143
}
144144

145145
let mut total_memo_fields = 0;
146146
let mut total_memo_metadata = 0;
147147
for (_, memo) in &memos {
148148
total_memo_fields += memo.size_of_fields();
149+
total_memo_fields += memo.heap_size_of_fields().unwrap_or(0);
149150
total_memo_metadata += memo.size_of_metadata();
150151
}
151152

crates/ty_project/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Project {
201201
/// This is a salsa query to prevent re-computing queries if other, unrelated
202202
/// settings change. For example, we don't want that changing the terminal settings
203203
/// invalidates any type checking queries.
204-
#[salsa::tracked(returns(deref), heap_size=get_size2::heap_size)]
204+
#[salsa::tracked(returns(deref), heap_size=ruff_memory_usage::heap_size)]
205205
pub fn rules(self, db: &dyn Db) -> Arc<RuleSelection> {
206206
self.settings(db).to_rules()
207207
}
@@ -524,7 +524,7 @@ impl Project {
524524
}
525525
}
526526

527-
#[salsa::tracked(returns(ref), heap_size=get_size2::heap_size)]
527+
#[salsa::tracked(returns(ref), heap_size=ruff_memory_usage::heap_size)]
528528
pub(crate) fn check_file_impl(db: &dyn Db, file: File) -> Result<Box<[Diagnostic]>, Diagnostic> {
529529
let mut diagnostics: Vec<Diagnostic> = Vec::new();
530530

0 commit comments

Comments
 (0)