Skip to content

Commit

Permalink
Add available_bytes to the heaps
Browse files Browse the repository at this point in the history
Summary: Useful for figuring out memory that is allocated but not used.

Reviewed By: swgillespie, krallin

Differential Revision: D30939477

fbshipit-source-id: 17c4ab1f40c94197a844aec86e9cadc0da25b6a8
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Sep 14, 2021
1 parent 89b0c9c commit 7ecffc1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ impl Arena {
self.drop.allocated_bytes() + self.non_drop.allocated_bytes()
}

pub fn available_bytes(&self) -> usize {
self.drop.chunk_capacity() + self.non_drop.chunk_capacity()
}

/// Bytes allocated which can't be iterated over
pub fn allocated_bytes_inline(&self) -> usize {
self.non_drop.allocated_bytes()
Expand Down Expand Up @@ -478,6 +482,6 @@ mod test {
assert_eq!(res.len(), 1);
let entry = res.values().next().unwrap();
assert_eq!(entry.0, 2);
assert_eq!(entry.1, arena.allocated_bytes())
assert_eq!(entry.1, arena.allocated_bytes());
}
}
17 changes: 17 additions & 0 deletions third-party/rust/starlark-rust/starlark/src/values/layout/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ impl FrozenHeapRef {
self.0.arena.allocated_bytes()
}

/// Number of bytes allocated by the heap but not filled.
/// Note that these bytes will _never_ be filled as no further allocations can
/// be made on this heap (it has been sealed).
pub fn available_bytes(&self) -> usize {
self.0.arena.available_bytes()
}

/// Obtain a summary of how much memory is currently allocated by this heap.
/// Doesn't include the heaps it keeps alive by reference.
pub fn allocated_summary(&self) -> HeapSummary {
Expand Down Expand Up @@ -204,6 +211,11 @@ impl FrozenHeap {
self.arena.allocated_bytes()
}

/// Number of bytes allocated by the heap but not yet filled.
pub fn available_bytes(&self) -> usize {
self.arena.available_bytes()
}

/// Obtain a summary of how much memory is currently allocated by this heap.
pub fn allocated_summary(&self) -> HeapSummary {
self.arena.allocated_summary()
Expand Down Expand Up @@ -289,6 +301,11 @@ impl Heap {
self.arena.borrow().allocated_bytes()
}

/// Number of bytes allocated by the heap but not yet filled.
pub fn available_bytes(&self) -> usize {
self.arena.borrow().available_bytes()
}

/// Only those allocated on the inline heap (mostly strings)
pub(crate) fn allocated_bytes_inline(&self) -> usize {
self.arena.borrow().allocated_bytes_inline()
Expand Down

0 comments on commit 7ecffc1

Please sign in to comment.