Make Stack<T>.Enumerator.Current inlineable by the JIT (dotnet/corefx#9942)
authorJames Ko <jamesqko@gmail.com>
Sun, 10 Jul 2016 17:26:19 +0000 (13:26 -0400)
committerStephen Toub <stoub@microsoft.com>
Sun, 10 Jul 2016 17:26:19 +0000 (13:26 -0400)
* Make Stack<T>.Enumerator.Current inlineable by the JIT

Commit migrated from https://github.com/dotnet/corefx/commit/603c29ee79e5c03d3bb03b04f49e40084974d76c

src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs

index 3cd68bb..20e6470 100644 (file)
@@ -258,15 +258,15 @@ namespace System.Collections.Generic
         public struct Enumerator : IEnumerator<T>,
             System.Collections.IEnumerator
         {
-            private Stack<T> _stack;
+            private readonly Stack<T> _stack;
+            private readonly int _version;
             private int _index;
-            private int _version;
             private T _currentElement;
 
             internal Enumerator(Stack<T> stack)
             {
                 _stack = stack;
-                _version = _stack._version;
+                _version = stack._version;
                 _index = -2;
                 _currentElement = default(T);
             }
@@ -308,12 +308,18 @@ namespace System.Collections.Generic
             {
                 get
                 {
-                    if (_index == -2) throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
-                    if (_index == -1) throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
+                    if (_index < 0)
+                        ThrowEnumerationNotStartedOrEnded();
                     return _currentElement;
                 }
             }
-
+            
+            private void ThrowEnumerationNotStartedOrEnded()
+            {
+                Debug.Assert(_index == -1 || _index == -2);
+                throw new InvalidOperationException(_index == -2 ? SR.InvalidOperation_EnumNotStarted : SR.InvalidOperation_EnumEnded);
+            }
+            
             Object System.Collections.IEnumerator.Current
             {
                 get { return Current; }