Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 040bf59

Browse files
committed
Reduce allocations in MeshOutlineGenerator
1 parent fc533cc commit 040bf59

File tree

5 files changed

+31
-46
lines changed

5 files changed

+31
-46
lines changed

Source/.editorconfig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ csharp_style_prefer_extended_property_pattern = true:suggestion
212212
csharp_style_prefer_top_level_statements = false:silent
213213
dotnet_diagnostic.CA1309.severity = error
214214
dotnet_diagnostic.CA1311.severity = suggestion
215-
dotnet_diagnostic.SYSLIB1054.severity = warning
215+
dotnet_diagnostic.SYSLIB1054.severity = warning
216+
csharp_style_prefer_primary_constructors = false:silent
217+
csharp_style_prefer_utf8_string_literals = true:suggestion
218+
csharp_style_prefer_readonly_struct = true:suggestion
219+
csharp_style_prefer_readonly_struct_member = true:suggestion
220+
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent
221+
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
216222

217223
[*.{cs,vb}]
218224
dotnet_style_coalesce_expression = true:suggestion
@@ -248,4 +254,5 @@ dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
248254
dotnet_style_qualification_for_field = false:silent
249255
dotnet_style_qualification_for_property = false:silent
250256
dotnet_style_qualification_for_method = false:silent
251-
dotnet_style_qualification_for_event = false:silent
257+
dotnet_style_qualification_for_event = false:silent
258+
dotnet_style_prefer_collection_expression = true:suggestion

Source/AssetRipper.Numerics/VectorExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ namespace AssetRipper.Numerics
44
{
55
public static class VectorExtensions
66
{
7+
public static Vector2 AsVector2(this Vector3 vector3)
8+
{
9+
return Unsafe.As<Vector3, Vector2>(ref vector3);
10+
}
11+
712
public static Vector3 AsVector3(this Vector2 vector2)
813
{
914
return new Vector3(vector2, 0);

Source/AssetRipper.Processing/Textures/SpriteProcessor.ObjectFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using AssetRipper.Assets.Bundles;
2-
using AssetRipper.Assets.Collections;
1+
using AssetRipper.Assets.Collections;
32
using AssetRipper.SourceGenerated.Classes.ClassID_28;
43

54
namespace AssetRipper.Processing.Textures;

Source/AssetRipper.Processing/Textures/SpriteProcessor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ private static void ProcessSprite(ISprite sprite)
107107
m_RD.SecondaryTextures.Clear();
108108
foreach (SecondarySpriteTexture spt in spriteData.SecondaryTextures)
109109
{
110-
SecondarySpriteTexture newSpt = m_RD.SecondaryTextures.AddNew();
111-
newSpt.Name = spt.Name;
112-
newSpt.Texture.CopyValues(spt.Texture, converter);
110+
m_RD.SecondaryTextures.AddNew().CopyValues(spt, converter);
113111
}
114112
}
115113
}

Source/AssetRipper.SourceGenerated.Extensions/MeshOutlineGenerator.cs

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,9 @@ namespace AssetRipper.SourceGenerated.Extensions
55
{
66
public sealed class MeshOutlineGenerator
77
{
8-
private sealed class Outline
8+
private readonly struct Outline
99
{
10-
private readonly struct Outside
11-
{
12-
public Outside(int triIndex, int vertex)
13-
{
14-
Triangle = triIndex;
15-
Member = vertex;
16-
}
17-
18-
public override string ToString()
19-
{
20-
return $"{Triangle}:{Member}";
21-
}
22-
23-
public int Triangle { get; }
24-
public int Member { get; }
25-
}
10+
private readonly record struct Outside(int Triangle, int Member);
2611

2712
public Outline(IReadOnlyList<Vector3i> triangles, int startTriangle)
2813
{
@@ -34,14 +19,14 @@ public Outline(IReadOnlyList<Vector3i> triangles, int startTriangle)
3419

3520
public void GenerateOutline()
3621
{
37-
List<int> outline = new();
22+
m_outline.Clear();
3823
Outside outsider = m_outsiders[0];
3924
Vector3i tri = m_triangles[outsider.Triangle];
4025
int first = tri.GetValueByMember(outsider.Member);
4126
int second = tri.GetValueByMember(outsider.Member + 1);
4227
int startTriIndex = outsider.Triangle;
43-
outline.Add(first);
44-
outline.Add(second);
28+
m_outline.Add(first);
29+
m_outline.Add(second);
4530

4631
Vector3i lastTri = tri;
4732
int lastMember = outsider.Member + 1;
@@ -74,10 +59,8 @@ public void GenerateOutline()
7459
break;
7560
}
7661

77-
outline.Add(nextVertex);
62+
m_outline.Add(nextVertex);
7863
}
79-
80-
GeneratedOutline = outline;
8164
}
8265

8366
public bool IsContain(int triIndex)
@@ -291,23 +274,18 @@ private bool IsConnectedNeighbors(Vector3i tri1, Vector3i tri2, int vertex)
291274
}
292275

293276
public int TriangleCount => m_indexes.Count;
294-
public IReadOnlyList<int> GeneratedOutline { get; private set; } = Array.Empty<int>();
277+
public IReadOnlyList<int> GeneratedOutline => m_outline;
295278

296279
private readonly IReadOnlyList<Vector3i> m_triangles;
297280
private readonly HashSet<int> m_indexes = new();
281+
private readonly List<int> m_outline = new();
298282
private readonly List<Outside> m_outsiders = new();
299283
}
300284

301285
public MeshOutlineGenerator(IReadOnlyList<Vector3> vertices, IReadOnlyList<Vector3i> triangles)
302286
{
303-
if (vertices == null)
304-
{
305-
throw new ArgumentNullException(nameof(vertices));
306-
}
307-
if (triangles == null)
308-
{
309-
throw new ArgumentNullException(nameof(triangles));
310-
}
287+
ArgumentNullException.ThrowIfNull(vertices);
288+
ArgumentNullException.ThrowIfNull(triangles);
311289
m_vertices = vertices;
312290
foreach (Vector3i triangle in triangles)
313291
{
@@ -320,7 +298,7 @@ public MeshOutlineGenerator(IReadOnlyList<Vector3> vertices, IReadOnlyList<Vecto
320298

321299
public List<Vector2[]> GenerateOutlines()
322300
{
323-
List<Outline> outlines = new List<Outline>();
301+
List<Outline> outlines = new();
324302
for (int i = 0; i < m_triangles.Count; i++)
325303
{
326304
bool isKnown = false;
@@ -362,32 +340,30 @@ public List<Vector2[]> GenerateOutlines()
362340
for (int l = index; l < nextOutline.GeneratedOutline.Count; l++)
363341
{
364342
int nextVertex = nextOutline.GeneratedOutline[l];
365-
resultLine.Add((Vector2f)m_vertices[nextVertex]);
343+
resultLine.Add(m_vertices[nextVertex].AsVector2());
366344
}
367345
for (int m = 0; m < index; m++)
368346
{
369347
int nextVertex = nextOutline.GeneratedOutline[m];
370-
resultLine.Add((Vector2f)m_vertices[nextVertex]);
348+
resultLine.Add(m_vertices[nextVertex].AsVector2());
371349
}
372350
outlines.RemoveAt(k--);
373351
}
374352
}*/
375-
resultLine.Add(ConvertToVector2f(m_vertices[vertex]));
353+
resultLine.Add(m_vertices[vertex].AsVector2());
376354
}
377355
result.Add(resultLine.ToArray());
378356
}
379357

380358
return result;
381359
}
382360

383-
private static Vector2 ConvertToVector2f(Vector3 v3) => new Vector2(v3.X, v3.Y);
384-
385361
private static bool IsValidTriangle(Vector3i triangle)
386362
{
387363
return triangle.X != triangle.Y && triangle.X != triangle.Z && triangle.Y != triangle.Z;
388364
}
389365

390366
private readonly IReadOnlyList<Vector3> m_vertices;
391-
private readonly List<Vector3i> m_triangles = new List<Vector3i>();
367+
private readonly List<Vector3i> m_triangles = new();
392368
}
393369
}

0 commit comments

Comments
 (0)