Skip to content

Commit 2991f6d

Browse files
committed
Boost efficiency of Rooted IRecursiveParent.Children
And fix a bug found by this change making a test fail.
1 parent 5d831bd commit 2991f6d

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

src/ImmutableObjectGraph.Generation/CodeGen+RootedStructGen.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -743,19 +743,15 @@ protected MethodDeclarationSyntax CreateParentedNodeMethod()
743743
protected PropertyDeclarationSyntax[] CreateChildrenProperties()
744744
{
745745
return new PropertyDeclarationSyntax[] {
746-
// IEnumerable<IRecursiveType> IRecursiveParent.Children { get; }
746+
// IEnumerable<IRecursiveType> IRecursiveParent.Children => this.greenNode.Children;
747747
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))), nameof(IRecursiveParent.Children))
748748
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
749-
.AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration(
750-
SyntaxKind.GetAccessorDeclaration,
751-
SyntaxFactory.Block(
752-
// return System.Linq.Enumerable.Cast<IRecursiveType>(this.Children);
753-
SyntaxFactory.ReturnStatement(
754-
Syntax.EnumerableExtension(
755-
SyntaxFactory.GenericName(nameof(Enumerable.Cast))
756-
.AddTypeArgumentListArguments(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
757-
Syntax.ThisDot(this.applyTo.RecursiveParent.RecursiveField.NameAsProperty),
758-
SyntaxFactory.ArgumentList()))))),
749+
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
750+
SyntaxFactory.MemberAccessExpression(
751+
SyntaxKind.SimpleMemberAccessExpression,
752+
Syntax.ThisDot(GreenNodeFieldName),
753+
this.applyTo.RecursiveParent.RecursiveField.NameAsProperty)))
754+
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
759755
// IEnumerable<TRootedRecursiveType> IRecursiveParent<TRootedRecursiveType>.Children { get; }
760756
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(this.rootedRecursiveType), nameof(IRecursiveParent<IRecursiveType>.Children))
761757
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(CreateIRecursiveParentOfTSyntax(this.rootedRecursiveType)))

src/ImmutableObjectGraph/RecursiveTypeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ public static IEnumerable<TRecursiveType> GetSelfAndDescendentsBreadthFirst<TRec
292292
var visiting = nodesToVisit.Dequeue();
293293
yield return visiting;
294294

295-
var visitingAsParent = visiting as IRecursiveParent;
295+
var visitingAsParent = visiting as IRecursiveParent<TRecursiveType>;
296296
if (visitingAsParent != null && visitingAsParent.Children != null)
297297
{
298-
foreach (TRecursiveType child in visitingAsParent.Children)
298+
foreach (var child in visitingAsParent.Children)
299299
{
300300
nodesToVisit.Enqueue(child);
301301
}

0 commit comments

Comments
 (0)