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

Started by
5 comments, last by frob 8 years, 7 months ago

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.

Advertisement

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.

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.

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!

Are you really sure your vector class is the cause of your performance bottleneck?

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

This topic is closed to new replies.

Advertisement