Skip to content

Commit f01f9ba

Browse files
committed
don't allocate ConsoleAppContext if no needed
1 parent 4ab2d5f commit f01f9ba

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
using ConsoleAppFramework;
22

3-
var builder = ConsoleApp.Create();
4-
builder.Add<Commands>();
5-
await builder.RunAsync(args);
63

7-
public class Commands
4+
ConsoleApp.Run(args, (int x, int y, ConsoleAppContext context) =>
85
{
9-
[Hidden]
10-
public void Command1() { Console.Write("command1"); }
11-
12-
public void Command2() { Console.Write("command2"); }
13-
14-
[Hidden]
15-
public void Command3(int x, [Hidden] int y) { Console.Write($"command3: x={x} y={y}"); }
16-
}
6+
});

src/ConsoleAppFramework/ConsoleAppBaseCode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ public void Dispose()
487487
internal partial class ConsoleAppBuilder
488488
{
489489
FuncGlobalOptionsBuilderObject? configureGlobalOptions;
490+
bool isRequireCallBuildAndSetServiceProvider = false;
490491
491492
public ConsoleAppBuilder()
492493
{
@@ -554,6 +555,7 @@ static bool TryShowHelpOrVersion(ReadOnlySpan<string> args, int requiredParamete
554555
public ConsoleAppBuilder ConfigureGlobalOptions(FuncGlobalOptionsBuilderObject configure)
555556
{
556557
this.configureGlobalOptions = configure;
558+
this.isRequireCallBuildAndSetServiceProvider = true;
557559
return this;
558560
}
559561

src/ConsoleAppFramework/Emitter.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,27 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
7272
{
7373
using (command.HasFilter ? sb.Nop : sb.BeginBlock("try"))
7474
{
75-
// prepare commandArgs
75+
// prepare commandArgsMemory
7676
if (command.HasFilter)
7777
{
7878
sb.AppendLine("var commandArgsMemory = context.InternalCommandArgs;"); // already prepared and craeted ConsoleAppContext
7979
}
8080
else
8181
{
82-
sb.AppendLine("var escapeIndex = args.AsSpan().IndexOf(\"--\");");
83-
if (!emitForBuilder)
82+
if (hasConsoleAppContext || emitForBuilder)
8483
{
85-
sb.AppendLine("var commandDepth = 0;");
86-
}
84+
sb.AppendLine("var escapeIndex = args.AsSpan().IndexOf(\"--\");");
85+
if (!emitForBuilder)
86+
{
87+
sb.AppendLine("var commandDepth = 0;");
88+
}
8789

88-
sb.AppendLine("ReadOnlyMemory<string> commandArgsMemory = (escapeIndex == -1) ? args.AsMemory(commandDepth) : args.AsMemory(commandDepth, escapeIndex - commandDepth);");
90+
sb.AppendLine("ReadOnlyMemory<string> commandArgsMemory = (escapeIndex == -1) ? args.AsMemory(commandDepth) : args.AsMemory(commandDepth, escapeIndex - commandDepth);");
91+
}
92+
else
93+
{
94+
sb.AppendLine("ReadOnlyMemory<string> commandArgsMemory = args;");
95+
}
8996
}
9097

9198
sb.AppendLine($"if (TryShowHelpOrVersion(commandArgsMemory.Span, {requiredParsableParameterCount}, {commandWithId.Id})) return;");
@@ -104,18 +111,21 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
104111
if (emitForBuilder)
105112
{
106113
sb.AppendLine("ConsoleAppContext context;");
107-
using (sb.BeginBlock("if (configureGlobalOptions == null)"))
114+
using (hasConsoleAppContext ? sb.Nop : sb.BeginBlock("if (isRequireCallBuildAndSetServiceProvider)"))
108115
{
109-
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, commandArgsMemory, null, null, commandDepth, escapeIndex);");
110-
}
111-
using (sb.BeginBlock("else"))
112-
{
113-
sb.AppendLine("var builder = new GlobalOptionsBuilder(commandArgsMemory);");
114-
sb.AppendLine("var globalOptions = configureGlobalOptions(ref builder);");
115-
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, builder.RemainingArgs, null, globalOptions, commandDepth, escapeIndex);");
116-
sb.AppendLine("commandArgsMemory = builder.RemainingArgs;");
116+
using (sb.BeginBlock("if (configureGlobalOptions == null)"))
117+
{
118+
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, commandArgsMemory, null, null, commandDepth, escapeIndex);");
119+
}
120+
using (sb.BeginBlock("else"))
121+
{
122+
sb.AppendLine("var builder = new GlobalOptionsBuilder(commandArgsMemory);");
123+
sb.AppendLine("var globalOptions = configureGlobalOptions(ref builder);");
124+
sb.AppendLine($"context = new ConsoleAppContext(\"{command.Name}\", args, builder.RemainingArgs, null, globalOptions, commandDepth, escapeIndex);");
125+
sb.AppendLine("commandArgsMemory = builder.RemainingArgs;");
126+
}
127+
sb.AppendLine("BuildAndSetServiceProvider(context);");
117128
}
118-
sb.AppendLine("BuildAndSetServiceProvider(context);");
119129

120130
if (dllReference != null && dllReference.Value.HasHost)
121131
{
@@ -790,6 +800,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
790800
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)"))
791801
{
792802
sb.AppendLine("this.configureServices = (_, _, services) => configure(services);");
803+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
793804
sb.AppendLine("return this;");
794805
}
795806

@@ -799,6 +810,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
799810
// for backward-compatiblity, we chooce (IConfiguration, IServiceCollection) for two arguments overload
800811
sb.AppendLine("this.requireConfiguration = true;");
801812
sb.AppendLine("this.configureServices = (_, configuration, services) => configure(configuration, services);");
813+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
802814
sb.AppendLine("return this;");
803815
}
804816

@@ -807,6 +819,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
807819
{
808820
sb.AppendLine("this.requireConfiguration = true;");
809821
sb.AppendLine("this.configureServices = configure;");
822+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
810823
sb.AppendLine("return this;");
811824
}
812825
}
@@ -816,13 +829,15 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
816829
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)"))
817830
{
818831
sb.AppendLine("this.configureServices = (_, services) => configure(services);");
832+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
819833
sb.AppendLine("return this;");
820834
}
821835

