From ee683a09634c967240bd63b3c925d6a99e0ac014 Mon Sep 17 00:00:00 2001 From: James Ko Date: Tue, 17 May 2016 11:46:08 -0400 Subject: [PATCH] Optimize Array.GetLength, GetUpperBound, GetLowerBound (#4993) ... if the argument is 0. As the rank will always be greater, this is safe. --- src/classlibnative/bcltype/arraynative.cpp | 44 ++++++++++++++++++------------ 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/classlibnative/bcltype/arraynative.cpp b/src/classlibnative/bcltype/arraynative.cpp index 3f3b2b7..c98eaf8 100644 --- a/src/classlibnative/bcltype/arraynative.cpp +++ b/src/classlibnative/bcltype/arraynative.cpp @@ -39,13 +39,15 @@ FCIMPL2(INT32, ArrayNative::GetLowerBound, ArrayBase* array, unsigned int dimens if (array == NULL) FCThrow(kNullReferenceException); - - // What is this an array of? - MethodTable *pArrayMT = array->GetMethodTable(); - DWORD Rank = pArrayMT->GetRank(); - - if (dimension >= Rank) - FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex")); + + if (dimension != 0) + { + // Check the dimension is within our rank + unsigned int rank = array->GetRank(); + + if (dimension >= rank) + FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex")); + } return array->GetLowerBoundsPtr()[dimension]; } @@ -61,13 +63,15 @@ FCIMPL2(INT32, ArrayNative::GetUpperBound, ArrayBase* array, unsigned int dimens if (array == NULL) FCThrow(kNullReferenceException); - - // What is this an array of? - MethodTable *pArrayMT = array->GetMethodTable(); - DWORD Rank = pArrayMT->GetRank(); - - if (dimension >= Rank) - FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex")); + + if (dimension != 0) + { + // Check the dimension is within our rank + unsigned int rank = array->GetRank(); + + if (dimension >= rank) + FCThrowRes(kIndexOutOfRangeException, W("IndexOutOfRange_ArrayRankIndex")); + } return array->GetBoundsPtr()[dimension] + array->GetLowerBoundsPtr()[dimension] - 1; } @@ -82,9 +86,15 @@ FCIMPL2(INT32, ArrayNative::GetLength, ArrayBase* array, unsigned int dimension) if (array==NULL) FCThrow(kNullReferenceException); - unsigned int rank = array->GetRank(); - if (dimension >= rank) - FCThrow(kIndexOutOfRangeException); + + if (dimension != 0) + { + // Check the dimension is within our rank + unsigned int rank = array->GetRank(); + if (dimension >= rank) + FCThrow(kIndexOutOfRangeException); + } + return array->GetBoundsPtr()[dimension]; } FCIMPLEND -- 2.7.4