Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache expensive calls #22

Open
jbevain opened this issue Feb 12, 2020 · 2 comments
Open

Cache expensive calls #22

jbevain opened this issue Feb 12, 2020 · 2 comments
Assignees
Labels
approved rule Indicates if the new proposed rule has been approved to move to implementation phase

Comments

@jbevain
Copy link
Member

jbevain commented Feb 12, 2020

It's generally recommended to cache the result of expensive calls like Camera.main and GetComponent inside the Start message and avoid calling them in Update/FixedUpdate.

Proposed solution

We should detect those calls, offer to cache the result and replace the invocations by the cached field.

@AnBucyk AnBucyk added the approved rule Indicates if the new proposed rule has been approved to move to implementation phase label Feb 13, 2020
@SilentSin
Copy link
Contributor

I would recommend putting the initialisation in Awake because it runs immediately when that script instance is created instead of Start which only runs before its first update.

Also, if it is possible to offer multiple different fix suggestions then it might be useful to have one for lazy initialisation rather than moving it to Awake:

private Rigidbody _Rigidbody;

private void Update()
{
    if (_Rigidbody == null)
        _Rigidbody = GetComponent<Rigidbody>();
}

@AnBucyk AnBucyk self-assigned this Feb 27, 2020
@jbevain
Copy link
Member Author

jbevain commented Mar 6, 2020

@SilentSin it's definitely possible to offer multiple suggestions for one diagnostic! I agree about using Awake instead of Start

@AnBucyk AnBucyk mentioned this issue Mar 11, 2020
12 tasks
@jbevain jbevain assigned sailro and unassigned AnBucyk Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved rule Indicates if the new proposed rule has been approved to move to implementation phase
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants