From 82455defd6e9b403d692f1df65353907f2fdfe6a Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Sat, 20 Aug 2022 19:17:28 +0200 Subject: [PATCH 1/3] Optimize Usages of .ToImmutableArray --- .../Silk.NET.SilkTouch.Scraper/XmlVisitor.cs | 8 ++-- .../ClassSymbolTests.cs | 5 +-- .../EmitterNamespaceMemberTests.cs | 18 ++++---- .../EmitterStructMemberFieldsTests.cs | 12 +++--- .../FunctionPointerTypeReferenceTests.cs | 8 ++-- .../Silk.NET.SilkTouch.Tests.Common/Fakers.cs | 41 ++++++++++++------- .../NameResolverSymbolVisitorTests.cs | 8 ++-- .../TypeScopeSymbolVisitorTests.cs | 12 +----- 8 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs b/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs index ee5b6cf64a..e67ec8b616 100644 --- a/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs +++ b/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs @@ -7,6 +7,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection.Metadata; +using System.Runtime.InteropServices; using System.Xml; using Microsoft.Extensions.Logging; using Silk.NET.SilkTouch.Symbols; @@ -203,7 +204,8 @@ private IEnumerable VisitType(XmlElement type) private IEnumerable VisitStruct(XmlElement @struct) { - var fields = new List(); + var fields = new FieldSymbol[@struct.ChildNodes.Count]; + var i = 0; foreach (var node in @struct.ChildNodes.Cast()) { var symbols = Visit(node); @@ -211,7 +213,7 @@ private IEnumerable VisitStruct(XmlElement @struct) { if (v is FieldSymbol fieldSymbol) { - fields.Add(fieldSymbol); + fields[i++] = fieldSymbol; } } } @@ -228,7 +230,7 @@ private IEnumerable VisitStruct(XmlElement @struct) @struct.Attributes?["name"]?.Value ?? throw new InvalidOperationException(), ImmutableArray.Empty ), - fields.ToImmutableArray(), + ImmutableArray.Create(fields, 0, i), ImmutableArray.Empty ) ) diff --git a/tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs b/tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs index 0b623e155f..1bafa434ef 100644 --- a/tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs +++ b/tests/Silk.NET.SilkTouch.Emitter.Tests/ClassSymbolTests.cs @@ -44,10 +44,7 @@ public void StringTestWithMethods() ( TypeId.CreateNew(), new IdentifierSymbol("C", ImmutableArray.Empty), - new MethodSymbol[] - { - method - }.ToImmutableArray(), + ImmutableArray.Create(method), ImmutableArray.Empty ); diff --git a/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceMemberTests.cs b/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceMemberTests.cs index 7621a8ada0..34523f0b62 100644 --- a/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceMemberTests.cs +++ b/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceMemberTests.cs @@ -22,16 +22,16 @@ public void SingleMemberIntegration() new NamespaceSymbol ( new IdentifierSymbol("Test", ImmutableArray.Empty), - new[] - { - (TypeSymbol) new StructSymbol + ImmutableArray.Create + ( + new StructSymbol ( TypeId.CreateNew(), new IdentifierSymbol("Test2", ImmutableArray.Empty), ImmutableArray.Empty, ImmutableArray.Empty ) - }.ToImmutableArray(), + ), ImmutableArray.Empty ) ); @@ -56,23 +56,23 @@ public void MultipleMembersIntegration() new NamespaceSymbol ( new IdentifierSymbol("Test", ImmutableArray.Empty), - new[] - { - (TypeSymbol) new StructSymbol + ImmutableArray.Create + ( + new StructSymbol ( TypeId.CreateNew(), new IdentifierSymbol("Test2", ImmutableArray.Empty), ImmutableArray.Empty, ImmutableArray.Empty ), - (TypeSymbol) new StructSymbol + new StructSymbol ( TypeId.CreateNew(), new IdentifierSymbol("Test3", ImmutableArray.Empty), ImmutableArray.Empty, ImmutableArray.Empty ) - }.ToImmutableArray(), + ), ImmutableArray.Empty ) ); diff --git a/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructMemberFieldsTests.cs b/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructMemberFieldsTests.cs index 1019c51fc6..df619a514f 100644 --- a/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructMemberFieldsTests.cs +++ b/tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructMemberFieldsTests.cs @@ -24,8 +24,8 @@ public void SingleFieldIntegration() ( TypeId.CreateNew(), new IdentifierSymbol("Test", ImmutableArray.Empty), - new[] - { + ImmutableArray.Create + ( new FieldSymbol ( new ExternalTypeReference @@ -37,7 +37,7 @@ public void SingleFieldIntegration() new IdentifierSymbol("F1", ImmutableArray.Empty), ImmutableArray.Empty ) - }.ToImmutableArray(), + ), ImmutableArray.Empty ) ); @@ -64,8 +64,8 @@ public void MultipleFieldsIntegration() ( TypeId.CreateNew(), new IdentifierSymbol("Test", ImmutableArray.Empty), - new[] - { + ImmutableArray.Create + ( new FieldSymbol ( new ExternalTypeReference @@ -99,7 +99,7 @@ public void MultipleFieldsIntegration() new IdentifierSymbol("F3", ImmutableArray.Empty), ImmutableArray.Empty ) - }.ToImmutableArray(), + ), ImmutableArray.Empty ) ); diff --git a/tests/Silk.NET.SilkTouch.Emitter.Tests/FunctionPointerTypeReferenceTests.cs b/tests/Silk.NET.SilkTouch.Emitter.Tests/FunctionPointerTypeReferenceTests.cs index df03752fba..59146ff2d1 100644 --- a/tests/Silk.NET.SilkTouch.Emitter.Tests/FunctionPointerTypeReferenceTests.cs +++ b/tests/Silk.NET.SilkTouch.Emitter.Tests/FunctionPointerTypeReferenceTests.cs @@ -42,8 +42,8 @@ public void StringTestWithParams() new IdentifierSymbol("Ret", ImmutableArray.Empty), ImmutableArray.Empty ), - new TypeReference[] - { + ImmutableArray.Create + ( new ExternalTypeReference ( null, @@ -55,8 +55,8 @@ public void StringTestWithParams() null, new IdentifierSymbol("Param2", ImmutableArray.Empty), ImmutableArray.Empty - ), - }.ToImmutableArray(), + ) + ), ImmutableArray.Empty ); diff --git a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs index ec862fd64b..5fc10c1e4f 100644 --- a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs +++ b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs @@ -30,14 +30,14 @@ static Fakers() x => x.Value, f => f.Random.String2(1, StandardGenerateCount, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_") ) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker ExternalTypeReference { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.Namespace, f => IdentifierSymbol.Generate().OrNull(f, 0.1f)) .RuleFor(x => x.TypeIdentifier, f => IdentifierSymbol.Generate()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker TypeReference { get; } = new Faker() @@ -58,7 +58,7 @@ static Fakers() .Select(v => new Parameter(v.First, v.Second)) .ToImmutableArray(); }) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker MethodSymbol { get; } = new Faker() @@ -73,28 +73,28 @@ static Fakers() .SkipConstructor() .RuleFor(x => x.Id, f => TypeId.From(f.Random.Guid())) .RuleFor(x => x.Identifier, f => IdentifierSymbol.Generate()) - .RuleFor(x => x.Methods, f => MethodSymbol.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Methods, f => MethodSymbol.GenerateImmutableArray(0, StandardGenerateCount)) + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker FieldSymbol { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.Identifier, f => IdentifierSymbol.Generate()) .RuleFor(x => x.Type, f => TypeReference.Generate()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker FunctionPointerTypeReference { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.ReturnType, f => TypeReference.Generate()) - .RuleFor(x => x.ParameterTypes, f => TypeReference.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.ParameterTypes, f => TypeReference.GenerateImmutableArray(0, StandardGenerateCount)) + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker InternalTypeReference { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.ReferencedTypeId, f => TypeId.From(f.Random.Guid())) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker TypeSymbol { get; } = new Faker() @@ -108,29 +108,40 @@ static Fakers() new Faker() .SkipConstructor() .RuleFor(x => x.Identifier, f => IdentifierSymbol.Generate()) - .RuleFor(x => x.Types, f => TypeSymbol.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Types, f => TypeSymbol.GenerateImmutableArray(0, StandardGenerateCount)) + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker PointerTypeReference { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.Underlying, f => TypeReference.Generate()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker StructSymbol { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.Id, f => TypeId.From(f.Random.Guid())) .RuleFor(x => x.Identifier, f => IdentifierSymbol.Generate()) - .RuleFor(x => x.Fields, f => FieldSymbol.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Fields, f => FieldSymbol.GenerateImmutableArray(0, StandardGenerateCount)) + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); public static Faker UnresolvedTypeReference { get; } = new Faker() .SkipConstructor() .RuleFor(x => x.Text, f => f.Random.String(0, StandardGenerateCount)) - .RuleFor(x => x.Annotations, () => Annotation.GenerateBetween(0, StandardGenerateCount).ToImmutableArray()); + .RuleFor(x => x.Annotations, () => Annotation.GenerateImmutableArray(0, StandardGenerateCount)); private static Faker SkipConstructor(this Faker f) where T : class => f.CustomInstantiator(_ => (FormatterServices.GetUninitializedObject(typeof(T)) as T)!); + + private static ImmutableArray GenerateImmutableArray(this Faker faker, int min, int max) where T : class + { + var count = ((IFakerTInternal)faker).FakerHub.Random.Number(min, max); + var builder = ImmutableArray.CreateBuilder(count); + for (int i = 0; i < max; i++) + { + builder.Add(faker.Generate()); + } + return builder.MoveToImmutable(); + } } diff --git a/tests/Silk.NET.SilkTouch.TypeResolution.Tests/NameResolverSymbolVisitorTests.cs b/tests/Silk.NET.SilkTouch.TypeResolution.Tests/NameResolverSymbolVisitorTests.cs index 27834e80bb..adcb76586d 100644 --- a/tests/Silk.NET.SilkTouch.TypeResolution.Tests/NameResolverSymbolVisitorTests.cs +++ b/tests/Silk.NET.SilkTouch.TypeResolution.Tests/NameResolverSymbolVisitorTests.cs @@ -21,15 +21,15 @@ public void SelfTypeIsResolvedCorrectly() ( TypeId.CreateNew(), new IdentifierSymbol("a", ImmutableArray.Empty), - new FieldSymbol[] - { - new + ImmutableArray.Create + ( + new FieldSymbol ( new UnresolvedTypeReference("a", ImmutableArray.Empty), new IdentifierSymbol("someField", ImmutableArray.Empty), ImmutableArray.Empty ) - }.ToImmutableArray(), + ), ImmutableArray.Empty ); var typeStore = new TypeStore(); diff --git a/tests/Silk.NET.SilkTouch.TypeResolution.Tests/TypeScopeSymbolVisitorTests.cs b/tests/Silk.NET.SilkTouch.TypeResolution.Tests/TypeScopeSymbolVisitorTests.cs index c510c35838..b61507cca7 100644 --- a/tests/Silk.NET.SilkTouch.TypeResolution.Tests/TypeScopeSymbolVisitorTests.cs +++ b/tests/Silk.NET.SilkTouch.TypeResolution.Tests/TypeScopeSymbolVisitorTests.cs @@ -61,10 +61,7 @@ public void RootScopeContainsSingleNamespacedType() var @namespace = new NamespaceSymbol ( new IdentifierSymbol("", ImmutableArray.Empty), - new[] - { - (TypeSymbol) testType - }.ToImmutableArray(), + ImmutableArray.Create(testType), ImmutableArray.Empty ); var visitor = new TypeScopeSymbolVisitor(new TypeStore()); @@ -105,12 +102,7 @@ public void RootScopeContainsMultipleNamespacedTypes() var @namespace = new NamespaceSymbol ( new IdentifierSymbol("", ImmutableArray.Empty), - new TypeSymbol[] - { - testType1, - testType2, - testType3 - }.ToImmutableArray(), + ImmutableArray.Create(testType1, testType2, testType3), ImmutableArray.Empty ); var visitor = new TypeScopeSymbolVisitor(new TypeStore()); From dedd60125629e8f160e07359a13306dd38f0e2af Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Sat, 20 Aug 2022 19:22:31 +0200 Subject: [PATCH 2/3] Off by one error --- tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs index 5fc10c1e4f..cb1a65cb8b 100644 --- a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs +++ b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs @@ -138,7 +138,7 @@ private static ImmutableArray GenerateImmutableArray(this Faker faker, { var count = ((IFakerTInternal)faker).FakerHub.Random.Number(min, max); var builder = ImmutableArray.CreateBuilder(count); - for (int i = 0; i < max; i++) + for (int i = 0; i <= max; i++) { builder.Add(faker.Generate()); } From 657de5fa2775db34e8f328797fb507bd312a22ff Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Sat, 20 Aug 2022 19:26:14 +0200 Subject: [PATCH 3/3] More fixing --- tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs index cb1a65cb8b..4a39d942be 100644 --- a/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs +++ b/tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs @@ -138,7 +138,7 @@ private static ImmutableArray GenerateImmutableArray(this Faker faker, { var count = ((IFakerTInternal)faker).FakerHub.Random.Number(min, max); var builder = ImmutableArray.CreateBuilder(count); - for (int i = 0; i <= max; i++) + for (int i = 0; i < count; i++) { builder.Add(faker.Generate()); }