Skip to content

More use of SequenceEqual, IndexOf, IndexOfAnyExcept, etc. #71278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -843,27 +843,13 @@ public void AddSentinel()
AddElementType(CorElementType.ELEMENT_TYPE_SENTINEL);
}

public override bool Equals(object? obj)
{
if (!(obj is SignatureHelper))
{
return false;
}

SignatureHelper temp = (SignatureHelper)obj;

if (!temp.m_module!.Equals(m_module) || temp.m_currSig != m_currSig || temp.m_sizeLoc != m_sizeLoc || temp.m_sigDone != m_sigDone)
{
return false;
}

for (int i = 0; i < m_currSig; i++)
{
if (m_signature[i] != temp.m_signature[i])
return false;
}
return true;
}
public override bool Equals(object? obj) =>
obj is SignatureHelper other &&
other.m_module!.Equals(m_module) &&
other.m_currSig == m_currSig &&
other.m_sizeLoc == m_sizeLoc &&
other.m_sigDone == m_sigDone &&
m_signature.AsSpan(0, m_currSig).SequenceEqual(other.m_signature.AsSpan(0, m_currSig));

public override int GetHashCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,8 @@ private static bool TryParseMapsEntry(string line, out (long StartAddress, int S

static bool HasReadAndExecFlags(string s, ref int start, ref int end)
{
bool sawRead = false, sawExec = false;
for (int i = start; i < end; i++)
{
if (s[i] == 'r')
sawRead = true;
else if (s[i] == 'x')
sawExec = true;
}

return sawRead & sawExec;
ReadOnlySpan<char> span = s.AsSpan(start, end - start);
return span.Contains('r') && span.Contains('x');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,9 @@ public bool Equals(IPAddress other)

fixed (byte* thisAddress = Address)
{
for (int i = 0; i < addressByteCount; i++)
{
if (thisAddress[i] != other.Address[i])
{
return false;
}
}
return new ReadOnlySpan<byte>(thisAddress, addressByteCount).SequenceEqual(
new ReadOnlySpan<byte>(other.Address, addressByteCount));
}

return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,7 @@ private static bool ArrayEquals(byte[]? previousHash, byte[] currentHash)
}

Debug.Assert(previousHash.Length == currentHash.Length);
for (int i = 0; i < previousHash.Length; i++)
{
if (previousHash[i] != currentHash[i])
{
return false;
}
}

return true;
return previousHash.AsSpan().SequenceEqual(currentHash.AsSpan());
}

private void ComputeHash(IncrementalHash sha256, string path, DateTime lastChangedUtc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,20 +1874,8 @@ void ICollection.CopyTo(Array array, int index)
#endregion


private bool AreAllBucketsEmpty()
{
int[] countPerLock = _tables._countPerLock;

for (int i = 0; i < countPerLock.Length; i++)
{
if (countPerLock[i] != 0)
{
return false;
}
}

return true;
}
private bool AreAllBucketsEmpty() =>
_tables._countPerLock.AsSpan().IndexOfAnyExcept(0) < 0;

/// <summary>
/// Replaces the bucket table with a larger one. To prevent multiple threads from resizing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,17 @@ internal partial struct StringSequenceMany : IEquatable<StringSequenceMany>, ISt
{
private readonly string[] _values;

public StringSequenceMany(string[] values)
{
public StringSequenceMany(string[] values) =>
_values = values;
}

public Span<string> AsSpan()
{
return _values.AsSpan();
}
public Span<string> AsSpan() =>
_values.AsSpan();

public bool Equals(StringSequenceMany other)
{
if (_values.Length != other._values.Length)
{
return false;
}
for (int i = 0; i < _values.Length; i++)
{
if (_values[i] != other._values[i])
{
return false;
}
}
return true;
}
public bool Equals(StringSequenceMany other) =>
_values.AsSpan().SequenceEqual(other._values.AsSpan());

//GetHashCode() is in the platform specific files
public override bool Equals(object? obj)
{
return obj is StringSequenceMany ssm && Equals(ssm);
}
public override bool Equals(object? obj) =>
obj is StringSequenceMany ssm && Equals(ssm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,8 @@ internal static long ConvertDecimalToOctal(long value)
}

// Returns true if all the bytes in the specified array are nulls, false otherwise.
internal static bool IsAllNullBytes(Span<byte> buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] != 0)
{
return false;
}
}
return true;
}
internal static bool IsAllNullBytes(Span<byte> buffer) =>
buffer.IndexOfAnyExcept((byte)0) < 0;

