Browse Source

Applied some of the optimizations suggested by Kris Vandermotten. #150

pull/170/merge
Daniel Grunwald 14 years ago
parent
commit
508073d6ac
  1. 2
      ICSharpCode.Decompiler/Ast/TextOutputFormatter.cs
  2. 2
      ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs
  3. 10
      ICSharpCode.Decompiler/ILAst/GotoRemoval.cs
  4. 4
      ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs
  5. 2
      ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
  6. 4
      ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs
  7. 4
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs
  8. 2
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs
  9. 4
      NRefactory/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs
  10. 4
      NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

2
ICSharpCode.Decompiler/Ast/TextOutputFormatter.cs

@ -136,7 +136,7 @@ namespace ICSharpCode.Decompiler.Ast
if (ranges != null && ranges.Count > 0) if (ranges != null && ranges.Count > 0)
{ {
// find the ancestor that has method mapping as annotation // find the ancestor that has method mapping as annotation
if (node.Ancestors != null && node.Ancestors.Count() > 0) if (node.Parent != null)
{ {
var n = node.Ancestors.FirstOrDefault(a => a.Annotation<MemberMapping>() != null); var n = node.Ancestors.FirstOrDefault(a => a.Annotation<MemberMapping>() != null);
if (n != null) { if (n != null) {

2
ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs

@ -61,7 +61,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
public override object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data) public override object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
{ {
if (objectCreateExpression.Arguments.Count() == 2) { if (objectCreateExpression.Arguments.Count == 2) {
Expression obj = objectCreateExpression.Arguments.First(); Expression obj = objectCreateExpression.Arguments.First();
Expression func = objectCreateExpression.Arguments.Last(); Expression func = objectCreateExpression.Arguments.Last();
Annotation annotation = func.Annotation<Annotation>(); Annotation annotation = func.Annotation<Annotation>();

10
ICSharpCode.Decompiler/ILAst/GotoRemoval.cs

@ -91,7 +91,7 @@ namespace ICSharpCode.Decompiler.ILAst
} }
} }
var defaultCase = ilSwitch.CaseBlocks.Where(cb => cb.Values == null).SingleOrDefault(); var defaultCase = ilSwitch.CaseBlocks.SingleOrDefault(cb => cb.Values == null);
// If there is no default block, remove empty case blocks // If there is no default block, remove empty case blocks
if (defaultCase == null || (defaultCase.Body.Count == 1 && defaultCase.Body.Single().Match(ILCode.LoopOrSwitchBreak))) { if (defaultCase == null || (defaultCase.Body.Count == 1 && defaultCase.Body.Single().Match(ILCode.LoopOrSwitchBreak))) {
ilSwitch.CaseBlocks.RemoveAll(b => b.Body.Count == 1 && b.Body.Single().Match(ILCode.LoopOrSwitchBreak)); ilSwitch.CaseBlocks.RemoveAll(b => b.Body.Count == 1 && b.Body.Single().Match(ILCode.LoopOrSwitchBreak));
@ -140,14 +140,14 @@ namespace ICSharpCode.Decompiler.ILAst
return true; return true;
} }
ILNode breakBlock = GetParents(gotoExpr).Where(n => n is ILWhileLoop || n is ILSwitch).FirstOrDefault(); ILNode breakBlock = GetParents(gotoExpr).FirstOrDefault(n => n is ILWhileLoop || n is ILSwitch);
if (breakBlock != null && target == Exit(breakBlock, new HashSet<ILNode>() { gotoExpr })) { if (breakBlock != null && target == Exit(breakBlock, new HashSet<ILNode>() { gotoExpr })) {
gotoExpr.Code = ILCode.LoopOrSwitchBreak; gotoExpr.Code = ILCode.LoopOrSwitchBreak;
gotoExpr.Operand = null; gotoExpr.Operand = null;
return true; return true;
} }
ILNode continueBlock = GetParents(gotoExpr).Where(n => n is ILWhileLoop).FirstOrDefault(); ILNode continueBlock = GetParents(gotoExpr).FirstOrDefault(n => n is ILWhileLoop);
if (continueBlock != null && target == Enter(continueBlock, new HashSet<ILNode>() { gotoExpr })) { if (continueBlock != null && target == Enter(continueBlock, new HashSet<ILNode>() { gotoExpr })) {
gotoExpr.Code = ILCode.LoopContinue; gotoExpr.Code = ILCode.LoopContinue;
gotoExpr.Operand = null; gotoExpr.Operand = null;
@ -209,10 +209,10 @@ namespace ICSharpCode.Decompiler.ILAst
} else if (expr.Code == ILCode.Nop) { } else if (expr.Code == ILCode.Nop) {
return Exit(expr, visitedNodes); return Exit(expr, visitedNodes);
} else if (expr.Code == ILCode.LoopOrSwitchBreak) { } else if (expr.Code == ILCode.LoopOrSwitchBreak) {
ILNode breakBlock = GetParents(expr).Where(n => n is ILWhileLoop || n is ILSwitch).First(); ILNode breakBlock = GetParents(expr).First(n => n is ILWhileLoop || n is ILSwitch);
return Exit(breakBlock, new HashSet<ILNode>() { expr }); return Exit(breakBlock, new HashSet<ILNode>() { expr });
} else if (expr.Code == ILCode.LoopContinue) { } else if (expr.Code == ILCode.LoopContinue) {
ILNode continueBlock = GetParents(expr).Where(n => n is ILWhileLoop).First(); ILNode continueBlock = GetParents(expr).First(n => n is ILWhileLoop);
return Enter(continueBlock, new HashSet<ILNode>() { expr }); return Enter(continueBlock, new HashSet<ILNode>() { expr });
} else { } else {
return expr; return expr;

4
ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs

@ -455,7 +455,7 @@ namespace ICSharpCode.Decompiler.ILAst
if (byteCode.StoreTo != null && byteCode.StoreTo.Count > 1) { if (byteCode.StoreTo != null && byteCode.StoreTo.Count > 1) {
var locVars = byteCode.StoreTo; var locVars = byteCode.StoreTo;
// For each of the variables, find the location where it is loaded - there should be preciesly one // For each of the variables, find the location where it is loaded - there should be preciesly one
var loadedBy = locVars.Select(locVar => reachableBody.SelectMany(bc => bc.StackBefore).Where(s => s.LoadFrom == locVar).Single()).ToList(); var loadedBy = locVars.Select(locVar => reachableBody.SelectMany(bc => bc.StackBefore).Single(s => s.LoadFrom == locVar)).ToList();
// We now know that all the variables have a single load, // We now know that all the variables have a single load,
// Let's make sure that they have also a single store - us // Let's make sure that they have also a single store - us
if (loadedBy.All(slot => slot.PushedBy.Length == 1 && slot.PushedBy[0] == byteCode)) { if (loadedBy.All(slot => slot.PushedBy.Length == 1 && slot.PushedBy[0] == byteCode)) {
@ -572,7 +572,7 @@ namespace ICSharpCode.Decompiler.ILAst
Loads = new List<ByteCode>() { load } Loads = new List<ByteCode>() { load }
}); });
} else if (storedBy.Length == 1) { } else if (storedBy.Length == 1) {
VariableInfo newVar = newVars.Where(v => v.Stores.Contains(storedBy[0])).Single(); VariableInfo newVar = newVars.Single(v => v.Stores.Contains(storedBy[0]));
newVar.Loads.Add(load); newVar.Loads.Add(load);
} else { } else {
List<VariableInfo> mergeVars = newVars.Where(v => v.Stores.Union(storedBy).Any()).ToList(); List<VariableInfo> mergeVars = newVars.Where(v => v.Stores.Union(storedBy).Any()).ToList();

2
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

@ -398,7 +398,7 @@ namespace ICSharpCode.Decompiler.ILAst
lastNode.IsUnconditionalControlFlow()) lastNode.IsUnconditionalControlFlow())
{ {
// Try to reuse the label // Try to reuse the label
ILLabel label = currNode is ILLabel ? ((ILLabel)currNode) : new ILLabel() { Name = "Block_" + (nextLabelIndex++) }; ILLabel label = currNode as ILLabel ?? new ILLabel() { Name = "Block_" + (nextLabelIndex++).ToString() };
// Terminate the last block // Terminate the last block
if (!lastNode.IsUnconditionalControlFlow()) { if (!lastNode.IsUnconditionalControlFlow()) {

4
ICSharpCode.Decompiler/ILAst/LoopsAndConditions.cs

@ -31,7 +31,7 @@ namespace ICSharpCode.Decompiler.ILAst
{ {
Dictionary<ILLabel, ControlFlowNode> labelToCfNode = new Dictionary<ILLabel, ControlFlowNode>(); Dictionary<ILLabel, ControlFlowNode> labelToCfNode = new Dictionary<ILLabel, ControlFlowNode>();
DecompilerContext context; readonly DecompilerContext context;
uint nextLabelIndex = 0; uint nextLabelIndex = 0;
@ -286,7 +286,7 @@ namespace ICSharpCode.Decompiler.ILAst
ILLabel condLabel = caseLabels[i]; ILLabel condLabel = caseLabels[i];
// Find or create new case block // Find or create new case block
ILSwitch.CaseBlock caseBlock = ilSwitch.CaseBlocks.Where(b => b.EntryGoto.Operand == condLabel).FirstOrDefault(); ILSwitch.CaseBlock caseBlock = ilSwitch.CaseBlocks.FirstOrDefault(b => b.EntryGoto.Operand == condLabel);
if (caseBlock == null) { if (caseBlock == null) {
caseBlock = new ILSwitch.CaseBlock() { caseBlock = new ILSwitch.CaseBlock() {
Values = new List<int>(), Values = new List<int>(),

4
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/ComposedType.cs

@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp
public int PointerRank { public int PointerRank {
get { get {
return GetChildrenByRole(PointerRole).Count(); return GetChildrenByRole(PointerRole).Count;
} }
set { set {
if (value < 0) if (value < 0)
@ -141,7 +141,7 @@ namespace ICSharpCode.NRefactory.CSharp
} }
public int Dimensions { public int Dimensions {
get { return 1 + GetChildrenByRole(Roles.Comma).Count(); } get { return 1 + GetChildrenByRole(Roles.Comma).Count; }
set { set {
int d = this.Dimensions; int d = this.Dimensions;
while (d > value) { while (d > value) {

2
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs

@ -96,7 +96,7 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
if (name == null) if (name == null)
throw new ArgumentNullException("name"); throw new ArgumentNullException("name");
IsVerbatim = name.StartsWith ("@"); IsVerbatim = name.Length > 0 && name[0] == '@';
this.Name = IsVerbatim ? name.Substring (1) : name; this.Name = IsVerbatim ? name.Substring (1) : name;
this.startLocation = location; this.startLocation = location;
} }

4
NRefactory/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs

@ -263,7 +263,7 @@ namespace ICSharpCode.NRefactory.CSharp
#region Fields #region Fields
public override IEntity VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data) public override IEntity VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data)
{ {
bool isSingleField = fieldDeclaration.Variables.Count() == 1; bool isSingleField = fieldDeclaration.Variables.Count == 1;
Modifiers modifiers = fieldDeclaration.Modifiers; Modifiers modifiers = fieldDeclaration.Modifiers;
DefaultField field = null; DefaultField field = null;
foreach (VariableInitializer vi in fieldDeclaration.Variables) { foreach (VariableInitializer vi in fieldDeclaration.Variables) {
@ -476,7 +476,7 @@ namespace ICSharpCode.NRefactory.CSharp
#region Events #region Events
public override IEntity VisitEventDeclaration(EventDeclaration eventDeclaration, object data) public override IEntity VisitEventDeclaration(EventDeclaration eventDeclaration, object data)
{ {
bool isSingleEvent = eventDeclaration.Variables.Count() == 1; bool isSingleEvent = eventDeclaration.Variables.Count == 1;
Modifiers modifiers = eventDeclaration.Modifiers; Modifiers modifiers = eventDeclaration.Modifiers;
DefaultEvent ev = null; DefaultEvent ev = null;
foreach (VariableInitializer vi in eventDeclaration.Variables) { foreach (VariableInitializer vi in eventDeclaration.Variables) {

4
NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

@ -241,7 +241,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
ResolveResult VisitFieldOrEventDeclaration(AttributedNode fieldOrEventDeclaration) ResolveResult VisitFieldOrEventDeclaration(AttributedNode fieldOrEventDeclaration)
{ {
int initializerCount = fieldOrEventDeclaration.GetChildrenByRole(FieldDeclaration.Roles.Variable).Count(); int initializerCount = fieldOrEventDeclaration.GetChildrenByRole(FieldDeclaration.Roles.Variable).Count;
ResolveResult result = null; ResolveResult result = null;
for (AstNode node = fieldOrEventDeclaration.FirstChild; node != null; node = node.NextSibling) { for (AstNode node = fieldOrEventDeclaration.FirstChild; node != null; node = node.NextSibling) {
if (node.Role == FieldDeclaration.Roles.Variable) { if (node.Role == FieldDeclaration.Roles.Variable) {
@ -939,7 +939,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
firstInitializer != null ? firstInitializer.Initializer : null, firstInitializer != null ? firstInitializer.Initializer : null,
false); false);
int initializerCount = variableDeclarationStatement.Variables.Count(); int initializerCount = variableDeclarationStatement.Variables.Count;
ResolveResult result = null; ResolveResult result = null;
for (AstNode node = variableDeclarationStatement.FirstChild; node != null; node = node.NextSibling) { for (AstNode node = variableDeclarationStatement.FirstChild; node != null; node = node.NextSibling) {
if (node.Role == FieldDeclaration.Roles.Variable) { if (node.Role == FieldDeclaration.Roles.Variable) {

Loading…
Cancel
Save