From 1ee11018ab4d8e02c6eaef691554f3b0dc271174 Mon Sep 17 00:00:00 2001 From: Caran <142813963+carl-andersson-at-westermo@users.noreply.github.com> Date: Fri, 24 May 2024 11:39:47 +0000 Subject: [PATCH 1/6] Used old syntax for array creation --- FactoryGenerator/FactoryGenerator.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/FactoryGenerator/FactoryGenerator.cs b/FactoryGenerator/FactoryGenerator.cs index 2658d8a..79d0e5a 100644 --- a/FactoryGenerator/FactoryGenerator.cs +++ b/FactoryGenerator/FactoryGenerator.cs @@ -591,10 +591,11 @@ private static void MakeArray(Dictionary declarations, string na factory = @$" {type}[] {factoryName} {{ - {type}[] source = [{string.Join(", ", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))}]; + List<{type}> source = {{ + {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} }}; {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null) - .Select(i => $"if({i.BooleanInjection!.Key}) source = [..source, {i.Name}];"))} - return source; + .Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} + return source.ToArray(); }}"; } From 7f94218631d538ea4c4cc0606dc10c28b5d61c12 Mon Sep 17 00:00:00 2001 From: Caran <142813963+carl-andersson-at-westermo@users.noreply.github.com> Date: Fri, 24 May 2024 11:43:15 +0000 Subject: [PATCH 2/6] Using --- FactoryGenerator/FactoryGenerator.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/FactoryGenerator/FactoryGenerator.cs b/FactoryGenerator/FactoryGenerator.cs index 79d0e5a..8ad385e 100644 --- a/FactoryGenerator/FactoryGenerator.cs +++ b/FactoryGenerator/FactoryGenerator.cs @@ -145,6 +145,7 @@ private static IEnumerable GenerateCode(ImmutableArray dataIn log.Log(LogLevel.Debug, "Starting Code Generation"); var usingStatements = $@" using System; +using System.Linq; using System.Collections.Generic; using FactoryGenerator; using System.CodeDom.Compiler; @@ -591,8 +592,9 @@ private static void MakeArray(Dictionary declarations, string na factory = @$" {type}[] {factoryName} {{ - List<{type}> source = {{ - {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} }}; + List<{type}> source = new List<{type> {{ + {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} + }}; {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null) .Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} return source.ToArray(); From d8c5535955204fcfe6b7b4937a9af1e79d5e0970 Mon Sep 17 00:00:00 2001 From: Caran <142813963+carl-andersson-at-westermo@users.noreply.github.com> Date: Fri, 24 May 2024 11:46:39 +0000 Subject: [PATCH 3/6] Microfixes --- FactoryGenerator/FactoryGenerator.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/FactoryGenerator/FactoryGenerator.cs b/FactoryGenerator/FactoryGenerator.cs index 8ad385e..5319635 100644 --- a/FactoryGenerator/FactoryGenerator.cs +++ b/FactoryGenerator/FactoryGenerator.cs @@ -584,7 +584,7 @@ public partial class {className} private static void MakeArray(Dictionary declarations, string name, INamedTypeSymbol type, Dictionary> interfaceInjectors, bool function = false) { - var factoryName = "[]"; + var factoryName = $"new {type}[0]"; var factory = string.Empty; if (interfaceInjectors.TryGetValue(type, out var injections)) { @@ -595,8 +595,7 @@ private static void MakeArray(Dictionary declarations, string na List<{type}> source = new List<{type> {{ {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} }}; - {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null) - .Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} + {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null).Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} return source.ToArray(); }}"; } From 841516d8d851e5cb120f253b7d310d6e20481b0b Mon Sep 17 00:00:00 2001 From: caran Date: Fri, 24 May 2024 13:52:14 +0200 Subject: [PATCH 4/6] } fix --- FactoryGenerator/FactoryGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FactoryGenerator/FactoryGenerator.cs b/FactoryGenerator/FactoryGenerator.cs index 5319635..dfcdaba 100644 --- a/FactoryGenerator/FactoryGenerator.cs +++ b/FactoryGenerator/FactoryGenerator.cs @@ -592,7 +592,7 @@ private static void MakeArray(Dictionary declarations, string na factory = @$" {type}[] {factoryName} {{ - List<{type}> source = new List<{type> {{ + List<{type}> source = new List<{type}> {{ {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} }}; {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null).Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} From 2f6f5855795e564b50fda8e7c58ac02e45dd9be6 Mon Sep 17 00:00:00 2001 From: caran Date: Fri, 24 May 2024 13:54:06 +0200 Subject: [PATCH 5/6] Added array benchmark --- Benchmarking/Benchmarks/Program.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Benchmarking/Benchmarks/Program.cs b/Benchmarking/Benchmarks/Program.cs index 01cb85f..58f18a7 100644 --- a/Benchmarking/Benchmarks/Program.cs +++ b/Benchmarking/Benchmarks/Program.cs @@ -25,6 +25,9 @@ public class ResolveBenchmarks [Benchmark] public IOverridable ResolveTransient() => m_container.Resolve(); + [Benchmark] + public IEnumerable ResolveArray() => m_container.Resolve>(); + [Benchmark] public IContainer Create() => new DependencyInjectionContainer(default, default, default!); } From c257aa01e6dcb1547184e4201acf1c9e8ebdcd15 Mon Sep 17 00:00:00 2001 From: caran Date: Fri, 24 May 2024 14:10:16 +0200 Subject: [PATCH 6/6] Benchmark & Creation fixes --- Benchmarking/Benchmarks/Program.cs | 3 ++- FactoryGenerator/FactoryGenerator.cs | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Benchmarking/Benchmarks/Program.cs b/Benchmarking/Benchmarks/Program.cs index 58f18a7..3c32307 100644 --- a/Benchmarking/Benchmarks/Program.cs +++ b/Benchmarking/Benchmarks/Program.cs @@ -1,4 +1,5 @@ using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Engines; using BenchmarkDotNet.Running; using FactoryGenerator; using Inherited; @@ -26,7 +27,7 @@ public class ResolveBenchmarks public IOverridable ResolveTransient() => m_container.Resolve(); [Benchmark] - public IEnumerable ResolveArray() => m_container.Resolve>(); + public List ResolveArray() => (List) m_container.Resolve>(); [Benchmark] public IContainer Create() => new DependencyInjectionContainer(default, default, default!); diff --git a/FactoryGenerator/FactoryGenerator.cs b/FactoryGenerator/FactoryGenerator.cs index dfcdaba..9117c6c 100644 --- a/FactoryGenerator/FactoryGenerator.cs +++ b/FactoryGenerator/FactoryGenerator.cs @@ -590,20 +590,20 @@ private static void MakeArray(Dictionary declarations, string na { factoryName = $"Create{name}()".Replace("_", ""); factory = @$" - {type}[] {factoryName} + IEnumerable<{type}> {factoryName} {{ List<{type}> source = new List<{type}> {{ {string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))} }}; {string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null).Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))} - return source.ToArray(); + return source; }}"; } if (function) { declarations[name] = $@" - internal {type}[] {name}() + internal IEnumerable<{type}> {name}() {{ if (m_{name} != null) return m_{name}; @@ -615,12 +615,12 @@ private static void MakeArray(Dictionary declarations, string na return m_{name} = {factoryName}; }} }} - internal {type}[]? m_{name};" + factory; + internal IEnumerable<{type}>? m_{name};" + factory; } else { declarations[name] = $@" - internal {type}[] {name} + internal IEnumerable<{type}> {name} {{ get {{ @@ -635,7 +635,7 @@ private static void MakeArray(Dictionary declarations, string na }} }} }} - internal {type}[]? m_{name};" + factory; + internal IEnumerable<{type}>? m_{name};" + factory; } }