Skip to content

Commit

Permalink
Change StorageMemoryUsage type from struct to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
notinmybackyaard committed Feb 26, 2025
1 parent 1ce9bca commit 6b2e07a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 53 deletions.
6 changes: 2 additions & 4 deletions src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::storage::Storage;
use crate::tracking::TrackingTimestamp;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::any::type_name;
use core::iter::repeat_with;
use core::mem::size_of;

Expand Down Expand Up @@ -430,12 +429,11 @@ impl Storage for Entities {
self.list = Some((self.data.len() - end - 1, begin));
}
fn memory_usage(&self) -> Option<StorageMemoryUsage> {
Some(StorageMemoryUsage {
storage_name: type_name::<Self>().into(),
Some(StorageMemoryUsage::Entities {
allocated_memory_bytes: (self.data.capacity() * size_of::<EntityId>())
+ size_of::<Entities>(),
used_memory_bytes: (self.data.len() * size_of::<EntityId>()) + size_of::<Entities>(),
component_count: self.data.len(),
entity_count: self.data.len(),
})
}
fn is_empty(&self) -> bool {
Expand Down
62 changes: 43 additions & 19 deletions src/memory_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,50 @@ pub struct WorldMemoryUsage<'w>(pub(crate) &'w World);

pub struct AllStoragesMemoryUsage<'a>(pub(crate) &'a AllStorages);

/// A trait to query the amount of memory a storage uses.
pub struct StorageMemoryUsage {
#[allow(missing_docs)]
pub storage_name: Cow<'static, str>,
/// Amount of memory used by the storage in bytes.
pub used_memory_bytes: usize,
/// Amount of memory allocated by the storage in bytes (including reserved memory).
pub allocated_memory_bytes: usize,
#[allow(missing_docs)]
pub component_count: usize,
#[derive(Debug)]
pub struct SparseSetMemoryUsage {
pub spase: usize,
pub dense: usize,
pub data: usize,
pub insertion_data: usize,
pub modification_data: usize,
pub deletion_data: usize,
pub removal_data: usize,
pub self_data: usize,
}

impl core::fmt::Debug for StorageMemoryUsage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!(
"{}: {} bytes used for {} components ({} bytes reserved in total)",
self.storage_name,
self.used_memory_bytes,
self.component_count,
self.allocated_memory_bytes
))
impl SparseSetMemoryUsage {
pub fn sum(&self) -> usize {
self.spase
+ self.dense
+ self.data
+ self.insertion_data
+ self.modification_data
+ self.deletion_data
+ self.removal_data
+ self.self_data
}
}

/// A enum to query the amount of memory a storage uses.
#[allow(missing_docs, unused)]
#[derive(Debug)]
pub enum StorageMemoryUsage {
Entities {
used_memory_bytes: usize,
allocated_memory_bytes: usize,
entity_count: usize,
},
SparseSet {
storage_name: Cow<'static, str>,
used_memory_usage: SparseSetMemoryUsage,
allocated_memory_usage: SparseSetMemoryUsage,
used_memory_bytes: usize,
allocated_memory_bytes: usize,
component_count: usize,
},
Unique {
storage_name: Cow<'static, str>,
used_memory_bytes: usize,
},
}
57 changes: 30 additions & 27 deletions src/sparse_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::borrow::{NonSend, NonSendSync, NonSync};
use crate::component::Component;
use crate::entity_id::EntityId;
use crate::error;
use crate::memory_usage::StorageMemoryUsage;
use crate::memory_usage::{SparseSetMemoryUsage, StorageMemoryUsage};
use crate::r#mut::Mut;
use crate::storage::{Storage, StorageId};
use crate::tracking::{Tracking, TrackingTimestamp};
Expand Down Expand Up @@ -746,33 +746,35 @@ impl<T: Component> SparseSet<T> {

impl<T: Component> SparseSet<T> {
fn private_memory_usage(&self) -> StorageMemoryUsage {
StorageMemoryUsage {
let used_memory_usage = SparseSetMemoryUsage {
spase: self.sparse.used_memory(),
dense: self.dense.len() * size_of::<EntityId>(),
data: self.data.len() * size_of::<T>(),
insertion_data: self.insertion_data.len() * size_of::<TrackingTimestamp>(),
modification_data: self.modification_data.len() * size_of::<TrackingTimestamp>(),
deletion_data: self.deletion_data.len() * size_of::<(EntityId, TrackingTimestamp, T)>(),
removal_data: self.removal_data.len() * size_of::<(EntityId, TrackingTimestamp)>(),
self_data: size_of::<Self>(),
};
let allocated_memory_usage = SparseSetMemoryUsage {
spase: self.sparse.reserved_memory(),
dense: self.dense.capacity() * size_of::<EntityId>(),
data: self.data.capacity() * size_of::<T>(),
insertion_data: self.insertion_data.capacity() * size_of::<TrackingTimestamp>(),
modification_data: self.modification_data.capacity() * size_of::<TrackingTimestamp>(),
deletion_data: self.deletion_data.capacity() * size_of::<(EntityId, TrackingTimestamp, T)>(),
removal_data: self.removal_data.capacity() * size_of::<(EntityId, TrackingTimestamp)>(),
self_data: size_of::<Self>(),
};
StorageMemoryUsage::SparseSet {
storage_name: type_name::<Self>().into(),
allocated_memory_bytes: self.allocated_memory_bytes(),
used_memory_bytes: self.used_memory_bytes(),
used_memory_bytes: used_memory_usage.sum(),
allocated_memory_bytes: allocated_memory_usage.sum(),
used_memory_usage,
allocated_memory_usage,
component_count: self.len(),
}
}
fn allocated_memory_bytes(&self) -> usize {
self.sparse.reserved_memory()
+ (self.dense.capacity() * size_of::<EntityId>())
+ (self.data.capacity() * size_of::<T>())
+ (self.insertion_data.capacity() * size_of::<TrackingTimestamp>())
+ (self.modification_data.capacity() * size_of::<TrackingTimestamp>())
+ (self.deletion_data.capacity() * size_of::<(EntityId, TrackingTimestamp, T)>())
+ (self.removal_data.capacity() * size_of::<(EntityId, TrackingTimestamp)>())
+ size_of::<Self>()
}
fn used_memory_bytes(&self) -> usize {
self.sparse.used_memory()
+ (self.dense.len() * size_of::<EntityId>())
+ (self.data.len() * size_of::<T>())
+ (self.insertion_data.len() * size_of::<TrackingTimestamp>())
+ (self.modification_data.len() * size_of::<TrackingTimestamp>())
+ (self.deletion_data.len() * size_of::<(EntityId, TrackingTimestamp, T)>())
+ (self.removal_data.len() * size_of::<(EntityId, TrackingTimestamp)>())
+ size_of::<Self>()
}
}

impl<T: Ord + Component> SparseSet<T> {
Expand Down Expand Up @@ -1433,8 +1435,9 @@ mod tests {
+ expected_removal_tracking_memory
+ expected_self_memory;

let memory_usage = sparse_set.private_memory_usage();

assert_eq!(memory_usage.used_memory_bytes, expected_total_memory);
let StorageMemoryUsage::SparseSet { used_memory_bytes, .. } = sparse_set.private_memory_usage() else {
unreachable!()
};
assert_eq!(used_memory_bytes, expected_total_memory);
}
}
4 changes: 1 addition & 3 deletions src/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ pub struct UniqueStorage<T: Unique> {

impl<T: Unique> Storage for UniqueStorage<T> {
fn memory_usage(&self) -> Option<StorageMemoryUsage> {
Some(StorageMemoryUsage {
Some(StorageMemoryUsage::Unique {
storage_name: type_name::<Self>().into(),
allocated_memory_bytes: size_of::<Self>(),
used_memory_bytes: size_of::<Self>(),
component_count: 1,
})
}
fn is_empty(&self) -> bool {
Expand Down

0 comments on commit 6b2e07a

Please sign in to comment.