From c8a34dc7711f96826bebe65e7a3ab7273ff31620 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 17 Jan 2022 11:16:35 +0500 Subject: [PATCH 1/2] Use more efficient platform detection API Use static and const new OperatingSystem class API to avoid string comparisons and extra allocations. --- .../commands/management/ComputerUnix.cs | 2 +- .../commands/utility/UnblockFile.cs | 2 +- src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs | 2 +- .../CoreCLR/CorePsAssemblyLoadContext.cs | 6 +++--- src/System.Management.Automation/CoreCLR/CorePsPlatform.cs | 6 +++--- .../namespaces/FileSystemProvider.cs | 6 ++---- .../security/SecuritySupport.cs | 2 +- src/powershell/Program.cs | 3 ++- test/perf/benchmarks/Engine.ScriptBlock.cs | 4 ++-- 9 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index fb01823790f..a11fb0270c9 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -67,7 +67,7 @@ public sealed class StopComputerCommand : CommandLineCmdletBase protected override void BeginProcessing() { var args = "-P now"; - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (Platform.IsMacOS) { args = "now"; } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 5c297ed86d5..6633a66f96f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -137,7 +137,7 @@ protected override void ProcessRecord() } } #else - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + if (Platform.IsLinux) { string errorMessage = UnblockFileStrings.LinuxNotSupported; Exception e = new PlatformNotSupportedException(errorMessage); diff --git a/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs b/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs index 3b253582b63..33675e30b44 100644 --- a/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs +++ b/src/Microsoft.PowerShell.GlobalTool.Shim/GlobalToolShim.cs @@ -26,7 +26,7 @@ public static class EntryPoint public static int Main(string[] args) { var currentPath = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).Directory.FullName; - var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + var isWindows = Platform.IsWindows; string platformFolder = isWindows ? WinFolderName : UnixFolderName; diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 43e6ad17eb4..fad311df98d 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -540,17 +540,17 @@ private static string GetNativeDllSubFolderName(out string ext) ext = string.Empty; var processArch = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (Platform.IsWindows) { folderName = "win-" + processArch; ext = ".dll"; } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + else if (Platform.IsLinux) { folderName = "linux-" + processArch; ext = ".so"; } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + else if (Platform.IsMacOS) { folderName = "osx-x64"; ext = ".dylib"; diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 45be8721b7f..1986c4c63be 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -22,7 +22,7 @@ public static bool IsLinux { get { - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + return OperatingSystem.IsLinux(); } } @@ -33,7 +33,7 @@ public static bool IsMacOS { get { - return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + return OperatingSystem.IsMacOS(); } } @@ -44,7 +44,7 @@ public static bool IsWindows { get { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + return OperatingSystem.IsWindows(); } } diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 1e3ab21ccaf..8608372cc5a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -469,9 +469,7 @@ protected override ProviderInfo Start(ProviderInfo providerInfo) #if !UNIX // The placeholder mode management APIs Rtl(Set|Query)(Process|Thread)PlaceholderCompatibilityMode // are only supported starting with Windows 10 version 1803 (build 17134) - Version minBuildForPlaceHolderAPIs = new Version(10, 0, 17134, 0); - - if (Environment.OSVersion.Version >= minBuildForPlaceHolderAPIs) + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134, 0)) { // let's be safe, don't change the PlaceHolderCompatibilityMode if the current one is not what we expect if (NativeMethods.RtlQueryProcessPlaceholderCompatibilityMode() == NativeMethods.PHCM_DISGUISE_PLACEHOLDER) @@ -2725,7 +2723,7 @@ private static bool WinCreateSymbolicLink(string path, string strTargetPath, boo var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File; Version minBuildOfDeveloperMode = new Version(10, 0, 14972, 0); - if (Environment.OSVersion.Version >= minBuildOfDeveloperMode) + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14972, 0)) { flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate; } diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 7b5087411ec..6321d301ced 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -1213,7 +1213,7 @@ private void ResolveFromStoreById(ResolutionPurpose purpose, out ErrorRecord err storeCU.Open(OpenFlags.ReadOnly); X509Certificate2Collection storeCerts = storeCU.Certificates; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (Platform.IsWindows) { using (var storeLM = new X509Store("my", StoreLocation.LocalMachine)) { diff --git a/src/powershell/Program.cs b/src/powershell/Program.cs index 3fee683618f..d085db22b98 100644 --- a/src/powershell/Program.cs +++ b/src/powershell/Program.cs @@ -5,6 +5,7 @@ using System; using System.IO; +using System.Management.Automation; using System.Reflection; using System.Runtime.InteropServices; @@ -89,7 +90,7 @@ private static void AttemptExecPwshLogin(string[] args) return; } - bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + bool isLinux = Platform.IsLinux; // The first byte (ASCII char) of the name of this process, used to detect '-' for login byte procNameFirstByte; diff --git a/test/perf/benchmarks/Engine.ScriptBlock.cs b/test/perf/benchmarks/Engine.ScriptBlock.cs index ad373dd5f99..1f9ac989428 100644 --- a/test/perf/benchmarks/Engine.ScriptBlock.cs +++ b/test/perf/benchmarks/Engine.ScriptBlock.cs @@ -36,7 +36,7 @@ public IEnumerable ValuesForScript() yield return @"[System.IO.Path]::HasExtension('')"; // Test on COM method invocation. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (Platform.IsWindows) { yield return @"$sh=New-Object -ComObject Shell.Application; $sh.Namespace('c:\')"; yield return @"$fs=New-Object -ComObject scripting.filesystemobject; $fs.Drives"; @@ -54,7 +54,7 @@ public void GlobalSetup() // believe that there is no need to run many ops in each interation. However, the subsequent runs // of this method is much faster than the first run, and this causes 'MinIterationTime' warnings // to our benchmarks and make the benchmark results not reliable. - // Calling this method once in 'GlobalSetup' is a workaround. + // Calling this method once in 'GlobalSetup' is a workaround. // See https://github.com/dotnet/BenchmarkDotNet/issues/837#issuecomment-828600157 scriptBlock.Invoke(); } From 2df0e8016e3946007a66de4e89542f6d90e41d68 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 18 Jan 2022 22:37:18 +0500 Subject: [PATCH 2/2] Remove unused variable --- .../namespaces/FileSystemProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 8608372cc5a..b54c4ab6b00 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2722,7 +2722,6 @@ private static bool WinCreateSymbolicLink(string path, string strTargetPath, boo // The new AllowUnprivilegedCreate is only available on Win10 build 14972 or newer var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File; - Version minBuildOfDeveloperMode = new Version(10, 0, 14972, 0); if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14972, 0)) { flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate;