Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/UNT0042.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Accessing array properties like `vertices`, `normals`, `tangents`, `uv`, `colors`, etc. on `Mesh` inside a loop causes a new array allocation on each access. This can significantly impact performance, especially in frequently called methods like `Update()`.

Only reading these properties allocates. Assigning them (for example `mesh.vertices = vertices;`) does not allocate and is not reported. Accesses on a mesh that changes with each iteration (for example a `foreach` variable) are not reported either, as they cannot be cached before the loop.

## Examples of patterns that are flagged by this analyzer

```csharp
Expand Down
273 changes: 273 additions & 0 deletions src/Microsoft.Unity.Analyzers.Tests/MeshArrayPropertyInLoopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,279 @@ void Update()
// loop end
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
}

[Fact]
public async Task SetterInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = new Vector3[0];
Color32[] colors = new Color32[0];
for (int i = 0; i < 10; i++)
{
mesh.vertices = vertices;
(mesh.colors32) = colors;
}
}
}
";

// Setters are not allocating
await VerifyCSharpDiagnosticAsync(test);
}

[Fact]
public async Task SetterWithDeconstructionInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = new Vector3[0];
Vector3[] normals = new Vector3[0];
for (int i = 0; i < 10; i++)
{
(mesh.vertices, mesh.normals) = (vertices, normals);
}
}
}
";

await VerifyCSharpDiagnosticAsync(test);
}

[Fact]
public async Task SetterBeforeGetterInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = new Vector3[0];
for (int i = 0; i < 10; i++)
{
mesh.vertices = vertices;
var v = mesh.vertices[i];
}
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(13, 21)
.WithArguments("vertices");

await VerifyCSharpDiagnosticAsync(test, diagnostic);
}

[Fact]
public async Task GetterAndSetterInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
for (int i = 0; i < mesh.vertexCount; i++)
{
var vertices = mesh.vertices;
vertices[i] += Vector3.up;
mesh.vertices = vertices;
}
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(11, 28)
.WithArguments("vertices");

await VerifyCSharpDiagnosticAsync(test, diagnostic);

const string fixedTest = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] meshVertices = mesh.vertices;
for (int i = 0; i < mesh.vertexCount; i++)
{
var vertices = meshVertices;
vertices[i] += Vector3.up;
mesh.vertices = vertices;
}
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
}

[Fact]
public async Task ReceiverDeclaredInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
foreach (var filter in GetComponentsInChildren<MeshFilter>())
{
var normals = filter.mesh.normals;
Mesh mesh = filter.mesh;
var vertices = mesh.vertices;
}
}
}
";

// A different mesh is used on each iteration, so it cannot be cached before the loop
await VerifyCSharpDiagnosticAsync(test);
}

[Fact]
public async Task ReceiverAssignedInLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh[] meshes = new Mesh[0];
Mesh mesh = null;
for (int i = 0; i < meshes.Length; i++)
{
mesh = meshes[i];
var vertices = mesh.vertices;
}
}
}
";

await VerifyCSharpDiagnosticAsync(test);
}

[Fact]
public async Task ReceiverDeclaredInOuterLoop()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
foreach (var filter in GetComponentsInChildren<MeshFilter>())
{
Mesh mesh = filter.mesh;
for (int i = 0; i < mesh.vertices.Length; i++)
{
// some work
}
}
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(11, 33)
.WithArguments("vertices");

await VerifyCSharpDiagnosticAsync(test, diagnostic);

const string fixedTest = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
foreach (var filter in GetComponentsInChildren<MeshFilter>())
{
Mesh mesh = filter.mesh;
Vector3[] meshVertices = mesh.vertices;
for (int i = 0; i < meshVertices.Length; i++)
{
// some work
}
}
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
}

[Fact]
public async Task ReceiverUsingLoopVariable()
{
const string test = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh[] meshes = new Mesh[0];
for (int i = 0; i < meshes.Length; i++)
{
var current = meshes[i].vertices;
var first = meshes[0].vertices;
}
}
}
";

var diagnostic = ExpectDiagnostic()
.WithLocation(12, 25)
.WithArguments("vertices");

await VerifyCSharpDiagnosticAsync(test, diagnostic);

const string fixedTest = @"
using UnityEngine;

class Camera : MonoBehaviour
{
void Update()
{
Mesh[] meshes = new Mesh[0];
Vector3[] meshVertices = meshes[0].vertices;
for (int i = 0; i < meshes.Length; i++)
{
var current = meshes[i].vertices;
var first = meshVertices;
}
}
}
";

await VerifyCSharpFixAsync(test, fixedTest);
Expand Down
Loading
Loading