Implement SystemParam for SmallVec#24405
Conversation
|
Looks like CI failure is unrelated. |
kfc35
left a comment
There was a problem hiding this comment.
Looks good to me, follows the regular Vec impl except for one thing: the unsafe impl for ParamSet<'_, '_, SmallVec<[T; N]>>. Is that also necessary to implement? I assume since it works for you, it doesn’t need to be done but just figured I’d ask the question (for my learning)
I personally don't need it, just implemented it for consistency (since |
chescock
left a comment
There was a problem hiding this comment.
Oh, yay, I'm happy to see SystemParamBuilder is getting use!
But it would be nice to avoid allocations when the number of system parameters is low.
Hmm, I wonder whether we could even make a Vec-like parameter that avoids per-run allocations entirely by re-using a buffer between runs.
|
|
||
| // SAFETY: Registers access for each element of `state`. | ||
| // If any one conflicts, it will panic. | ||
| unsafe impl<T: SystemParam, const N: usize> SystemParam for SmallVec<[T; N]> { |
There was a problem hiding this comment.
I think you also need an impl<...> SystemParamBuilder<SmallVec<[P; N]>> for SmallVec<[B; N]> in builder.rs so that you can actually configure the systems.
There was a problem hiding this comment.
Ah, right, I didn't test the builder yet 😅 Added!
| } | ||
| } | ||
|
|
||
| impl<T: SystemParam, const N: usize> ParamSet<'_, '_, SmallVec<[T; N]>> { |
There was a problem hiding this comment.
It looks like you copied the impl block for ParamSet<Vec<_>>, but not the impl SystemParam, so there's no way to actually obtain a ParamSet<SmallVec>.
I don't think you need a SmallVec version of ParamSet, though. ParamSet<Vec> never actually allocates a Vec, and just creates the subparameters on-demand in get_mut, so there is no allocation to save. So I'd be inclined to delete this impl block.
There was a problem hiding this comment.
Makes sense, removed!
This was my original goal, but I couldn't find where to store the Vec, so I went with SmallVec. If you have any idea, I'm happy to try. |
I did a few experiments, but without much success. The "buffer reuse" technique from Wild Performance Tricks seems like it should work, but if you try to put a Then I thought that a I think I might have gotten it working using raw pointers! You can use |
Would be interesting to see! |
Objective
To register a system from a script, I need to support for dynamic number of system parameters (since scripting languages usually don't play well with generics). Right now the only solution is to use
Vec<T>as a system param which is allocated every time the system runs. But it would be nice to avoid allocations when the number of system parameters is low.Solution
Implement
SystemParamforSmallVec. It's already a dependency forbevy_ecsand allows to avoid allocations with user-defined number of system parameters.Testing
SmallVec<[Local<usize>; 8]>.Vecimpl.