Is there a benefit in caching a transform's rotation?

I’ve read that caching a GameObject’s transform helps with optimization:

public Transform _transform;

public void Start()
{
_transform = gameObject.transform;
}

But, is it also efficient to cache the transform’s rotation?

public Transform _transform;
public Quaternion _q;

public void Start()
{
_transform = gameObject.transform;
_q = gameObject.transform.rotation;
}

Thanks.

not really, since the rotation is going to change. What you’ve done is store the initial rotation of the object.

The point about caching the transform is that it points to a single, non changeable entity which you then don’t have to look up every time you want to interact with it.

2 Likes

You no longer have to cache the Transform component, Unity does it for you automatically.

Unity doesn’t cache the Transform component. Using the transform shortcut is faster than using GetComponent, but not as fast as caching it. As for the question, no, that’s bad practice unless you need to store the initial rotation for some reason, in which case you should use a descriptive variable name such as “initialStoredRotation”.

–Eric

3 Likes

Ok. But what do you mean by “Using the transform shortcut”?

So basically in the above code, using:

Quaternion q = _transform.rotation;

Would be an optimized version of:

Quaternion q = gameObject.transform.rotation;

?

No, basically a cached version like “myTransform = transform;” then doing “myTransform.rotation = someVector;” is faster than just using “transform.rotation = someVector;”, which that one would be faster than “GetComponent().rotation = someVector;”.

Those are three separate ways to do the same thing differently :smile:

1 Like

The blog says it is cached as of Unity 5: http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

“[2] in Unity5 we also cache the transform component on the c# side, so there should no longer be a performance reason to cache the transform component yourself.”

3 Likes

This is exactly why usually code optimizations are a WASTE OF TIME.
Another optimization which goes around the forums involves eliminating empty LateUpdate() and FixedUpdate() functions. This appears to not be worth the effort also. I should probably focus on other areas instead now.
Thanks.

The answer to this is you can’t cache the rotation. Rotation is a Quaternion. Quaternions are value types. Thus the rotation won’t ever Update.

Google reference versus value type for more details.

1 Like

I know about that, but unfortunately it’s not true. Perhaps they intended to do that, but it didn’t happen in the released version. It’s trivial to test this for yourself…don’t just take someone’s word for it, not even Unity’s.

You should definitely remove empty *Update functions. If they exist, they have overhead (involving reflection) even if there’s nothing in them, plus they make your code ugly. It’s hardly “effort” to remove them.

–Eric

3 Likes

Oh I hadn’t noticed that (the page saying it is true - or the fact that it is wrong, haha) but interesting findings there.

Why would you have empty FixedUpdate or LateUpdate functions in your code?

1 Like

Did I say I had empty fixedUpdate or lateUpdate functions?

Not you specifically, why would anyone? You said that removing empty methods is not a code improvement. Regardless of weather it is or not, why would anyone have empty functions in their code?

Your all wrong. You should totally spend the effort to write in empty FixedUpdate and LateUpdate functions… :eyes:

2 Likes

Better yet, make them recursive:

void FixedUpdate()
{
    //1. Write recursive function
    FixedUpdate();
}

//2. ?????

//3:
void Profit() {}
3 Likes

Take the following for example:
when i started unity a few months ago I had the following in each of my scripts:

currentUpdateType  = UPDATE;
public void Update()
{
    if (currentUpdateType  == UPDATE)
         PerformUpdate();
}
pulic void FixedUpdate()
{
    if (currentUpdateType  == FIXED_UPDATE;
         PerformUpdate();
}

public void PerformUpdate()
{
//..do whatever needs to be done
}

This allowed my to just change the currentUpdate variable based on what type of update I wanted to use.
sometimes it was needed to change to a different update type during runtime.
For example, i have a player moving in Update in certain scenes. but then in other scenes, the player is moved in FixedUpdate instead because it is being moved externally by some physics force. Then it can switch back to Update.

But, could not someone consider one of these functions to be “empty” at any given time? If currentUpdate = UPDATE, then technically FIxedUpdate is “empty”. And vice-versa. This is why I wondered if its really worth the “effort” to change something like this just for optimization.

(I was using this same template for every class).
Then i went and deleted all the above updates in every class if they werent actually needed, but did not really notice any performance increase in the profiler.
I can see if someone had hundreds of these, it might impact performance, but i cant image 10 scripts would have that big of an impact.

if (! itIsBroken())
{
   dontFixIt();
}

As far as the caching transform, I went ahead now and did that, just to get the most possible optimizations.

Technically it’s not empty at all…an empty function has no code in it. Testing for a condition and then returning if false is not empty by any definition; you’re doing valid work.

–Eric

2 Likes