Apply Optimization in Index.GetOffset suggested by Levi (#23349)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Fri, 22 Mar 2019 22:22:50 +0000 (15:22 -0700)
committerGitHub <noreply@github.com>
Fri, 22 Mar 2019 22:22:50 +0000 (15:22 -0700)
* Apply Optimization in Index.GetOffset suggested by Levi

* Add Comment

* address the feedback

src/System.Private.CoreLib/shared/System/Index.cs

index 9767b98..62d7f34 100644 (file)
@@ -103,13 +103,15 @@ namespace System
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public int GetOffset(int length)
         {
-            int offset;
-
+            int offset = _value;
             if (IsFromEnd)
-                offset = length - (~_value);
-            else
-                offset = _value;
+            {
+                // offset = length - (~value)
+                // offset = length + (~(~value) + 1)
+                // offset = length + value + 1
 
+                offset += length + 1;
+            }
             return offset;
         }