Skip to content

Commit

Permalink
Reduce garbage created by ILNode.GetSelfAndChildrenRecursive when enu…
Browse files Browse the repository at this point in the history
…merating over Lists of child nodes.
  • Loading branch information
kg committed Aug 16, 2012
1 parent 4a75356 commit a870acd
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions ICSharpCode.Decompiler/ILAst/ILAstTypes.cs
Expand Up @@ -41,12 +41,17 @@ public abstract class ILNode
return result;
}

void AccumulateSelfAndChildrenRecursive<T>(List<T> list, Func<T, bool> predicate) where T:ILNode
internal void AccumulateSelfAndChildrenRecursive<T>(List<T> list, Func<T, bool> predicate) where T:ILNode
{
// Note: RemoveEndFinally depends on self coming before children
T thisAsT = this as T;
if (thisAsT != null && (predicate == null || predicate(thisAsT)))
list.Add(thisAsT);

AccumulateChildrenRecursiveInto<T>(list, predicate);
}

protected virtual void AccumulateChildrenRecursiveInto<T> (List<T> list, Func<T, bool> predicate) where T: ILNode {
foreach (ILNode node in this.GetChildren()) {
if (node != null)
node.AccumulateSelfAndChildrenRecursive(list, predicate);
Expand Down Expand Up @@ -83,11 +88,20 @@ public ILBlock(List<ILNode> body)
{
this.Body = body;
}

protected override void AccumulateChildrenRecursiveInto<T> (List<T> list, Func<T, bool> predicate) {
if (this.EntryGoto != null)
this.EntryGoto.AccumulateSelfAndChildrenRecursive(list, predicate);

foreach (ILNode child in this.Body)
child.AccumulateSelfAndChildrenRecursive(list, predicate);
}

public override IEnumerable<ILNode> GetChildren()
{
if (this.EntryGoto != null)
yield return this.EntryGoto;

foreach(ILNode child in this.Body) {
yield return child;
}
Expand All @@ -106,7 +120,12 @@ public class ILBasicBlock: ILNode
{
/// <remarks> Body has to start with a label and end with unconditional control flow </remarks>
public List<ILNode> Body = new List<ILNode>();


protected override void AccumulateChildrenRecursiveInto<T> (List<T> list, Func<T, bool> predicate) {
foreach (var child in this.Body)
child.AccumulateSelfAndChildrenRecursive(list, predicate);
}

public override IEnumerable<ILNode> GetChildren()
{
return this.Body;
Expand Down Expand Up @@ -349,6 +368,11 @@ public ILExpressionPrefix GetPrefix(ILCode code)
}
return null;
}

protected override void AccumulateChildrenRecursiveInto<T> (List<T> list, Func<T, bool> predicate) {
foreach (var child in Arguments)
child.AccumulateSelfAndChildrenRecursive(list, predicate);
}

public override IEnumerable<ILNode> GetChildren()
{
Expand Down

0 comments on commit a870acd

Please sign in to comment.