// Converts the specified number of seconds that have passed since the Unix Epoch to a DateTimeOffset.
internal static DateTimeOffset GetDateTimeOffsetFromSecondsSinceEpoch(long secondsSinceUnixEpoch) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,15 @@ public void Clear()
/// </summary>
/// <param name="item">the object to locate in the <see cref="ReadOnlyCollectionBuilder{T}"/>.</param>
/// <returns>true if item is found in the <see cref="ReadOnlyCollectionBuilder{T}"/>; otherwise, false.</returns>
public bool Contains(T item)
{
if ((object?)item == null)
{
for (int i = 0; i < _size; i++)
{
if ((object?)_items[i] == null)
{
return true;
}
}
return false;
}
else
{
EqualityComparer<T> c = EqualityComparer<T>.Default;
for (int i = 0; i < _size; i++)
{
if (c.Equals(_items[i], item))
{
return true;
}
}
return false;
}
}
public bool Contains(T item) => IndexOf(item) >= 0;

/// <summary>
/// Copies the elements of the <see cref="ReadOnlyCollectionBuilder{T}"/> to an <see cref="Array"/>,
/// starting at particular <see cref="Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="ReadOnlyCollectionBuilder{T}"/>.</param>
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
public void CopyTo(T[] array, int arrayIndex)
{
Array.Copy(_items, 0, array, arrayIndex, _size);
}
public void CopyTo(T[] array, int arrayIndex) => Array.Copy(_items, 0, array, arrayIndex, _size);

bool ICollection<T>.IsReadOnly => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,22 @@ private bool ReadLine(out int startIndex, out int length)
{
Debug.Assert(_buffer != null);

int i = _position;
int pos = _position;

while (i < _length)
int newline = _buffer.AsSpan(pos, _length - pos).IndexOf("\r\n".AsSpan());
if (newline >= 0)
{
char ch = _buffer[i];
if (ch == '\r')
{
int next = i + 1;
if (next < _length && _buffer[next] == '\n')
{
startIndex = _position;
length = i - _position;
_position = i + 2;
return true;
}
}
i++;
startIndex = pos;
length = newline;
_position = pos + newline + 2;
return true;
}

if (i > _position)
if (pos < _length)
{
startIndex = _position;
length = i - _position;
_position = i;
startIndex = pos;
length = _length - pos;
_position = _length;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,8 @@ public HttpListenerContext EndGetContext(IAsyncResult asyncResult)

// Find the beginning of the blob. Trust that HTTP.SYS parsed out just our header ok.
Debug.Assert(authorizationHeader != null);
for (index++; index < authorizationHeader!.Length; index++)
{
if (authorizationHeader[index] != ' ' && authorizationHeader[index] != '\t' &&
authorizationHeader[index] != '\r' && authorizationHeader[index] != '\n')
{
break;
}
}
int nonWhitespace = authorizationHeader.AsSpan(index + 1).IndexOfAnyExcept(" \t\r\n");
index = nonWhitespace >= 0 ? index + 1 + nonWhitespace : authorizationHeader.Length;
string inBlob = index < authorizationHeader.Length ? authorizationHeader.Substring(index) : "";

IPrincipal? principal = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,11 @@ public override int GetHashCode()
return _hash;
}

public override bool Equals([NotNullWhen(true)] object? comparand)
{
PhysicalAddress? address = comparand as PhysicalAddress;
if (address == null)
{
return false;
}

if (_address.Length != address._address.Length)
{
return false;
}

if (GetHashCode() != address.GetHashCode())
{
return false;
}

for (int i = 0; i < address._address.Length; i++)
{
if (_address[i] != address._address[i])
{
return false;
}
}

return true;
}
public override bool Equals([NotNullWhen(true)] object? comparand) =>
comparand is PhysicalAddress other &&
_address.Length == other._address.Length &&
GetHashCode() == other.GetHashCode() &&
_address.AsSpan().SequenceEqual(other._address);

public override string ToString()
{
Expand Down
38 changes: 10 additions & 28 deletions src/libraries/System.Private.CoreLib/src/System/ApplicationId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,17 @@ public override string ToString()
return sb.ToString();
}

public override bool Equals([NotNullWhen(true)] object? o)
{
ApplicationId? other = o as ApplicationId;
if (other == null)
return false;

if (!(Equals(Name, other.Name) &&
Equals(Version, other.Version) &&
Equals(ProcessorArchitecture, other.ProcessorArchitecture) &&
Equals(Culture, other.Culture)))
return false;

if (_publicKeyToken.Length != other._publicKeyToken.Length)
return false;

for (int i = 0; i < _publicKeyToken.Length; i++)
{
if (_publicKeyToken[i] != other._publicKeyToken[i])
return false;
}

return true;
}

public override int GetHashCode()
{
public override bool Equals([NotNullWhen(true)] object? o) =>
o is ApplicationId other &&
Equals(Name, other.Name) &&
Equals(Version, other.Version) &&
Equals(ProcessorArchitecture, other.ProcessorArchitecture) &&
Equals(Culture, other.Culture) &&
_publicKeyToken.AsSpan().SequenceEqual(other._publicKeyToken);

public override int GetHashCode() =>
// Note: purposely skipping publicKeyToken, processor architecture and culture as they
// are less likely to make things not equal than name and version.
return Name.GetHashCode() ^ Version.GetHashCode();
}
Name.GetHashCode() ^ Version.GetHashCode();
}
}
Loading