kube
kube copied to clipboard
Resource unnecessarily clones
The Meta helper trait has the following signatures,
fn name(&self) -> String
fn namespace(&self) -> Option<String>
fn resource_ver(&self) -> Option<String>
All of these end up cloning the underlying thing they're returning; none of them need to; they could return &str references.
In my particular case, I'm iterating through every object of a particular resource type, this results in quite a few allocations that I end up never needing. (I'm just comparing the name, for example.)
This would be a breaking change, however, since kube-rs isn't at 1.0 yet, I thought I'd suggest it.
I did originally start out with references, but it made it hard to work with the big openapi struct because of borrows happening in many places, and a couple of extra copies didn't feel that worth optimizing for. Besides, you can always just reach into metadata directly if you are concerned about the cost of using Meta.
That said, you can try changing the trait and seeing how the examples expand. If it's not as bad as I originally felt it was, then we could possibly change it.
The experiments for this in https://github.com/clux/kube-rs/pull/311 did leave a lot to be desired in terms of usability (which is primarily why it still clones like it does today).
Resurrecting this discussion now as the trait is being proposed to be split into a more foundational one + a user-facing one in #486.
This type of split potentially opens up the door to having an alternative trait that does not clone as heavily (one that users can even define themselves, as it's not tied to how kube_runtime or kube uses it). Maybe if there's demand for such a trait, we could even have it in here as an alternate ResourceExt.