BorisTheBrave wrote:Sounds like you are best of doing sub-expression elimination manually.
Maybe, although I think there are some cases where it may make sense to always cache: Vec2 Dot Vec2, Vec2 Cross Vec2. In both cases, if they are used as a sub-expression in a context where a vector is being scalarized, I know they will be computed twice (for X and Y). There may be some other cases, like (V2-V1).unit. I'll think about somehow generalizing this.
But yeah, I think some manual optimization of complex expressions will be required anyway in the end. It would be nice to give the user an idea of how things will be optimized, but I'm not sure how to do that. Maybe I could emit compile warnings for cases that are likely to be sub-optimal...
BorisTheBrave wrote:Next you should try it on all of Box2D, to see how much difference it can make in total - it's not more effort for the manual process right?
Yep. It may take time though -- I've surely created some bugs that haven't come out yet and Matrices are not fully supported yet (only Mat22 * Vec2 and Mat22 transpose* Vec2).