From 6ac136e313826833b7fd32f0c8968297e8d7458e Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 12 Apr 2018 22:08:04 +0100 Subject: Use string.IsNullOrEmpty to eliminate bounds check to first char (#17512) --- src/mscorlib/shared/System/String.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mscorlib/shared/System/String.cs b/src/mscorlib/shared/System/String.cs index 42d21ba068..9d230fe3dc 100644 --- a/src/mscorlib/shared/System/String.cs +++ b/src/mscorlib/shared/System/String.cs @@ -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) -- cgit v1.2.3