822836
sb.AppendLine();
823837
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<ConsoleAppContext, IServiceCollection> configure)"))
824838
{
825839
sb.AppendLine("this.configureServices = configure;");
840+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
826841
sb.AppendLine("return this;");
827842
}
828843
}
@@ -850,6 +865,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
850865
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)"))
851866
{
852867
sb.AppendLine("this.configureLogging = (_, _, logging) => configure(logging);");
868+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
853869
sb.AppendLine("return this;");
854870
}
855871

@@ -858,6 +874,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
858874
{
859875
sb.AppendLine("this.requireConfiguration = true;");
860876
sb.AppendLine("this.configureLogging = (_, configuration, logging) => configure(configuration, logging);");
877+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
861878
sb.AppendLine("return this;");
862879
}
863880

@@ -866,6 +883,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
866883
{
867884
sb.AppendLine("this.requireConfiguration = true;");
868885
sb.AppendLine("this.configureLogging = configure;");
886+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
869887
sb.AppendLine("return this;");
870888
}
871889
}
@@ -875,13 +893,15 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
875893
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)"))
876894
{
877895
sb.AppendLine("this.configureLogging = (_, logging) => configure(logging);");
896+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
878897
sb.AppendLine("return this;");
879898
}
880899

881900
sb.AppendLine();
882901
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ConsoleAppContext, ILoggingBuilder> configure)"))
883902
{
884903
sb.AppendLine("this.configureLogging = configure;");
904+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
885905
sb.AppendLine("return this;");
886906
}
887907
}

0 commit comments

Comments
 (0)