Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ default-features = false
optional = true
version = "1"
default-features = false

[dev-dependencies]

[dev-dependencies.dhat]
version = "0.3.3"
9 changes: 9 additions & 0 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,15 @@ impl BigInt {
// The top bit may have been cleared, so normalize
self.normalize();
}

/// Returns the total amount of memory allocated internally by the
/// big int, in bytes.
///
/// The returned number is informational only. It is intended to be
/// primarily used for memory profiling.
pub fn allocation_size(&self) -> usize {
self.data.allocation_size()
}
}

impl num_traits::FromBytes for BigInt {
Expand Down
9 changes: 9 additions & 0 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,15 @@ impl BigUint {
self.normalize();
}
}

/// Returns the total amount of memory allocated internally by the
/// big uint, in bytes.
///
/// The returned number is informational only. It is intended to be
/// primarily used for memory profiling.
pub fn allocation_size(&self) -> usize {
self.data.capacity() * std::mem::size_of::<BigDigit>()
}
}

impl num_traits::FromBytes for BigUint {
Expand Down
12 changes: 12 additions & 0 deletions tests/bigint_allocation_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use num_bigint::BigInt;

#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

#[test]
fn test_biguint_allocation_size() {
let _profiler = dhat::Profiler::builder().testing().build();
let big: BigInt = "-1234567898765432123456789876543212345678987654321".parse().unwrap();
let stats = dhat::HeapStats::get();
assert_eq!(stats.curr_bytes, big.allocation_size());
}
12 changes: 12 additions & 0 deletions tests/biguint_allocation_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use num_bigint::BigUint;

#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

#[test]
fn test_biguint_allocation_size() {
let _profiler = dhat::Profiler::builder().testing().build();
let big: BigUint = "1234567898765432123456789876543212345678987654321".parse().unwrap();
let stats = dhat::HeapStats::get();
assert_eq!(stats.curr_bytes, big.allocation_size());
}