-
Notifications
You must be signed in to change notification settings - Fork 77
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
Avoid array copy when querying Length for mesh.vertices #52
Comments
Hi, thank you for submitting this. That's a fantastic idea for a rule. |
> `mesh.vertices` returns a copy of _all_ vertices. If you have code like `mesh.vertices.Length`, this will be quite inefficient. It's much better to use `mesh.vertexCount` Source: microsoft/Microsoft.Unity.Analyzers#52
I think this should also apply to While there isn't a separate count property for colors, it's my understanding that the array will (should?) always be same length as the If that is the case, using If it is not the case, then at least a warning about the array copy incurred by accessing Edit: Also |
Automatically adding "help wanted" tag for stale issues identified as good community contribution opportunities |
I think I have implemented this. Is the repository still alive? I'll submit a PR if this is. |
Sure, feel free to submit a PR. Thanks! |
mesh.vertices
returns a copy of all vertices. If you have code likemesh.vertices.Length
, this will be quite inefficient. It's much better to usemesh.vertexCount
or cachemesh.vertices
if you plan to use data from vertices array later.I have seen this at least one time in real code where there was code similar to this:
There is also a much worse offender which look a bit similar but is perhaps another issue:
while correct code should be something like this:
Unity Mesh API reference : https://docs.unity3d.com/ScriptReference/Mesh.html
I don't really know how to create an analyzer for this yet. I want to be sure that this is something useful and in the scope of this project first!
The text was updated successfully, but these errors were encountered: