0
\$\begingroup\$

I'm just going through a quick C# / Unity crash course (coming from Java) and I recieved a warning about "Repeated property access of a built-in component is inefficient" while working in Jetbrains Rider for the following code snippet.

if (Mathf.Abs(horizontalMovement) > Mathf.Epsilon) {
        horizontalMovement += horizontalMovement * Time.deltaTime;
        var position = transform.position;
        horizontalMovement += position.x;

        position = new Vector2(horizontalMovement, position.y);
        //warning is for this line
        transform.position = position;
    }

It was already complaining about using transform.position.x and transform.position.y which seemed fair enough so I created the position var but would declaring var cachedTransform = transform; ...; cachedTransform.position = position really be more performant here or is the overhead of creating the local variable just going to negate it?

I'm not asking if this is a valid warning in general, it seems to be. I'm asking if it's valid when you're literally just reassigning in the same statement as I am in the last line of my code above

For reference here's what I would end up with if I followed the warning -

 if (Mathf.Abs(horizontalMovement) > Mathf.Epsilon) {
            horizontalMovement += horizontalMovement * Time.deltaTime;
            var cachedTransform = transform;
            var position = cachedTransform.position;
            horizontalMovement += position.x;

            position = new Vector2(horizontalMovement, position.y);
            cachedTransform.position = position;
        }
\$\endgroup\$
  • 1
    \$\begingroup\$ Whenever there's question of "slow" code, profile it. I was told something was slow on Pro Feel Golf. I profiled it and found out that on 500,000,000 executions of the "slow" thing, we lost 0.5 seconds. Ordinarily this thing wouldn't be called more than 10-100 times. So yes, it was slower, but it was irrelevant. \$\endgroup\$ – Almo Aug 12 at 13:56
3
\$\begingroup\$

Sinse you are new to C#, make sure you read up on properties if you haven't already done so. Because over-simplified, they are basically special methods that look like a variable to the end user.

Due to them actually being methods, the C# compiler can't know for sure what you are doing to retrieve the value. So that warning might be over-zealous yes, but that depends on the actual property in question. So if you create a property that does a lot of stuff and is very slow as a result, then you get a huge benefit by caching it. If your property is basically just returning a backed field without any checks, then there is little to no benefit caching the property.

On whether the transform property in your question is fast/slow and therefore needs to be cached, see this forum post (I advise you to read the whole thing, or just test it yourself to be sure).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.