Skip to content

Commit f72569d

Browse files
committed
avoid overcounting memory usage of shared objects
1 parent 5bfffe1 commit f72569d

File tree

25 files changed

+96
-53
lines changed

25 files changed

+96
-53
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
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" }
@@ -83,7 +84,7 @@ etcetera = { version = "0.10.0" }
8384
fern = { version = "0.7.0" }
8485
filetime = { version = "0.2.23" }
8586
getrandom = { version = "0.3.1" }
86-
get-size2 = { version = "0.6.0", features = [
87+
get-size2 = { git = "https://github.com/ibraheemdev/get-size2", branch = "ibraheem/non-static-tracked", features = [
8788
"derive",
8889
"smallvec",
8990
"hashbrown",

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/lib.rs

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

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

crates/ty_project/src/metadata/settings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Override {
9696
}
9797

9898
/// Resolves the settings for a given file.
99-
#[salsa::tracked(returns(ref), heap_size=get_size2::heap_size)]
99+
#[salsa::tracked(returns(ref), heap_size=ruff_memory_usage::heap_size)]
100100
pub(crate) fn file_settings(db: &dyn Db, file: File) -> FileSettings {
101101
let settings = db.project().settings(db);
102102

@@ -155,7 +155,7 @@ pub(crate) fn file_settings(db: &dyn Db, file: File) -> FileSettings {
155155
/// This is to make Salsa happy because it requires that queries with only a single argument
156156
/// take a salsa-struct as argument, which isn't the case here. The `()` enables salsa's
157157
/// automatic interning for the arguments.
158-
#[salsa::tracked(heap_size=get_size2::heap_size)]
158+
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
159159
fn merge_overrides(db: &dyn Db, overrides: Vec<Arc<InnerOverrideOptions>>, _: ()) -> FileSettings {
160160
let mut overrides = overrides.into_iter().rev();
161161
let mut merged = (*overrides.next().unwrap()).clone();

0 commit comments

Comments
 (0)