Why do you have an Enumerator class and an EnumeratorImpl class?

I am watching Roslyn September 2012 CTP with Reflector, and I noticed that the ChildSyntaxList structure has the following:

public struct ChildSyntaxList : IEnumerable<SyntaxNodeOrToken> { private readonly SyntaxNode node; private readonly int count; public Enumerator GetEnumerator() { return node == null ? new Enumerator() : new Enumerator(node, count); } IEnumerator<SyntaxNodeOrToken> IEnumerable<SyntaxNodeOrToken>.GetEnumerator() { return node == null ? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>() : new EnumeratorImpl(node, count); } IEnumerator IEnumerable.GetEnumerator() { return node == null ? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>() : new EnumeratorImpl(node, count); } public struct Enumerator { internal Enumerator(SyntaxNode node, int count) { /* logic */ } public SyntaxNodeOrToken Current { get { /* logic */ } } public bool MoveNext() { /* logic */ } public void Reset() { /* logic */ } } private class EnumeratorImpl : IEnumerator<SyntaxNodeOrToken> { private Enumerator enumerator; internal EnumeratorImpl(SyntaxNode node, int count) { enumerator = new Enumerator(node, count); } public SyntaxNodeOrToken Current { get { return enumerator.Current; } } object IEnumerator.Current { get { return enumerator.Current; } } public void Dispose() { } public bool MoveNext() { return enumerator.MoveNext(); } public void Reset() { enumerator.Reset(); } } } 

That is, there is a GetEnumerator method that returns a structure.

It seems that

However, unlike the BCL List<T> class, there is a nested EnumeratorImpl class. Is the purpose of this for

  • avoid creating a one-time structure and
  • to avoid boxing within explicitly implemented methods IEnumerable<SyntaxNodeOrToken>.GetEnumerator and IEnumerable.GetEnumerator ?

Are there any other reasons?

+4
source share
1 answer

Are there any other reasons?

No one comes to mind. You seem to have accurately described the goals of this rather odd implementation of the sequence template.

I hasten to add: Roslyn is an unusual .NET application in its complexity, performance requirements and the number of objects to create. A compiler that parses programs with thousands of files, millions of lines and tens of millions of characters as the user types them in must do some rather unusual things to ensure that it does not suppress the garbage collector. Thus, Roslyn uses combination strategies, uses modified value types and other methods that do not comply with the basic principles to achieve these goals. I do not recommend taking the costs and difficulties associated with these practices unless you have empirical evidence of a serious performance problem that these methods mitigate. Just because this code was written by the C # compiler team does not mean that this is the gold standard for how you should write your main business objects.

+19
source

Source: https://habr.com/ru/post/1501669/


All Articles