summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-12-12 10:13:41 (GMT)
committerGitHub <noreply@github.com>2016-12-12 10:13:41 (GMT)
commit4a86f9481ae4902cc3a0048050c60e8645eb56b2 (patch)
tree8449c23716622bcb743e2a05ccfc23aab1416988
parent22bdca32339838702920802e49465264fcccd48b (diff)
downloadxamarin-forms-4a86f9481ae4902cc3a0048050c60e8645eb56b2.zip
xamarin-forms-4a86f9481ae4902cc3a0048050c60e8645eb56b2.tar.gz
xamarin-forms-4a86f9481ae4902cc3a0048050c60e8645eb56b2.tar.bz2
[XamlC] optimize Datatemplate IL as well (#612)
-rw-r--r--Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs40
-rw-r--r--Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs2
-rw-r--r--Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj1
-rw-r--r--Xamarin.Forms.Build.Tasks/XamlCTask.cs25
4 files changed, 44 insertions, 24 deletions
diff --git a/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs b/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs
new file mode 100644
index 0000000..f5bdc85
--- /dev/null
+++ b/Xamarin.Forms.Build.Tasks/MethodBodyExtensions.cs
@@ -0,0 +1,40 @@
+using Mono.Cecil.Cil;
+using Mono.Cecil.Rocks;
+using System;
+
+namespace Xamarin.Forms.Build.Tasks
+{
+ static class MethodBodyExtensions
+ {
+ public static void Optimize(this MethodBody self)
+ {
+ if (self == null)
+ throw new ArgumentNullException(nameof(self));
+
+ self.OptimizeLongs();
+ self.OptimizeMacros();
+ }
+
+ static void ExpandMacro(Instruction instruction, OpCode opcode, object operand)
+ {
+ instruction.OpCode = opcode;
+ instruction.Operand = operand;
+ }
+
+ //this can be removed if/when https://github.com/jbevain/cecil/pull/307 is released in a nuget we consume
+ static void OptimizeLongs(this MethodBody self)
+ {
+ var method = self.Method;
+ for (var i = 0; i < self.Instructions.Count; i++) {
+ var instruction = self.Instructions[i];
+ if (instruction.OpCode.Code != Code.Ldc_I8)
+ continue;
+ var l = (long)instruction.Operand;
+ if (l < int.MinValue || l > int.MaxValue)
+ continue;
+ ExpandMacro(instruction, OpCodes.Ldc_I4, unchecked((int)l));
+ self.Instructions.Insert(++i, Instruction.Create(OpCodes.Conv_I8));
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
index 35316f2..67c345f 100644
--- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
+++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
@@ -1161,6 +1161,8 @@ namespace Xamarin.Forms.Build.Tasks
module.Import(typeof (IDataTemplate)).Resolve().Properties.First(p => p.Name == "LoadTemplate").SetMethod;
#pragma warning restore 0612
parentContext.IL.Emit(OpCodes.Callvirt, module.Import(propertySetter));
+
+ loadTemplate.Body.Optimize();
}
}
diff --git a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
index 1ccace2..589d006 100644
--- a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
+++ b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj
@@ -103,6 +103,7 @@
<Compile Include="CompiledMarkupExtensions\ArrayExtension.cs" />
<Compile Include="CompiledConverters\ConstraintTypeConverter.cs" />
<Compile Include="CompiledConverters\ThicknessTypeConverter.cs" />
+ <Compile Include="MethodBodyExtensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
diff --git a/Xamarin.Forms.Build.Tasks/XamlCTask.cs b/Xamarin.Forms.Build.Tasks/XamlCTask.cs
index 5127efd..4e2029c 100644
--- a/Xamarin.Forms.Build.Tasks/XamlCTask.cs
+++ b/Xamarin.Forms.Build.Tasks/XamlCTask.cs
@@ -185,8 +185,7 @@ namespace Xamarin.Forms.Build.Tasks
if (OptimizeIL)
{
Logger.LogString(2, " Optimizing IL... ");
- OptimizeLongs(initComp.Body);
- initComp.Body.OptimizeMacros();
+ initComp.Body.Optimize();
Logger.LogLine(2, "done");
}
@@ -237,28 +236,6 @@ namespace Xamarin.Forms.Build.Tasks
return success;
}
- static void ExpandMacro(Instruction instruction, OpCode opcode, object operand)
- {
- instruction.OpCode = opcode;
- instruction.Operand = operand;
- }
-
- //this can be removed if/when https://github.com/jbevain/cecil/pull/307 is released in a nuget we consume
- static void OptimizeLongs(MethodBody self)
- {
- var method = self.Method;
- for (var i = 0; i < self.Instructions.Count; i++) {
- var instruction = self.Instructions[i];
- if (instruction.OpCode.Code != Code.Ldc_I8)
- continue;
- var l = (long)instruction.Operand;
- if (l < int.MinValue || l > int.MaxValue)
- continue;
- ExpandMacro(instruction, OpCodes.Ldc_I4, unchecked((int)l));
- self.Instructions.Insert(++i, Instruction.Create(OpCodes.Conv_I8));
- }
- }
-
bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception)
{
try {