Sign in to follow this  

Is it worth to implement expression templates for vector2, 3 and 4 classes?

This topic is 825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it worth to implement expression templates for classes representing two or three dimensional vector? Expression templates improve performance, however I'm not sure if it is reasonable to implement such functionality when dealing with such simple class.

Share this post


Link to post
Share on other sites

My first goal is always readability and maintainability. Performance is important too, but in the sense that you avoid doing useless work, and make sure things scale well.

In my view, this is where you win the battle.

 

Adding templates for performance maybe valid, but I'd delay that until I have verified it is a performance problem.

Share this post


Link to post
Share on other sites

Expression templates improve performance

Funny, I was going to the say the other way. Templates increase compilation times, and as for runtime, they can get in the way in very subtle ways to perform certain optimizations due to their generic nature.

If you're going to implement Vector2-3-4 as templates, "higher performance" wouldn't be an argument I'd use.

Share this post


Link to post
Share on other sites

Expression templates aren't the easiest of things to implement and IMO are probably best left out of your own projects unless you can really justify the need for them. You'll spend a disproportionate amount of time implementing the tricky templating involved and the performance benefits are probably not significant.

Really all expression templates do is to remove temporaries from your equations. In all likelihood you'll be hard-pressed to find a situation where expression templates were *the* optimisation for job. In the very rare case where they might have helped you can always manually expand the expression at the site of the problem - overall you'll still be winning on time and effort involved.

Are you using SSE optimisations too? I would guess that SSE vectorisation will yield a bigger bang-for-buck for your dev time spent optimisation your linear algebra.

Personally I would just use a 3rd-party lib to do all this (e.g. Eigen) and move onto developing more interesting things!

Edited by dmatter

Share this post


Link to post
Share on other sites

To be honest I have hardly ever seen a templated vector math library in a game anyway, all of the ones that I have seen are SIMD based and optimised for the platform, Like Sony version of this which comes in AOS and SOA versions and are implemented in SSE, AltiVec or normal instructions, DirectX Math is another form of this.

If you look at the code for these you will see that they use lots of ifdefs to check which SIMD lib is available and if none is they fallback to an array of a particular size and implement the code in normal non-templated code. Bullet includes the sony vectormath lib which is the one I have see used so far.

Share this post


Link to post
Share on other sites

Expression templates might happen to simply some occasionally-useful operations, but I don't think they'll buy you anything significant. They can do a little for delayed evaluation, but you can usually find smart patterns for any actual bottleneck. 

 

Expression templates are a tool in your tool-belt but not one to reach for often.

 

 

 

As NightCreature mentioned, if you're looking for performance in this arena that's not where you want to go.  Parallel operations, rather than delayed operations, are the typical performance booster.

 

There was a recent GDC talk that had a fairly good introduction to getting performance with this type of thing.  If you're looking for high performance, a bunch of Vector2/3/4 classes are not the right approach generally.  

Share this post


Link to post
Share on other sites

This topic is 825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this