-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap some of starlark::values::Heap
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::collections::HashMap; | ||
|
||
use pyo3::{prelude::*, types::PyTuple}; | ||
use starlark::values::Heap; | ||
|
||
/// Information about the data stored on a heap. | ||
#[pyclass(module = "starlark_pyo3", name = "HeapSummary")] | ||
pub(crate) struct PyHeapSummary(HashMap<String, (usize, usize)>); | ||
|
||
impl From<HashMap<String, (usize, usize)>> for PyHeapSummary { | ||
fn from(value: HashMap<String, (usize, usize)>) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyHeapSummary { | ||
/// (Count, total size) by type. | ||
fn summary(slf: PyRef<'_, Self>) -> HashMap<String, Bound<'_, PyTuple>> { | ||
let mut x = HashMap::new(); | ||
for (k, v) in &slf.0 { | ||
let v = vec![v.0, v.1]; | ||
x.insert(k.clone(), PyTuple::new_bound(slf.py(), v)); | ||
} | ||
x | ||
} | ||
|
||
/// Total number of bytes allocated. | ||
#[getter] | ||
fn total_allocated_bytes(&self) -> usize { | ||
self.0.values().map(|(_count, bytes)| bytes).sum() | ||
} | ||
} | ||
|
||
/// A heap on which `Value`s can be allocated. | ||
#[pyclass(module = "starlark_pyo3", name = "Heap")] | ||
pub(crate) struct PyHeap(Heap); | ||
|
||
impl From<Heap> for PyHeap { | ||
fn from(value: Heap) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyHeap { | ||
/// Create a new `Heap`. | ||
#[new] | ||
fn py_new() -> Self { | ||
Heap::new().into() | ||
} | ||
|
||
/// Number of bytes allocated on this heap, not including any memory | ||
/// allocated outside of the starlark heap. | ||
#[getter] | ||
fn allocated_bytes(&self) -> usize { | ||
self.0.allocated_bytes() | ||
} | ||
|
||
/// Peak memory allocated to this heap, even if the value is now lower | ||
/// as a result of a subsequent garbage collection. | ||
#[getter] | ||
fn peak_allocated_bytes(&self) -> usize { | ||
self.0.peak_allocated_bytes() | ||
} | ||
|
||
/// Number of bytes allocated by the heap but not yet filled. | ||
#[getter] | ||
fn available_bytes(&self) -> usize { | ||
self.0.available_bytes() | ||
} | ||
|
||
/// Obtain a summary of how much memory is currently allocated by this heap. | ||
fn allocated_summary(&self) -> PyHeapSummary { | ||
self.0.allocated_summary().summary().into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import starlark_pyo3 | ||
|
||
|
||
def test_empty_heap(): | ||
h = starlark_pyo3.Heap() | ||
assert h.allocated_bytes == 0 | ||
assert h.peak_allocated_bytes == 0 | ||
assert h.available_bytes == 0 | ||
|
||
s = h.allocated_summary() | ||
assert s.summary() == {} | ||
assert s.total_allocated_bytes == 0 |