I see many vector libraries take an argument as the destination to the result of something like dot product, cross product, etc... The destination is allocated and written to.
I see glm always returning a value instead which seems like it can be more expensive. I always think, is there some kind of compiler optimization or other stuff behind the scenes that makes the whole thing efficient after all and isn't something for me to worry about?
As an example of what I'm talking about:
[source lang="cpp"]//passing the result as a destinationvec3 result;dot(vecA, vecB, result);//returning the resultglm::vec3 result = glm::dot(vecA, vecB);[/source]
4 replies to this topic
Sponsor:
#2 Members - Reputation: 841
Posted 09 November 2012 - 06:21 AM
- You are right, this is not something you should worry about, unless you have profiled your program and identified it as a performance bottleneck. Plus chances are that if it will be the bottleneck then simply passing destination as function argument will not help you.
- Yes, modern compilers should optimize out such assignments in release (optimized) mode.
Lauris Kaplinski
First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
#3 Members - Reputation: 3828
Posted 09 November 2012 - 07:02 AM
Yeah, generally this kind of thing falls into the realms of micro-optimization. You'd have to have a pretty extreme use-case for it to even register on any performance graph, so it's something that you really don't need to worry about.
It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.
#4 Members - Reputation: 616
Posted 09 November 2012 - 11:10 AM
I believe the relevant optimization technique here is Return value optimization.
Edited by kloffy, 09 November 2012 - 11:11 AM.






