From 3528365d2956e1ed9e40af0f568042eb5fc63dce Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 29 Apr 2020 15:55:48 +0100 Subject: [PATCH 1/5] Expand defaultRefAssemblies list capacity Expand `defaultRefAssemblies` initial list capacity to be greater than number of items stored. --- .../commands/utility/AddType.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 833c373ca9c..2b207e1dc57 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -668,8 +668,8 @@ private void LoadAssemblies(IEnumerable assemblies) /// private static IEnumerable InitDefaultRefAssemblies() { - // netcoreapp3.0 currently comes with 148 reference assemblies (maybe more in future), so we use a capacity of '150'. - var defaultRefAssemblies = new List(150); + // netcoreapp5.0 (5.0.0-preview.3.20214.6) is distributed with 152 reference assemblies, so we use a capacity of '160'. + var defaultRefAssemblies = new List(160); foreach (string file in Directory.EnumerateFiles(s_netcoreAppRefFolder, "*.dll", SearchOption.TopDirectoryOnly)) { From 91371158652ff09757a27750018eef64856574a5 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 1 May 2020 21:33:30 +0100 Subject: [PATCH 2/5] Restate list initial capacity --- .../commands/utility/AddType.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 2b207e1dc57..5fe9c52a774 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -668,14 +668,20 @@ private void LoadAssemblies(IEnumerable assemblies) /// private static IEnumerable InitDefaultRefAssemblies() { - // netcoreapp5.0 (5.0.0-preview.3.20214.6) is distributed with 152 reference assemblies, so we use a capacity of '160'. - var defaultRefAssemblies = new List(160); + // PowerShell is distributed with 149 reference assemblies, so we use a capacity of '150'. + var defaultRefAssemblies = new List(150); foreach (string file in Directory.EnumerateFiles(s_netcoreAppRefFolder, "*.dll", SearchOption.TopDirectoryOnly)) { defaultRefAssemblies.Add(MetadataReference.CreateFromFile(file)); } + // Add System.Management.Automation.dll defaultRefAssemblies.Add(MetadataReference.CreateFromFile(typeof(PSObject).Assembly.Location)); + + // We want to avoid reallocating the internal array, so we assert if the list capacity has increased. + System.Diagnostics.Debug.Assert(defaultRefAssemblies.Capacity <= 150, + $"defaultRefAssemblies was resized because of insufficient initial capacity! A capacity of {defaultRefAssemblies.Count} is required."); + return defaultRefAssemblies; } From d2ce5fba1ecd3858deff3c927e91a7fae41f7a03 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 1 May 2020 21:40:24 +0100 Subject: [PATCH 3/5] Fix CodeFactor issues --- .../commands/utility/AddType.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 5fe9c52a774..1a4cf3f043f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -675,11 +675,13 @@ private static IEnumerable InitDefaultRefAssemblies { defaultRefAssemblies.Add(MetadataReference.CreateFromFile(file)); } + // Add System.Management.Automation.dll defaultRefAssemblies.Add(MetadataReference.CreateFromFile(typeof(PSObject).Assembly.Location)); // We want to avoid reallocating the internal array, so we assert if the list capacity has increased. - System.Diagnostics.Debug.Assert(defaultRefAssemblies.Capacity <= 150, + System.Diagnostics.Debug.Assert( + defaultRefAssemblies.Capacity <= 150, $"defaultRefAssemblies was resized because of insufficient initial capacity! A capacity of {defaultRefAssemblies.Count} is required."); return defaultRefAssemblies; From c1a2211645bcd2b7c947795c7bc24fdc3891d1ca Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 2 May 2020 18:03:10 +0100 Subject: [PATCH 4/5] Use System.Management.Automation.Diagnostics.Assert address @ iSazonov review --- .../commands/utility/AddType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 1a4cf3f043f..d6f99cfc970 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -680,7 +680,7 @@ private static IEnumerable InitDefaultRefAssemblies defaultRefAssemblies.Add(MetadataReference.CreateFromFile(typeof(PSObject).Assembly.Location)); // We want to avoid reallocating the internal array, so we assert if the list capacity has increased. - System.Diagnostics.Debug.Assert( + Diagnostics.Assert( defaultRefAssemblies.Capacity <= 150, $"defaultRefAssemblies was resized because of insufficient initial capacity! A capacity of {defaultRefAssemblies.Count} is required."); From e1bc8d16754f054f64fc4fee4b1cf1be5dd0fa49 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 4 May 2020 16:21:24 +0100 Subject: [PATCH 5/5] Define a const for capacity, etc address @iSazonov review --- .../commands/utility/AddType.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index d6f99cfc970..6e19417e3ee 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -668,8 +668,12 @@ private void LoadAssemblies(IEnumerable assemblies) /// private static IEnumerable InitDefaultRefAssemblies() { - // PowerShell is distributed with 149 reference assemblies, so we use a capacity of '150'. - var defaultRefAssemblies = new List(150); + // Define number of reference assemblies distributed with PowerShell. + // This number is accurate as of PowerShell v7.1.0-preview.1 built with .NET v5.0.100-preview.1.20155.7 + const int numberOfPowershellRefAssemblies = 151; + + const int capacity = numberOfPowershellRefAssemblies + 1; + var defaultRefAssemblies = new List(capacity); foreach (string file in Directory.EnumerateFiles(s_netcoreAppRefFolder, "*.dll", SearchOption.TopDirectoryOnly)) { @@ -681,7 +685,7 @@ private static IEnumerable InitDefaultRefAssemblies // We want to avoid reallocating the internal array, so we assert if the list capacity has increased. Diagnostics.Assert( - defaultRefAssemblies.Capacity <= 150, + defaultRefAssemblies.Capacity <= capacity, $"defaultRefAssemblies was resized because of insufficient initial capacity! A capacity of {defaultRefAssemblies.Count} is required."); return defaultRefAssemblies;