Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 878dfda

Browse files
dotnet-botahsonkhan
authored andcommitted
Use string.IsNullOrEmpty to eliminate bounds check to first char (#17512) (#29082)
Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent c0264dd commit 878dfda

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Common/src/CoreLib/System/String.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,13 @@ public unsafe char[] ToCharArray(int startIndex, int length)
438438
[NonVersionable]
439439
public static bool IsNullOrEmpty(string value)
440440
{
441-
return (value == null || value.Length == 0);
441+
// Using 0u >= (uint)value.Length rather than
442+
// value.Length == 0 as it will elide the bounds check to
443+
// the first char: value[0] if that is performed following the test
444+
// for the same test cost.
445+
// Ternary operator returning true/false prevents redundant asm generation:
446+
// https://github.com/dotnet/coreclr/issues/914
447+
return (value == null || 0u >= (uint)value.Length) ? true : false;
442448
}
443449

444450
public static bool IsNullOrWhiteSpace(string value)

0 commit comments

Comments
 (0)