const btScalar& constructions

mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

const btScalar& constructions

Post by mreuvers »

Hi there,

In the code I noticed a lot of btScalar const references. Since btScalar is nothing more than a define to a built-in type (in this case float). The problem with const references in combination with these types, is that the resulting assembly can contains useless loads and stores.

Consider this example:

Code: Select all

btVector3::btVector3(const btScalar& p_x, const btScalar& p_y, const btScalar& p_z)
{
	m_floats[0] = p_x;
	m_floats[1] = p_y;
	m_floats[2] = p_z;
	m_floats[3] = btScalar(0.);
}
This produces the following PPC assembly:

Code: Select all

lfs      fp3,0(r4)
lfs      fp2,0(r5)
lfs      fp1,0(r6)
lfs      fp0,-30496(r2)
stfs     fp3,0(r3)
stfs     fp2,4(r3)
stfs     fp1,8(r3)
stfs     fp0,12(r3)
blr
When removing the const references, the following assembly is generated:

Code: Select all

lfs      fp0,-30496(r2)
stfs     fp1,0(r3)
stfs     fp2,4(r3)
stfs     fp3,8(r3)
stfs     fp0,12(r3)
blr
As you can see, this simple example already saves me 3 expensive loads, simply because no pointer is used.

When inlining is used, the compiler probably produces almost the same code in both cases. However as we already noticed in our Wii game, the compiler often fails to inline if the code is too complex. And this unfortunately occurs frequently in the bullet code.

Anyhow, perhaps it is a good idea to remove all those const references from at least all speed critical code (e.g., btVector3)

Cheers
Last edited by mreuvers on Mon Dec 22, 2008 1:28 pm, edited 1 time in total.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: const btScalar& constructions

Post by Erwin Coumans »

However as we already noticed in our Wii game, the compiler often fails to inline if the code is too complex.
Have you already reported those issues to the compiler team?

We can consider to work-around those compiler issues, applying your suggested fix for btVector3 constructor. Do you have any other improvements that produce some measurable improvement? Do you have some performance comparison between passing by reference and by value for btVector3, for a physics intensive simulation?
Thanks,
Erwin
mreuvers
Posts: 69
Joined: Sat May 10, 2008 8:39 am

Re: const btScalar& constructions

Post by mreuvers »

I've reported this behavior to the compiler team as well, and am awaiting their reply.

Meanwhile I'll try to figure out exactly how much of an impact this const reference vs by value has.

Happy holidays ;-)