Skip to content

Commit 1f85103

Browse files
ndmitchellfacebook-github-bot
authored andcommitted
Add available_bytes to the heaps
Summary: Useful for figuring out memory that is allocated but not used. Reviewed By: swgillespie, krallin Differential Revision: D30939477 fbshipit-source-id: 17c4ab1f40c94197a844aec86e9cadc0da25b6a8
1 parent 41f4c38 commit 1f85103

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

starlark/src/values/layout/arena.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl Arena {
117117
self.drop.allocated_bytes() + self.non_drop.allocated_bytes()
118118
}
119119

120+
pub fn available_bytes(&self) -> usize {
121+
self.drop.chunk_capacity() + self.non_drop.chunk_capacity()
122+
}
123+
120124
/// Bytes allocated which can't be iterated over
121125
pub fn allocated_bytes_inline(&self) -> usize {
122126
self.non_drop.allocated_bytes()
@@ -478,6 +482,6 @@ mod test {
478482
assert_eq!(res.len(), 1);
479483
let entry = res.values().next().unwrap();
480484
assert_eq!(entry.0, 2);
481-
assert_eq!(entry.1, arena.allocated_bytes())
485+
assert_eq!(entry.1, arena.allocated_bytes());
482486
}
483487
}

starlark/src/values/layout/heap.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ impl FrozenHeapRef {
133133
self.0.arena.allocated_bytes()
134134
}
135135

136+
/// Number of bytes allocated by the heap but not filled.
137+
/// Note that these bytes will _never_ be filled as no further allocations can
138+
/// be made on this heap (it has been sealed).
139+
pub fn available_bytes(&self) -> usize {
140+
self.0.arena.available_bytes()
141+
}
142+
136143
/// Obtain a summary of how much memory is currently allocated by this heap.
137144
/// Doesn't include the heaps it keeps alive by reference.
138145
pub fn allocated_summary(&self) -> HeapSummary {
@@ -204,6 +211,11 @@ impl FrozenHeap {
204211
self.arena.allocated_bytes()
205212
}
206213

214+
/// Number of bytes allocated by the heap but not yet filled.
215+
pub fn available_bytes(&self) -> usize {
216+
self.arena.available_bytes()
217+
}
218+
207219
/// Obtain a summary of how much memory is currently allocated by this heap.
208220
pub fn allocated_summary(&self) -> HeapSummary {
209221
self.arena.allocated_summary()
@@ -289,6 +301,11 @@ impl Heap {
289301
self.arena.borrow().allocated_bytes()
290302
}
291303

304+
/// Number of bytes allocated by the heap but not yet filled.
305+
pub fn available_bytes(&self) -> usize {
306+
self.arena.borrow().available_bytes()
307+
}
308+
292309
/// Only those allocated on the inline heap (mostly strings)
293310
pub(crate) fn allocated_bytes_inline(&self) -> usize {
294311
self.arena.borrow().allocated_bytes_inline()

0 commit comments

Comments
 (0)