Browse Source

Optimized the AstNode.Descendants property.

Over 3 times faster than the previous implementation - but still slower than a visitor.
Fastest is a recursive function based on a for-loop ("for (AstNode child = node.FirstChild; child != null; child = child.NextSibling)").
newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
4717de986e
  1. 2
      ICSharpCode.NRefactory.CSharp/Analysis/DefiniteAssignmentAnalysis.cs
  2. 27
      ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  4. 117
      ICSharpCode.NRefactory.ConsistencyCheck/VisitorBenchmark.cs

2
ICSharpCode.NRefactory.CSharp/Analysis/DefiniteAssignmentAnalysis.cs

@ -443,7 +443,7 @@ namespace ICSharpCode.NRefactory.CSharp.Analysis
// the special values are valid as output only, not as input // the special values are valid as output only, not as input
Debug.Assert(data == CleanSpecialValues(data)); Debug.Assert(data == CleanSpecialValues(data));
DefiniteAssignmentStatus status = data; DefiniteAssignmentStatus status = data;
foreach (AstNode child in node.Children) { for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
analysis.analysisCancellationToken.ThrowIfCancellationRequested(); analysis.analysisCancellationToken.ThrowIfCancellationRequested();
Debug.Assert(!(child is Statement)); // statements are visited with the CFG, not with the visitor pattern Debug.Assert(!(child is Statement)); // statements are visited with the CFG, not with the visitor pattern

27
ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs

@ -23,6 +23,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -288,17 +289,31 @@ namespace ICSharpCode.NRefactory.CSharp
/// Gets all descendants of this node (excluding this node itself). /// Gets all descendants of this node (excluding this node itself).
/// </summary> /// </summary>
public IEnumerable<AstNode> Descendants { public IEnumerable<AstNode> Descendants {
get { get { return GetDescendants(false); }
return Utils.TreeTraversal.PreOrder (this.Children, n => n.Children);
}
} }
/// <summary> /// <summary>
/// Gets all descendants of this node (including this node itself). /// Gets all descendants of this node (including this node itself).
/// </summary> /// </summary>
public IEnumerable<AstNode> DescendantsAndSelf { public IEnumerable<AstNode> DescendantsAndSelf {
get { get { return GetDescendants(true); }
return Utils.TreeTraversal.PreOrder (this, n => n.Children); }
IEnumerable<AstNode> GetDescendants(bool includeSelf)
{
if (includeSelf)
yield return this;
Stack<AstNode> nextStack = new Stack<AstNode>();
nextStack.Push(null);
AstNode pos = firstChild;
while (pos != null) {
if (pos.nextSibling != null)
nextStack.Push(pos.nextSibling);
yield return pos;
if (pos.firstChild != null)
pos = pos.firstChild;
else
pos = nextStack.Pop();
} }
} }
@ -322,7 +337,7 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
return Ancestors.OfType<T>().FirstOrDefault(); return Ancestors.OfType<T>().FirstOrDefault();
} }
public AstNodeCollection<T> GetChildrenByRole<T> (Role<T> role) where T : AstNode public AstNodeCollection<T> GetChildrenByRole<T> (Role<T> role) where T : AstNode
{ {
return new AstNodeCollection<T> (this, role); return new AstNodeCollection<T> (this, role);

2
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -3809,7 +3809,7 @@ namespace ICSharpCode.NRefactory.CSharp
var cu = Parse (new StringReader (code), "parsed.cs", lineModifier - 1); var cu = Parse (new StringReader (code), "parsed.cs", lineModifier - 1);
if (cu == null) if (cu == null)
return Enumerable.Empty<EntityDeclaration> (); return Enumerable.Empty<EntityDeclaration> ();
var td = cu.Children.FirstOrDefault () as TypeDeclaration; var td = cu.FirstChild as TypeDeclaration;
if (td != null) { if (td != null) {
var members = td.Members.ToArray(); var members = td.Members.ToArray();
// detach members from parent // detach members from parent

117
ICSharpCode.NRefactory.ConsistencyCheck/VisitorBenchmark.cs

@ -24,31 +24,122 @@ using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.NRefactory.ConsistencyCheck namespace ICSharpCode.NRefactory.ConsistencyCheck
{ {
/// <summary>
/// Determines the fastest way to retrieve a List{IdentifierExpression} of all identifiers
/// in a compilation unit.
/// </summary>
public class VisitorBenchmark public class VisitorBenchmark
{ {
public static void Run(IEnumerable<CompilationUnit> files) public static void Run(IEnumerable<CompilationUnit> files)
{ {
files = files.ToList(); files = files.ToList();
RunTest("DepthFirstAstVisitor", files, cu => cu.AcceptVisitor(new DepthFirst()));
RunTest("DepthFirstAstVisitor<object>", files, cu => cu.AcceptVisitor(new DepthFirst<object>())); RunTest("recursive method using for", files, (cu, list) => WalkTreeFor(cu, list));
RunTest("DepthFirstAstVisitor<int>", files, cu => cu.AcceptVisitor(new DepthFirst<int>())); RunTest("recursive method using foreach", files, (cu, list) => WalkTreeForEach(cu, list));
RunTest("DepthFirstAstVisitor<object, object>", files, cu => cu.AcceptVisitor(new DepthFirst<object, object>(), null)); RunTest("non-recursive loop", files, (cu, list) => WalkTreeNonRecursive(cu, list));
RunTest("DepthFirstAstVisitor<int, int>", files, cu => cu.AcceptVisitor(new DepthFirst<int, int>(), 0)); RunTest("foreach over Descendants.OfType()", files, (cu, list) => {
RunTest("ObservableAstVisitor", files, cu => cu.AcceptVisitor(new ObservableAstVisitor())); foreach (var node in cu.Descendants.OfType<IdentifierExpression>()) {
RunTest("ObservableAstVisitor<object, object>", files, cu => cu.AcceptVisitor(new ObservableAstVisitor<object, object>(), null)); list.Add(node);
RunTest("ObservableAstVisitor<int, int>", files, cu => cu.AcceptVisitor(new ObservableAstVisitor<int, int>(), 0)); }
});
RunTest("DepthFirstAstVisitor", files, (cu, list) => cu.AcceptVisitor(new DepthFirst(list)));
RunTest("DepthFirstAstVisitor<object>", files, (cu, list) => cu.AcceptVisitor(new DepthFirst<object>(list)));
RunTest("DepthFirstAstVisitor<object, object>", files, (cu, list) => cu.AcceptVisitor(new DepthFirst<object, object>(list), null));
RunTest("ObservableAstVisitor", files, (cu, list) => {
var visitor = new ObservableAstVisitor();
visitor.EnterIdentifierExpression += list.Add;
cu.AcceptVisitor(visitor);
});
RunTest("ObservableAstVisitor<object, object>", files, (cu, list) => {
var visitor = new ObservableAstVisitor<object, object>();
visitor.IdentifierExpressionVisited += (id, data) => list.Add(id);
cu.AcceptVisitor(visitor, null);
});
} }
class DepthFirst : DepthFirstAstVisitor {} static void WalkTreeForEach(AstNode node, List<IdentifierExpression> list)
class DepthFirst<T> : DepthFirstAstVisitor<T> {} {
class DepthFirst<T, S> : DepthFirstAstVisitor<T, S> {} foreach (AstNode child in node.Children) {
IdentifierExpression id = child as IdentifierExpression;
if (id != null) {
list.Add(id);
}
WalkTreeForEach(child, list);
}
}
static void WalkTreeFor(AstNode node, List<IdentifierExpression> list)
{
for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
IdentifierExpression id = child as IdentifierExpression;
if (id != null) {
list.Add(id);
}
WalkTreeFor(child, list);
}
}
static void WalkTreeNonRecursive(AstNode root, List<IdentifierExpression> list)
{
AstNode pos = root;
while (pos != null) {
{
IdentifierExpression id = pos as IdentifierExpression;
if (id != null) {
list.Add(id);
}
}
if (pos.FirstChild != null) {
pos = pos.FirstChild;
} else {
pos = pos.GetNextNode();
}
}
}
class DepthFirst : DepthFirstAstVisitor {
readonly List<IdentifierExpression> list;
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
{
list.Add(identifierExpression);
base.VisitIdentifierExpression(identifierExpression);
}
}
class DepthFirst<T> : DepthFirstAstVisitor<T> {
readonly List<IdentifierExpression> list;
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
public override T VisitIdentifierExpression(IdentifierExpression identifierExpression)
{
list.Add(identifierExpression);
return base.VisitIdentifierExpression(identifierExpression);
}
}
class DepthFirst<T, S> : DepthFirstAstVisitor<T, S> {
readonly List<IdentifierExpression> list;
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
public override S VisitIdentifierExpression(IdentifierExpression identifierExpression, T data)
{
list.Add(identifierExpression);
return base.VisitIdentifierExpression(identifierExpression, data);
}
}
static void RunTest(string text, IEnumerable<CompilationUnit> files, Action<CompilationUnit> action) static void RunTest(string text, IEnumerable<CompilationUnit> files, Action<CompilationUnit, List<IdentifierExpression>> action)
{ {
// validation:
var list = new List<IdentifierExpression>();
foreach (var file in files) {
list.Clear();
action(file, list);
if (!list.SequenceEqual(file.Descendants.OfType<IdentifierExpression>()))
throw new InvalidOperationException();
}
Stopwatch w = Stopwatch.StartNew(); Stopwatch w = Stopwatch.StartNew();
foreach (var file in files) { foreach (var file in files) {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
action(file); list.Clear();
action(file, list);
} }
} }
w.Stop(); w.Stop();

Loading…
Cancel
Save