Skip to content

Commit 33f7623

Browse files
authored
More accurate component, entity and resouce shallow size calculation (#33)
* Take `Entity` size into account * Use `Layout` to measure component size * Refactor `inspect_component_by_id` * Make `ComponentInspection::memory_size` non-optional * Use `ComponentId`'s `Layout` to calculate `Resource` size * Update docs
1 parent d3c6078 commit 33f7623

File tree

4 files changed

+29
-42
lines changed

4 files changed

+29
-42
lines changed

src/extension_methods.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ impl WorldInspectionExtensionTrait for World {
194194

195195
let total_bytes = components
196196
.iter()
197-
.filter_map(|comp| comp.memory_size.as_ref())
198-
.fold(0usize, |acc, size| acc + size.as_bytes());
197+
.map(|comp| comp.memory_size)
198+
.fold(std::mem::size_of::<Entity>(), |acc, size| {
199+
acc + size.as_bytes()
200+
});
199201
let total_memory_size = MemorySize::new(total_bytes);
200202

201203
(Some(components), Some(total_memory_size))
@@ -248,28 +250,26 @@ impl WorldInspectionExtensionTrait for World {
248250
let component_info = self.components().get_info(component_id).ok_or(
249251
ComponentInspectionError::ComponentIdNotRegistered(component_id),
250252
)?;
253+
let memory_size = MemorySize::new(component_info.layout().size());
251254

252255
if !self.entity(entity).contains_id(component_id) {
253256
return Err(ComponentInspectionError::ComponentNotFound(component_id));
254257
}
255258

256259
let name = component_info.name();
257260

258-
let (value, memory_size) = if settings.detail_level == ComponentDetailLevel::Names {
259-
(None, None)
261+
let value = if settings.detail_level == ComponentDetailLevel::Names {
262+
None
260263
} else {
261264
match metadata.type_id {
262265
Some(type_id) => match get_reflected_component_ref(self, entity, type_id) {
263-
Ok(reflected) => (
264-
Some(reflected_value_to_string(
265-
reflected,
266-
settings.full_type_names,
267-
)),
268-
Some(MemorySize::new(size_of_val(reflected))),
269-
),
270-
Err(err) => (Some(format!("<Unreflectable: {}>", err)), None),
266+
Ok(reflected) => Some(reflected_value_to_string(
267+
reflected,
268+
settings.full_type_names,
269+
)),
270+
Err(err) => Some(format!("<Unreflectable: {}>", err)),
271271
},
272-
None => (Some("Dynamic Type".to_string()), None),
272+
None => Some("Dynamic Type".to_string()),
273273
}
274274
};
275275

@@ -317,6 +317,7 @@ impl WorldInspectionExtensionTrait for World {
317317
.components()
318318
.get_info(component_id)
319319
.ok_or(ResourceInspectionError::ResourceNotFound(component_id))?;
320+
let memory_size = MemorySize::new(component_info.layout().size());
320321

321322
let name = component_info.name();
322323
let type_id = component_info.type_id();
@@ -327,15 +328,13 @@ impl WorldInspectionExtensionTrait for World {
327328
None => None,
328329
};
329330

330-
let (value, memory_size) = match type_id {
331+
let value = match type_id {
331332
Some(type_id) => match get_reflected_resource_ref(self, type_id) {
332-
Ok(reflected) => (
333-
reflected_value_to_string(reflected, settings.full_type_names),
334-
Some(MemorySize::new(size_of_val(reflected))),
335-
),
336-
Err(err) => (format!("<Unreflectable: {}>", err), None),
333+
Ok(reflected) => reflected_value_to_string(reflected, settings.full_type_names),
334+
335+
Err(err) => format!("<Unreflectable: {}>", err),
337336
},
338-
None => ("Dynamic Type".to_string(), None),
337+
None => "Dynamic Type".to_string(),
339338
};
340339

341340
Ok(ResourceInspection {

src/gui/panels/object_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn generate_resource_list(world: &mut World) -> Vec<ObjectListEntry> {
207207
ObjectListEntry::Resource {
208208
component_id: inspection.component_id,
209209
display_name: name.to_string(),
210-
memory_size: inspection.memory_size.unwrap_or(MemorySize::new(0)),
210+
memory_size: inspection.memory_size,
211211
}
212212
})
213213
.collect()

src/inspection/component_inspection.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ pub struct ComponentInspection {
4040
serde(with = "crate::serde_conversions::debug_name")
4141
)]
4242
pub name: DebugName,
43-
/// The size, in bytes, of the component value.
43+
/// The shallow size of the component in memory.
4444
///
4545
/// Note that this may differ from the size of the component type
4646
/// if the component is a dynamically-sized type: heap-allocated data is not included.
47-
///
48-
/// Computing this value requires reflection of the component value.
49-
/// As a result, it may be `None` if the component type is not reflected and registered,
50-
/// or if [`ComponentDetailLevel::Names`] was specified when inspecting the component.
51-
pub memory_size: Option<MemorySize>,
47+
pub memory_size: MemorySize,
5248
/// The value of the component as a string.
5349
///
5450
/// This information is gathered via reflection,
@@ -61,14 +57,8 @@ impl Display for ComponentInspection {
6157
let shortname = self.name.shortname();
6258

6359
match &self.value {
64-
Some(value) => match &self.memory_size {
65-
Some(size) => write!(f, "{shortname} ({}): {value}", size)?,
66-
None => write!(f, "{shortname}: {value}")?,
67-
},
68-
None => match &self.memory_size {
69-
Some(size) => write!(f, "{shortname} ({})", size)?,
70-
None => write!(f, "{shortname}")?,
71-
},
60+
Some(value) => write!(f, "{shortname} ({}): {value}", &self.memory_size)?,
61+
None => write!(f, "{shortname} ({})", &self.memory_size)?,
7262
}
7363

7464
Ok(())

src/inspection/resource_inspection.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ pub struct ResourceInspection {
3636
/// Note that dynamic types will not have a [`TypeId`].
3737
#[cfg_attr(feature = "serde", serde(skip))]
3838
pub type_id: Option<TypeId>,
39-
/// The size of the resource in memory.
39+
/// The shallow size of the resource in memory.
4040
///
41-
/// This is computed using [`core::mem::size_of_val`], and requires reflection of the resource value.
42-
pub memory_size: Option<MemorySize>,
41+
/// Note that this may differ from the size of the resource type
42+
/// if it is a dynamically-sized: heap-allocated data is not included.
43+
pub memory_size: MemorySize,
4344
/// The type information of the resource.
4445
///
4546
/// This contains metadata about the resource's type,
@@ -59,10 +60,7 @@ pub struct ResourceInspection {
5960
impl Display for ResourceInspection {
6061
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6162
let short_name = self.name.shortname();
62-
match &self.memory_size {
63-
Some(size) => write!(f, "{} ({}): {}", short_name, size, self.value)?,
64-
None => write!(f, "{}: {}", short_name, self.value)?,
65-
}
63+
write!(f, "{} ({}): {}", short_name, self.memory_size, self.value)?;
6664

6765
Ok(())
6866
}

0 commit comments

Comments
 (0)