Assist JIT in eliminating bounds checks (#81036)
authorxtqqczze <45661989+xtqqczze@users.noreply.github.com>
Mon, 15 May 2023 16:17:41 +0000 (17:17 +0100)
committerGitHub <noreply@github.com>
Mon, 15 May 2023 16:17:41 +0000 (12:17 -0400)
* for \(.+!= [^ ]+\.Length

* while \(.+!= [^ ]+\.Length

* Remove additional bounds check

13 files changed:
src/coreclr/tools/Common/Compiler/ReferenceSource/LinkAttributesParser.cs
src/coreclr/tools/aot/Mono.Linker.Tests/Extensions/NiceIO.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/BinderHelper.cs
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs
src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Merging/AsynchronousChannelMergeEnumerator.cs
src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Merging/SynchronousChannelMergeEnumerator.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.Number.cs
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.TimeSpan.BigG.cs
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.TimeSpan.cs
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.TimeSpanSplitter.cs
src/tools/illink/test/Mono.Linker.Tests/Extensions/NiceIO.cs

index 0acfdda..e4476e0 100644 (file)
@@ -173,7 +173,7 @@ namespace Mono.Linker.Steps
                     continue;
 
                 bool match = true;
-                for (int ii = 0; match && ii != args.Length; ++ii)
+                for (int ii = 0; match && ii < args.Length; ++ii)
                 {
                     //
                     // No candidates betterness, only exact matches are supported
index 5028f99..791d6b4 100644 (file)
@@ -328,7 +328,7 @@ namespace Mono.Linker.Tests.Extensions
                        if (p._elements.Length != _elements.Length)
                                return false;
 
-                       for (var i = 0; i != _elements.Length; i++)
+                       for (var i = 0; i < _elements.Length; i++)
                                if (!string.Equals (p._elements[i], _elements[i], PathStringComparison))
                                        return false;
 
index 2b4220b..9feac5c 100644 (file)
@@ -157,7 +157,7 @@ namespace Microsoft.CSharp.RuntimeBinder
         {
             if (arguments != null) // null is treated as empty, so not invalid
             {
-                for (int i = 0; i != arguments.Length; ++i)
+                for (int i = 0; i < arguments.Length; ++i)
                 {
                     ValidateBindArgument(arguments[i], $"{paramName}[{i}]");
                 }
index 953cc27..30496b0 100644 (file)
@@ -535,7 +535,7 @@ namespace Microsoft.Extensions.DependencyInjection
 
             public object CreateInstance(IServiceProvider provider)
             {
-                for (int index = 0; index != _parameters.Length; index++)
+                for (int index = 0; index < _parameters.Length; index++)
                 {
                     if (_parameterValues[index] == null)
                     {
index 167e1be..d5cd10c 100644 (file)
@@ -160,7 +160,7 @@ namespace System.Collections.Generic
                 {
                     comparer = Comparer; // obtain default if this is null.
                     Array.Sort<TKey, TValue>(keys, values, comparer);
-                    for (int i = 1; i != keys.Length; ++i)
+                    for (int i = 1; i < keys.Length; ++i)
                     {
                         if (comparer.Compare(keys[i - 1], keys[i]) == 0)
                         {
index 161fb73..4db5b14 100644 (file)
@@ -122,7 +122,7 @@ namespace System.Linq.Parallel
             int firstChannelIndex = _channelIndex;
 
             int currChannelIndex;
-            while ((currChannelIndex = _channelIndex) != _channels.Length)
+            while ((currChannelIndex = _channelIndex) < _channels.Length)
             {
                 AsynchronousChannel<T> current = _channels[currChannelIndex];
 
index ba10ef6..f288412 100644 (file)
@@ -84,7 +84,7 @@ namespace System.Linq.Parallel
             }
 
             // If the index has reached the end, we bail.
-            while (_channelIndex != _channels.Length)
+            while (_channelIndex < _channels.Length)
             {
                 SynchronousChannel<T> current = _channels[_channelIndex];
                 Debug.Assert(current != null);
index 81da0bf..e4e6f77 100644 (file)
@@ -74,10 +74,10 @@ namespace System.Net.Http.Headers
             int? maxAge = null;
             bool persist = false;
 
-            while (idx != value.Length)
+            while (idx < value.Length)
             {
                 // Skip OWS before semicolon.
-                while (idx != value.Length && IsOptionalWhiteSpace(value[idx])) ++idx;
+                while (idx < value.Length && IsOptionalWhiteSpace(value[idx])) ++idx;
 
                 if (idx == value.Length)
                 {
@@ -103,7 +103,7 @@ namespace System.Net.Http.Headers
                 ++idx;
 
                 // Skip OWS after semicolon / before value.
-                while (idx != value.Length && IsOptionalWhiteSpace(value[idx])) ++idx;
+                while (idx < value.Length && IsOptionalWhiteSpace(value[idx])) ++idx;
 
                 // Get the parameter key length.
                 int tokenLength = HttpRuleParser.GetTokenLength(value, idx);
index b0816d0..0349d7a 100644 (file)
@@ -44,7 +44,7 @@ namespace System.Buffers.Text
 
                 case Utf8Constants.Plus:
                     srcIndex++;
-                    if (srcIndex == source.Length)
+                    if (srcIndex >= source.Length)
                     {
                         bytesConsumed = 0;
                         return false;
@@ -61,7 +61,7 @@ namespace System.Buffers.Text
             int maxDigitCount = digits.Length - 1;
 
             // Throw away any leading zeroes
-            while (srcIndex != source.Length)
+            while (srcIndex < source.Length)
             {
                 c = source[srcIndex];
                 if (c != '0')
@@ -79,7 +79,7 @@ namespace System.Buffers.Text
             int startIndexNonLeadingDigitsBeforeDecimal = srcIndex;
 
             int hasNonZeroTail = 0;
-            while (srcIndex != source.Length)
+            while (srcIndex < source.Length)
             {
                 c = source[srcIndex];
                 int value = (byte)(c - (byte)('0'));
@@ -134,7 +134,7 @@ namespace System.Buffers.Text
                 srcIndex++;
                 int startIndexDigitsAfterDecimal = srcIndex;
 
-                while (srcIndex != source.Length)
+                while (srcIndex < source.Length)
                 {
                     c = source[srcIndex];
                     int value = (byte)(c - (byte)('0'));
@@ -268,7 +268,7 @@ namespace System.Buffers.Text
                 // continue eating digits from there
                 srcIndex += 10;
 
-                while (srcIndex != source.Length)
+                while (srcIndex < source.Length)
                 {
                     c = source[srcIndex];
                     int value = (byte)(c - (byte)('0'));
index 94d307d..5046731 100644 (file)
@@ -9,7 +9,7 @@ namespace System.Buffers.Text
         {
             int srcIndex = 0;
             byte c = default;
-            while (srcIndex != source.Length)
+            while (srcIndex < source.Length)
             {
                 c = source[srcIndex];
                 if (!(c == ' ' || c == '\t'))
index 7dd0382..a6ac0d4 100644 (file)
@@ -75,7 +75,7 @@ namespace System.Buffers.Text
             uint fraction = digit;
             int digitCount = 1;
 
-            while (srcIndex != source.Length)
+            while (srcIndex < source.Length)
             {
                 digit = source[srcIndex] - 48u; // '0'
                 if (digit > 9)
index ce119a8..60521e6 100644 (file)
@@ -38,7 +38,7 @@ namespace System.Buffers.Text
                 byte c = default;
 
                 // Unlike many other data types, TimeSpan allow leading whitespace.
-                while (srcIndex != source.Length)
+                while (srcIndex < source.Length)
                 {
                     c = source[srcIndex];
                     if (!(c == ' ' || c == '\t'))
index 4fde108..6969069 100644 (file)
@@ -325,7 +325,7 @@ namespace Mono.Linker.Tests.Extensions
                        if (p._elements.Length != _elements.Length)
                                return false;
 
-                       for (var i = 0; i != _elements.Length; i++)
+                       for (var i = 0; i < _elements.Length; i++)
                                if (!string.Equals (p._elements[i], _elements[i], PathStringComparison))
                                        return false;
 
@@ -869,4 +869,4 @@ namespace Mono.Linker.Tests.Extensions
                Normal,
                Soft
        }
-}
\ No newline at end of file
+}