Skip to content

Optimize Usages of .ToImmutableArray #1052

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

Merged
merged 5 commits into from
Aug 20, 2022
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
8 changes: 5 additions & 3 deletions src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.Scraper.Annotations;
Expand Down Expand Up @@ -208,15 +209,16 @@ private IEnumerable<Symbol> VisitType(XmlElement type)

private IEnumerable<Symbol> VisitStruct(XmlElement @struct)
{
var fields = new List<FieldSymbol>();
var fields = new FieldSymbol[@struct.ChildNodes.Count];
var i = 0;
foreach (var node in @struct.ChildNodes.Cast<XmlNode>())
{
var symbols = Visit(node);
foreach (var v in symbols)
{
if (v is FieldSymbol fieldSymbol)
{
fields.Add(fieldSymbol);
fields[i++] = fieldSymbol;
}
}
}
Expand All @@ -234,7 +236,7 @@ private IEnumerable<Symbol> VisitStruct(XmlElement @struct)
name,
ImmutableArray<ISymbolAnnotation>.Empty
),
fields.ToImmutableArray(),
ImmutableArray.Create(fields, 0, i),
ImmutableArray.Create<ISymbolAnnotation>(new NativeNameAnnotation(name))
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public void StringTestWithMethods()
(
TypeId.CreateNew(),
new IdentifierSymbol("C", ImmutableArray<ISymbolAnnotation>.Empty),
new MethodSymbol[]
{
method
}.ToImmutableArray(),
ImmutableArray.Create<MethodSymbol>(method),
ImmutableArray<ISymbolAnnotation>.Empty
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public void SingleMemberIntegration()
new NamespaceSymbol
(
new IdentifierSymbol("Test", ImmutableArray<ISymbolAnnotation>.Empty),
new[]
{
(TypeSymbol) new StructSymbol
ImmutableArray.Create<TypeSymbol>
(
new StructSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol("Test2", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<FieldSymbol>.Empty,
ImmutableArray<ISymbolAnnotation>.Empty
)
}.ToImmutableArray(),
),
ImmutableArray<ISymbolAnnotation>.Empty
)
);
Expand All @@ -56,23 +56,23 @@ public void MultipleMembersIntegration()
new NamespaceSymbol
(
new IdentifierSymbol("Test", ImmutableArray<ISymbolAnnotation>.Empty),
new[]
{
(TypeSymbol) new StructSymbol
ImmutableArray.Create<TypeSymbol>
(
new StructSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol("Test2", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<FieldSymbol>.Empty,
ImmutableArray<ISymbolAnnotation>.Empty
),
(TypeSymbol) new StructSymbol
new StructSymbol
(
TypeId.CreateNew(),
new IdentifierSymbol("Test3", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<FieldSymbol>.Empty,
ImmutableArray<ISymbolAnnotation>.Empty
)
}.ToImmutableArray(),
),
ImmutableArray<ISymbolAnnotation>.Empty
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void SingleFieldIntegration()
(
TypeId.CreateNew(),
new IdentifierSymbol("Test", ImmutableArray<ISymbolAnnotation>.Empty),
new[]
{
ImmutableArray.Create
(
new FieldSymbol
(
new ExternalTypeReference
Expand All @@ -37,7 +37,7 @@ public void SingleFieldIntegration()
new IdentifierSymbol("F1", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
)
}.ToImmutableArray(),
),
ImmutableArray<ISymbolAnnotation>.Empty
)
);
Expand All @@ -64,8 +64,8 @@ public void MultipleFieldsIntegration()
(
TypeId.CreateNew(),
new IdentifierSymbol("Test", ImmutableArray<ISymbolAnnotation>.Empty),
new[]
{
ImmutableArray.Create
(
new FieldSymbol
(
new ExternalTypeReference
Expand Down Expand Up @@ -99,7 +99,7 @@ public void MultipleFieldsIntegration()
new IdentifierSymbol("F3", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
)
}.ToImmutableArray(),
),
ImmutableArray<ISymbolAnnotation>.Empty
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void StringTestWithParams()
new IdentifierSymbol("Ret", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
),
new TypeReference[]
{
ImmutableArray.Create<TypeReference>
(
new ExternalTypeReference
(
null,
Expand All @@ -55,8 +55,8 @@ public void StringTestWithParams()
null,
new IdentifierSymbol("Param2", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
),
}.ToImmutableArray(),
)
),
ImmutableArray<ISymbolAnnotation>.Empty
);

Expand Down
41 changes: 26 additions & 15 deletions tests/Silk.NET.SilkTouch.Tests.Common/Fakers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> ExternalTypeReference { get; } =
new Faker<ExternalTypeReference>()
.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> TypeReference { get; } =
new Faker<TypeReference>()
Expand All @@ -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> MethodSymbol { get; } =
new Faker<MethodSymbol>()
Expand All @@ -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> FieldSymbol { get; } =
new Faker<FieldSymbol>()
.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> FunctionPointerTypeReference { get; } =
new Faker<FunctionPointerTypeReference>()
.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> InternalTypeReference { get; } =
new Faker<InternalTypeReference>()
.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> TypeSymbol { get; } =
new Faker<TypeSymbol>()
Expand All @@ -108,29 +108,40 @@ static Fakers()
new Faker<NamespaceSymbol>()
.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> PointerTypeReference { get; } =
new Faker<PointerTypeReference>()
.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> StructSymbol { get; } =
new Faker<StructSymbol>()
.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> UnresolvedTypeReference { get; } =
new Faker<UnresolvedTypeReference>()
.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<T> SkipConstructor<T>(this Faker<T> f) where T : class
=> f.CustomInstantiator(_ => (FormatterServices.GetUninitializedObject(typeof(T)) as T)!);

private static ImmutableArray<T> GenerateImmutableArray<T>(this Faker<T> faker, int min, int max) where T : class
{
var count = ((IFakerTInternal)faker).FakerHub.Random.Number(min, max);
var builder = ImmutableArray.CreateBuilder<T>(count);
for (int i = 0; i < count; i++)
{
builder.Add(faker.Generate());
}
return builder.MoveToImmutable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public void SelfTypeIsResolvedCorrectly()
(
TypeId.CreateNew(),
new IdentifierSymbol("a", ImmutableArray<ISymbolAnnotation>.Empty),
new FieldSymbol[]
{
new
ImmutableArray.Create
(
new FieldSymbol
(
new UnresolvedTypeReference("a", ImmutableArray<ISymbolAnnotation>.Empty),
new IdentifierSymbol("someField", ImmutableArray<ISymbolAnnotation>.Empty),
ImmutableArray<ISymbolAnnotation>.Empty
)
}.ToImmutableArray(),
),
ImmutableArray<ISymbolAnnotation>.Empty
);
var typeStore = new TypeStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ public void RootScopeContainsSingleNamespacedType()
var @namespace = new NamespaceSymbol
(
new IdentifierSymbol("", ImmutableArray<ISymbolAnnotation>.Empty),
new[]
{
(TypeSymbol) testType
}.ToImmutableArray(),
ImmutableArray.Create<TypeSymbol>(testType),
ImmutableArray<ISymbolAnnotation>.Empty
);
var visitor = new TypeScopeSymbolVisitor(new TypeStore());
Expand Down Expand Up @@ -105,12 +102,7 @@ public void RootScopeContainsMultipleNamespacedTypes()
var @namespace = new NamespaceSymbol
(
new IdentifierSymbol("", ImmutableArray<ISymbolAnnotation>.Empty),
new TypeSymbol[]
{
testType1,
testType2,
testType3
}.ToImmutableArray(),
ImmutableArray.Create<TypeSymbol>(testType1, testType2, testType3),
ImmutableArray<ISymbolAnnotation>.Empty
);
var visitor = new TypeScopeSymbolVisitor(new TypeStore());
Expand Down