I've got several different objects of varying sizes/complexities which all implement the same trait. My desired use case is to be able to parallel map over each object (as efficiently as possible) multiple times within a continuously running loop.
The options seem to be either an indirect Vec<Box<dyn Trait>>
or to make a single enum
to get the trait implemented for a compile-time Sized
object which would allow going to [TraitEnum; N]
.
The problem with the enum
option is that each struct
is based on a generic type (like a typenum) which sets the size of types to use for its fields from Vector3
/Matrix3
up to Vector6
/Matrix6
. That alone only gives a size ratio of 2:1 to 4:1 (6/3 to 36/9). But on top of that, the different struct
s also have significantly varying sizes, with the largest likely being about 8x the smallest. Thus the final enum
size ratio could be up to ~30:1, which if I recall correctly is well above what Clippy recommends.
The two options aren't drastically difficult to implement, so I may just end up making both and conditionally compiling two versions or allowing runtime selection for benchmarking. But, I'm leaning towards dropping the enum
option entirely. Does anyone have any thoughts on the performance of the Vec<Box<dyn Trait>>
option? The enum
feels like a relatively small optimization with a bad resulting memory footprint.