-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add Array.Reverse<T> #7132
Add Array.Reverse<T> #7132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1614,7 +1614,41 @@ public static void Reverse(Array array, int index, int length) { | |
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] | ||
private static extern bool TrySZReverse(Array array, int index, int count); | ||
|
||
|
||
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] | ||
public static void Reverse<T>(T[] array) | ||
{ | ||
if (array == null) | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); | ||
Contract.EndContractBlock(); | ||
Reverse(array, 0, array.Length); | ||
} | ||
|
||
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] | ||
public static void Reverse<T>(T[] array, int index, int length) | ||
{ | ||
if (array == null) | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); | ||
if (index < 0) | ||
ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); | ||
if (length < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might increase the generated code size, but I think you can make this a teeny bit faster for the common case: if ((index | length) < 0) // Will be true if either is negative
{
if (index < 0)
ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
else
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just aligned this with what @benaadams did for the non-generic |
||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); | ||
if (array.Length - index < length) | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); | ||
Contract.EndContractBlock(); | ||
|
||
int i = index; | ||
int j = index + length - 1; | ||
while (i < j) | ||
{ | ||
T temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
i++; | ||
j--; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop incurs range checks, while the native implementation does not. We may want to specialize if T is a primitive type and call into the native version if so, e.g.: // After arg validation
if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte) || ...)
{
bool result = TrySZReverse(array, index, length);
Contract.Assert(result);
return;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd be surprised if it did- last I checked, the JIT was unable to do so except for very simple cases. For example, this will incur range checks: int n = GetNumber();
var array = new int[n];
for (int i = 0; i < n; i++) array[i] = n;
For larger arrays it will be a big win because that's potentially hundreds/thousands of range checks that are being bypassed. You could change the conditional to if ((typeof(T) == ...) && length < SomeThreshold) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can always test the impact by switching them off https://github.com/dotnet/coreclr/issues/6790#issuecomment-241487380 There are some issues for range checks so hopefully this will be resolved, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also do something like: int j = index + length - 1;
for (var i = index; i < array.Length; i++)
{
if (i >= j)
{
break;
}
T temp = array[i];
array[i] = array[j];
array[j] = temp;
j--;
} Which should cut some (for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You're adding another branch for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer the original code - the fact that the jitter cannot eliminate the range checks today doesn't mean that it cannot do that tomorrow and if we tried to do various optimization hacks, it would be both less readable and may end up being slower after the jitter starts eliminating the checks here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is that clean and easy to read implementation is preferrable in this case. |
||
} | ||
|
||
// Sorts the elements of an array. The sort compares the elements to each | ||
// other using the IComparable interface, which must be implemented | ||
// by all elements of the array. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
EndContractBlock
isn't needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the code contract annotations aren't being maintained anymore going forward. I included them here for consistency with the existing code throughout this file, but I'd be fine with removing, if that's preferred. @jkotas do you have a preference? (I believe @weshaggard wanted to keep the annotations in existing code for now).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. The coding conventions say to use same style as the rest of the file.