-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
The dynamic
example is scary and needs a revamp
#11459
Comments
Your right that the memory allocation is scary, and it does seem to create a leak as the I do agree that having all of the dynamic ECS constructs in one example is a little overwhelming and makes it hard to parse for new users who probably only need 1 or 2 parts of it, so we should probably split it up or at the very least document it more thoroughly showing alternatives for simpler use cases. I do think there will be some people who want to do everything that the example is doing and it is non-trivial to figure out how to use bevy's dynamic APIs so I think all parts of it should still exist somewhere. The main reason I made the example the way it is was to prove it was possible to have the components, queries and entities "fully" defined dynamically, but it's non-ergonomic and I think it also points to some areas where we could improve the I also agree that we should show how to convert from |
Yeah! In any case, I think you did great work, and seriously, even a non-pedagogically-optimal example shouldn't be a blocker to merge such an important feature as the one you implemented. I want to say how much I'm glad you implemented dynamic queries. I personally need them a lot, and the rest of the community will be delighted, as direct users, and as users of the future functionalities built on top of dynamic queries. Thank you again! I think it makes sense that the rest of the community (me included) cleans up behind. I wanted to direct attention to things that can be improved so that new users could use more effectively your work. |
# Objective - Fix a memory leak in the dynamic ECS example mentioned in #11459 ## Solution - Rather than allocate the memory manually instead store a collection of `Vec` that will be dropped after it is used. --- I must have misinterpreted `OwningPtr`s semantics when initially writing this example. I believe we should be able to provide better APIs here for inserting dynamic components that don't require the user to wrangle so much unsafety. We have no other examples using `insert_by_ids` and our only tests show it being used for 1 or 2 values with nested calls to `OwningPtr::make` despite the function taking an iterator. Rust's type system is quite restrictive here but we could at least let `OwningPtr::new` take non u8 `NonNull`. I also agree with #11459 that we should generally be trying to simplify and clarify this example.
The example in question:
https://github.com/bevyengine/bevy/blob/259fb6896e07bed89df2637aace547f295fc7e82/examples/ecs/dynamic.rs
Does far more than presenting how to use the dynamic query API. Notably it:
std::alloc
andNonNull
to copy values from aVec
and point aOwningPtr
to it.To illustrate a dynamic querying API, I think (2) and (4) are sufficient. (1) should have a separate example. (3) could illustrate separately the
bevy_ptr
crate as well.This might seem harmless, but in reality, it causes real problems: it confuses users. Here is an example: #11424
Other problems with the example
bevy/examples/ecs/dynamic.rs
Lines 123 to 128 in 259fb68
There is direct calls to
std::alloc
functions, a lot of manual pointer fiddling (cast
,copy_from
,NonNull::new
), Even a memory leak! (thedata
ptr is allocated without ever being freed) I think this kind of code has nothing to do in a bevy example, not only it isn't representative of general bevy usage, but it might scare off potential users. Especially given that supposedly people chose a rust game engine because they don't have to remember to free their dynamic arrays!Proposed changes
In actuality, the currently exposed API do still require
OwningPtr
(thebevy_ptr
type) at least for insertion but I think the best example would show how to convert between thePtr
returned by dynamic query iteration into a&dyn Reflect
. This has already been a question (@coreh) and I suspect it's going to be super useful to a lot of users.Knowing how to convert into a
&dyn Reflect
aPtrMut
is immediately more useful than knowing how to copy from aVec<u64>
into a custom component. It also has more applications, especially for someone who is interested into using the dynamic query API.The text was updated successfully, but these errors were encountered: