Use string.IsNullOrEmpty to eliminate bounds check to first char (#17512)
authorBen Adams <thundercat@illyriad.co.uk>
Thu, 12 Apr 2018 21:08:04 +0000 (22:08 +0100)
committerJan Kotas <jkotas@microsoft.com>
Thu, 12 Apr 2018 21:08:04 +0000 (14:08 -0700)
src/mscorlib/shared/System/String.cs

index 42d21ba..9d230fe 100644 (file)
@@ -438,7 +438,13 @@ namespace System
         [NonVersionable]
         public static bool IsNullOrEmpty(string value)
         {
-            return (value == null || value.Length == 0);
+            // Using 0u >= (uint)value.Length rather than
+            // value.Length == 0 as it will elide the bounds check to
+            // the first char: value[0] if that is performed following the test
+            // for the same test cost.
+            // Ternary operator returning true/false prevents redundant asm generation:
+            // https://github.com/dotnet/coreclr/issues/914
+            return (value == null || 0u >= (uint)value.Length) ? true : false;
         }
 
         public static bool IsNullOrWhiteSpace(string value)