Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change StorageMemoryUsage type from struct to enum #219

Merged
merged 11 commits into from
Mar 6, 2025

Conversation

notinmybackyaard
Copy link
Contributor

@leudz
As mentioned earlier, I attempted to convert the StorageMemoryUsage type into a trait. The first approach involved defining an Out type for each Storage type that implements the StorageMemoryUsage trait, allowing the memory_usage function to return an Option<Box> type. The second approach was to add StorageMemoryUsage as an associated trait to the Storage trait. The first approach felt overly verbose, and the second required too extensive code changes, so I opted for a simpler solution by handling it as an enum to enable more detailed information during debugging. If there are any suggested modifications, please let me know.

notinmybackyaard added a commit to Npixel-Eclipse/shipyard that referenced this pull request Feb 26, 2025
@leudz
Copy link
Owner

leudz commented Feb 27, 2025

Thanks for putting this together!
Sadly an enum cannot work. Users are free to add other storages that could be any type ('static + Storage is the minimum bound). This is done with the CustomStorageAccess trait.
Sorry.


What do you think of something like this?

pub trait MemoryUsage {
    type Out;

    fn detailed_memory_usage(&self) -> Self::Out;
}

SparseSet and Entities could implement this trait. Maybe in the future World and AllStorages too. For Unique it would have to be implemented on the views but I wouldn't bother (at least for now) since its size is fixed.
StorageMemoryUsage wouldn't change.
MemoryUsage would be public so users can call detailed_memory_usage directly.

Does this make sense? Do you think it would also be too verbose?

@notinmybackyaard
Copy link
Contributor Author

Sorry for the late confirmation.
I didn’t consider CustomStorage, but I understand now.
It seems like implementing something like detailed_memory_usage separately would be a better and good idea.
I’ll get started on it right away. Thanks

@notinmybackyaard
Copy link
Contributor Author

I implemented it based on the ideas you provided. I also thought about the type names and field names, but if you have any better naming suggestions, please revise them. I'm not sure which parts of Entities should be subdivided to help with debugging, so I left it as is.

Here’s an example output of the newly added SparseSet’s detailed_memory_usage:

image

@@ -1437,4 +1504,129 @@ mod tests {

assert_eq!(memory_usage.used_memory_bytes, expected_total_memory);
}

/// Verifies that the `detailed_memory_usage` method accurately reflects the current `SparseSet` data breakdown.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing a test!

@leudz
Copy link
Owner

leudz commented Mar 6, 2025

Thanks for the update!

Sorry for the late confirmation.

No worries, there's no rush.

I also thought about the type names and field names, but if you have any better naming suggestions

I don't have a better idea right now. To understand this information, people need some understanding of SparseSet and in that case they'll know what the fields are. Maybe in the future I'll add an explanation in the fields' doc.

I'm not sure which parts of Entities should be subdivided to help with debugging, so I left it as is.

Now that I look at it yeah I don't know what more there is to add.

@leudz leudz merged commit 1d98572 into leudz:master Mar 6, 2025
13 checks passed
@notinmybackyaard notinmybackyaard deleted the memory-usage-enum branch March 7, 2025 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants