Skip to content

Commit e999ae9

Browse files
authored
consolidate memory usage information API (#964)
1 parent ea38537 commit e999ae9

File tree

2 files changed

+41
-45
lines changed

2 files changed

+41
-45
lines changed

src/database.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -164,52 +164,32 @@ mod memory_usage {
164164
use hashbrown::HashMap;
165165

166166
impl dyn Database {
167-
/// Returns information about any Salsa structs.
168-
pub fn structs_info(&self) -> Vec<IngredientInfo> {
169-
self.zalsa()
170-
.ingredients()
171-
.filter_map(|ingredient| {
172-
let mut size_of_fields = 0;
173-
let mut size_of_metadata = 0;
174-
let mut instances = 0;
175-
let mut heap_size_of_fields = None;
176-
177-
for slot in ingredient.memory_usage(self)? {
178-
instances += 1;
179-
size_of_fields += slot.size_of_fields;
180-
size_of_metadata += slot.size_of_metadata;
181-
182-
if let Some(slot_heap_size) = slot.heap_size_of_fields {
183-
heap_size_of_fields =
184-
Some(heap_size_of_fields.unwrap_or_default() + slot_heap_size);
185-
}
186-
}
187-
188-
Some(IngredientInfo {
189-
count: instances,
190-
size_of_fields,
191-
size_of_metadata,
192-
heap_size_of_fields,
193-
debug_name: ingredient.debug_name(),
194-
})
195-
})
196-
.collect()
197-
}
198-
199-
/// Returns information about any memoized Salsa queries.
200-
///
201-
/// The returned map holds memory usage information for memoized values of a given query, keyed
202-
/// by the query function name.
203-
pub fn queries_info(&self) -> HashMap<&'static str, IngredientInfo> {
167+
/// Returns memory usage information about ingredients in the database.
168+
pub fn memory_usage(&self) -> DatabaseInfo {
204169
let mut queries = HashMap::new();
170+
let mut structs = Vec::new();
205171

206172
for input_ingredient in self.zalsa().ingredients() {
207173
let Some(input_info) = input_ingredient.memory_usage(self) else {
208174
continue;
209175
};
210176

211-
for input in input_info {
212-
for memo in input.memos {
177+
let mut size_of_fields = 0;
178+
let mut size_of_metadata = 0;
179+
let mut count = 0;
180+
let mut heap_size_of_fields = None;
181+
182+
for input_slot in input_info {
183+
count += 1;
184+
size_of_fields += input_slot.size_of_fields;
185+
size_of_metadata += input_slot.size_of_metadata;
186+
187+
if let Some(slot_heap_size) = input_slot.heap_size_of_fields {
188+
heap_size_of_fields =
189+
Some(heap_size_of_fields.unwrap_or_default() + slot_heap_size);
190+
}
191+
192+
for memo in input_slot.memos {
213193
let info = queries.entry(memo.debug_name).or_insert(IngredientInfo {
214194
debug_name: memo.output.debug_name,
215195
..Default::default()
@@ -225,12 +205,30 @@ mod memory_usage {
225205
}
226206
}
227207
}
208+
209+
structs.push(IngredientInfo {
210+
count,
211+
size_of_fields,
212+
size_of_metadata,
213+
heap_size_of_fields,
214+
debug_name: input_ingredient.debug_name(),
215+
});
228216
}
229217

230-
queries
218+
DatabaseInfo { structs, queries }
231219
}
232220
}
233221

222+
/// Memory usage information about ingredients in the Salsa database.
223+
pub struct DatabaseInfo {
224+
/// Information about any Salsa structs.
225+
pub structs: Vec<IngredientInfo>,
226+
227+
/// Memory usage information for memoized values of a given query, keyed
228+
/// by the query function name.
229+
pub queries: HashMap<&'static str, IngredientInfo>,
230+
}
231+
234232
/// Information about instances of a particular Salsa ingredient.
235233
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
236234
pub struct IngredientInfo {

tests/memory-usage.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn test() {
7676
let _string1 = input_to_string(&db);
7777
let _string2 = input_to_string_get_size(&db);
7878

79-
let structs_info = <dyn salsa::Database>::structs_info(&db);
79+
let memory_usage = <dyn salsa::Database>::memory_usage(&db);
8080

8181
let expected = expect![[r#"
8282
[
@@ -123,11 +123,9 @@ fn test() {
123123
},
124124
]"#]];
125125

126-
expected.assert_eq(&format!("{structs_info:#?}"));
126+
expected.assert_eq(&format!("{:#?}", memory_usage.structs));
127127

128-
let mut queries_info = <dyn salsa::Database>::queries_info(&db)
129-
.into_iter()
130-
.collect::<Vec<_>>();
128+
let mut queries_info = memory_usage.queries.into_iter().collect::<Vec<_>>();
131129
queries_info.sort();
132130

133131
let expected = expect![[r#"

0 commit comments

Comments
 (0)