Remove the `actions` argument from `Model::actions` and return the actions
HaoYang670 opened this issue · comments
In the definition of Module::actions, the argument actions: &mut Vec<Self::Action>
is just used to be pushed the result in. This makes no sense because it should be the caller to decide how to use the returned actions, and it also introduces a mut
reference.
Instead, I'd like the definition of Module::actions
to be
fn actions(&self, state: &Self::State) -> Vec<Self::Action>
, and it could also make the API consistent with next_steps
and next_states
Hi @HaoYang670. Thanks for reaching out about this.
actions
is passed in to avoid an extra heap allocation by reusing the actions buffer across states. I haven't recently benchmarked this, and I suspect the performance difference is minimal in retrospect given that any complex Model
will likely also be doing plenty of its own allocations.
As a next step, I think someone just codes this up and does a before/after benchmark via the bench.sh
script before merging in the change. I'll be focusing on some other things for the the near future, but I would gladly accept a PR if you have interest/cycles.
actions is passed in to avoid an extra heap allocation by reusing the actions buffer across states.
Thank you @jonnadal, this is a good point that I didn't thought before. I should get more about how actions
would be used. Currently, I find
let mut actions1 = Vec::new();
model.actions(&last_state, &mut actions1);
appears may times.