Skip to content

Commit

Permalink
Assist JIT in eliminating bounds checks (#81036)
Browse files Browse the repository at this point in the history
* for \(.+!= [^ ]+\.Length

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

* Remove additional bounds check
  • Loading branch information
xtqqczze authored May 15, 2023
1 parent 435c1d2 commit 6730c71
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static string FormatCustomAttribute(CustomAttribute ca)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public bool Equals (NPath? p)
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void ValidateBindArgument(DynamicMetaObject[] arguments, string pa
{
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}]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public int Match(object[] givenParameters, IServiceProviderIsService serviceProv

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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public SortedList(IDictionary<TKey, TValue> dictionary, IComparer<TKey>? compare
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private bool MoveNextSlowPath()
int firstChannelIndex = _channelIndex;

int currChannelIndex;
while ((currChannelIndex = _channelIndex) != _channels.Length)
while ((currChannelIndex = _channelIndex) < _channels.Length)
{
AsynchronousChannel<T> current = _channels[currChannelIndex];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override bool MoveNext()
}

// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ protected override int GetParsedValueLength(string value, int startIndex, object
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)
{
Expand All @@ -103,7 +103,7 @@ protected override int GetParsedValueLength(string value, int startIndex, object
++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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static bool TryParseNumber(ReadOnlySpan<byte> source, ref Number.NumberB

case Utf8Constants.Plus:
srcIndex++;
if (srcIndex == source.Length)
if (srcIndex >= source.Length)
{
bytesConsumed = 0;
return false;
Expand All @@ -61,7 +61,7 @@ private static bool TryParseNumber(ReadOnlySpan<byte> source, ref Number.NumberB
int maxDigitCount = digits.Length - 1;

// Throw away any leading zeroes
while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
c = source[srcIndex];
if (c != '0')
Expand All @@ -79,7 +79,7 @@ private static bool TryParseNumber(ReadOnlySpan<byte> source, ref Number.NumberB
int startIndexNonLeadingDigitsBeforeDecimal = srcIndex;

int hasNonZeroTail = 0;
while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
c = source[srcIndex];
int value = (byte)(c - (byte)('0'));
Expand Down Expand Up @@ -134,7 +134,7 @@ private static bool TryParseNumber(ReadOnlySpan<byte> source, ref Number.NumberB
srcIndex++;
int startIndexDigitsAfterDecimal = srcIndex;

while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
c = source[srcIndex];
int value = (byte)(c - (byte)('0'));
Expand Down Expand Up @@ -268,7 +268,7 @@ private static bool TryParseNumber(ReadOnlySpan<byte> source, ref Number.NumberB
// continue eating digits from there
srcIndex += 10;

while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
c = source[srcIndex];
int value = (byte)(c - (byte)('0'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private static bool TryParseTimeSpanBigG(ReadOnlySpan<byte> source, out TimeSpan
{
int srcIndex = 0;
byte c = default;
while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
c = source[srcIndex];
if (!(c == ' ' || c == '\t'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static bool TryParseTimeSpanFraction(ReadOnlySpan<byte> source, out uint
uint fraction = digit;
int digitCount = 1;

while (srcIndex != source.Length)
while (srcIndex < source.Length)
{
digit = source[srcIndex] - 48u; // '0'
if (digit > 9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public bool TrySplitTimeSpan(ReadOnlySpan<byte> source, bool periodUsedToSeparat
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'))
Expand Down
4 changes: 2 additions & 2 deletions src/tools/illink/test/Mono.Linker.Tests/Extensions/NiceIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public bool Equals (NPath p)
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;

Expand Down Expand Up @@ -869,4 +869,4 @@ public enum DeleteMode
Normal,
Soft
}
}
}

0 comments on commit 6730c71

Please sign in to comment.