Skip to content

Lazy subcommand registration - #2895

Closed
adamsitnik wants to merge 3 commits into
dotnet:mainfrom
adamsitnik:lazy-symbols
Closed

adamsitnik wants to merge 3 commits into
dotnet:mainfrom
adamsitnik:lazy-symbols

Conversation

@adamsitnik

@adamsitnik adamsitnik commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Contributes to #2894, based on the idea from #2894 (comment).

Command.Subcommands now returns a new public CommandList type (still implements IList<Command>) that allows registering subcommands lazily:

public void AddLazy(string name, Func<Command> factory);
public void AddLazy(string name, string[] aliases, Func<Command> factory);

The parser registers only the names and aliases of lazy subcommands and creates just the subcommands that were actually matched, so unused parts of a large symbol tree are never allocated. Public members of the list (indexer, enumeration etc.) create lazy subcommands on demand, so existing code (help, completions) keeps working.

Options and arguments are unchanged.

Benchmarks

Perf_LazySubcommands creates the same symbol tree using eager (Subcommands.Add) and lazy (Subcommands.AddLazy) registration, and optionally parses it.


BenchmarkDotNet v0.14.0, Windows 11 (10.0.26200.9911)
Unknown processor
.NET SDK 11.0.100-rc.3.26522.148
  [Host]     : .NET 10.0.15 (10.0.1526.53008), X64 RyuJIT AVX2
  DefaultJob : .NET 10.0.15 (10.0.1526.53008), X64 RyuJIT AVX2


Method Top Nested Option Mean Error StdDev Median Gen0 Gen1 Allocated
Create_Eager 3 2 3 4,063.6 ns 82.07 ns 236.79 ns 4,004.6 ns 2.5177 0.1717 20.59 KB
Create_Lazy 3 2 3 498.6 ns 8.70 ns 10.69 ns 498.2 ns 0.1664 0.0005 1.36 KB
CreateAndParse_Eager 3 2 3 7,629.6 ns 284.06 ns 837.56 ns 7,560.5 ns 3.3188 0.2365 27.16 KB
CreateAndParse_Lazy 3 2 3 4,992.4 ns 153.72 ns 453.24 ns 5,014.6 ns 1.5144 0.0420 12.39 KB
Create_Eager 3 2 10 13,298.3 ns 473.47 ns 1,396.02 ns 13,454.3 ns 6.8054 1.0223 55.67 KB
Create_Lazy 3 2 10 556.8 ns 17.89 ns 52.75 ns 562.7 ns 0.1664 0.0005 1.36 KB
CreateAndParse_Eager 3 2 10 18,232.3 ns 569.45 ns 1,679.03 ns 18,504.0 ns 8.0109 1.3275 65.46 KB
CreateAndParse_Lazy 3 2 10 8,889.7 ns 299.69 ns 883.63 ns 9,277.0 ns 2.8610 0.1755 23.41 KB
Create_Eager 3 5 3 10,492.9 ns 205.47 ns 380.85 ns 10,400.1 ns 4.8752 0.6027 39.83 KB
Create_Lazy 3 5 3 461.9 ns 8.79 ns 8.22 ns 461.8 ns 0.1664 0.0005 1.36 KB
CreateAndParse_Eager 3 5 3 10,261.4 ns 196.96 ns 202.27 ns 10,287.9 ns 5.6915 0.7172 46.52 KB
CreateAndParse_Lazy 3 5 3 4,231.9 ns 79.19 ns 61.83 ns 4,242.7 ns 1.5564 0.0381 12.73 KB
Create_Eager 3 5 10 21,085.5 ns 416.85 ns 803.13 ns 21,128.8 ns 13.4583 3.4180 110 KB
Create_Lazy 3 5 10 457.4 ns 5.61 ns 4.97 ns 457.7 ns 0.1664 0.0005 1.36 KB
CreateAndParse_Eager 3 5 10 24,399.3 ns 484.13 ns 1,317.11 ns 23,717.0 ns 14.6790 4.0894 119.91 KB
CreateAndParse_Lazy 3 5 10 6,817.1 ns 74.37 ns 69.57 ns 6,831.8 ns 2.9068 0.1602 23.75 KB
Create_Eager 10 2 3 12,042.3 ns 239.68 ns 426.02 ns 12,087.3 ns 8.0719 1.5106 65.94 KB
Create_Lazy 10 2 3 946.9 ns 18.35 ns 18.02 ns 946.2 ns 0.2346 0.0010 1.92 KB
CreateAndParse_Eager 10 2 3 22,076.0 ns 434.71 ns 714.24 ns 22,292.3 ns 9.0332 1.7395 73.84 KB
CreateAndParse_Lazy 10 2 3 6,154.2 ns 126.32 ns 372.47 ns 6,305.7 ns 1.7471 0.0534 14.29 KB
Create_Eager 10 2 10 42,902.1 ns 2,609.63 ns 7,694.55 ns 45,608.6 ns 22.3389 7.5073 182.89 KB
Create_Lazy 10 2 10 599.5 ns 9.75 ns 8.65 ns 598.1 ns 0.2346 0.0010 1.92 KB
CreateAndParse_Eager 10 2 10 39,194.2 ns 501.19 ns 418.51 ns 39,075.3 ns 23.7427 9.4604 194.02 KB
CreateAndParse_Lazy 10 2 10 6,679.7 ns 132.68 ns 274.02 ns 6,549.2 ns 3.0975 0.1755 25.3 KB
Create_Eager 10 5 3 23,533.3 ns 280.87 ns 248.98 ns 23,429.9 ns 15.8997 4.9133 130.08 KB
Create_Lazy 10 5 3 618.7 ns 9.32 ns 8.72 ns 618.3 ns 0.2346 0.0010 1.92 KB
CreateAndParse_Eager 10 5 3 25,479.6 ns 376.11 ns 293.64 ns 25,458.2 ns 16.9067 4.9133 138.1 KB
CreateAndParse_Lazy 10 5 3 4,570.6 ns 114.17 ns 325.73 ns 4,423.4 ns 1.7853 0.0534 14.63 KB
Create_Eager 10 5 10 77,925.9 ns 1,331.42 ns 1,039.49 ns 78,035.5 ns 44.5557 24.7803 363.98 KB
Create_Lazy 10 5 10 626.4 ns 12.43 ns 11.63 ns 624.6 ns 0.2346 0.0010 1.92 KB
CreateAndParse_Eager 10 5 10 78,293.5 ns 1,143.07 ns 954.51 ns 78,277.9 ns 45.8984 3.6621 375.23 KB
CreateAndParse_Lazy 10 5 10 6,788.4 ns 108.60 ns 90.69 ns 6,769.4 ns 3.1357 0.1831 25.65 KB

adamsitnik and others added 3 commits November 20, 2026 12:07
Command.Subcommands is now a public CommandList that supports registering
subcommands lazily. The parser creates only the subcommands that were matched.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 89664327-8eb4-43a6-8c7d-a21cc4d78084
@adamsitnik

Copy link
Copy Markdown
Member Author

Closing in favor of #2901

@adamsitnik adamsitnik closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant