Browse Source

Improved caching

Marcin Ziąbek 4 years ago
parent
commit
88e58500a8

+ 6 - 1
QuestPDF.ReportSample/PerformanceTests.cs

@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Configs;
 using BenchmarkDotNet.Engines;
 using BenchmarkDotNet.Running;
 using Microsoft.VisualStudio.TestPlatform.TestHost;
@@ -20,7 +21,11 @@ namespace QuestPDF.ReportSample
         [Test]
         public void Run()
         {
-            BenchmarkRunner.Run<PerformanceTests>();
+            var configuration = ManualConfig
+                .Create(DefaultConfig.Instance)
+                .WithOptions(ConfigOptions.DisableOptimizationsValidator);
+            
+            BenchmarkRunner.Run<PerformanceTests>(configuration);
         }
         
         [IterationSetup]

+ 1 - 1
QuestPDF.ReportSample/QuestPDF.ReportSample.csproj

@@ -11,7 +11,7 @@
         <PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
         <PackageReference Include="nunit" Version="3.13.2" />
         <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
-        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
         <PackageReference Include="SkiaSharp" Version="2.80.3" />
     </ItemGroup>
 

+ 9 - 8
QuestPDF/Drawing/DocumentGenerator.cs

@@ -112,20 +112,21 @@ namespace QuestPDF.Drawing
 
         private static void ApplyCaching(Container content)
         {
-            content.HandleVisitor(x => x.CreateProxy(y => new CacheProxy
+            content.HandleVisitor(x =>
             {
-                Child = y
-            }));
+                if (x is ICacheable)
+                    x.CreateProxy(y => new CacheProxy(y));
+            });
         }
-        
+
         private static DebuggingState ApplyDebugging(Container content)
         {
             var debuggingState = new DebuggingState();
-            
-            content.HandleVisitor(x => x.CreateProxy(y => new DebuggingProxy(debuggingState)
+
+            content.HandleVisitor(x =>
             {
-                Child = y
-            }));
+                x.CreateProxy(y => new DebuggingProxy(debuggingState, x));
+            });
 
             return debuggingState;
         }

+ 5 - 0
QuestPDF/Drawing/Proxy/CacheProxy.cs

@@ -7,6 +7,11 @@ namespace QuestPDF.Drawing.Proxy
     {
         public Size? AvailableSpace { get; set; }
         public SpacePlan? MeasurementResult { get; set; }
+
+        public CacheProxy(Element child)
+        {
+            Child = child;
+        }
         
         internal override SpacePlan Measure(Size availableSpace)
         {

+ 2 - 1
QuestPDF/Drawing/Proxy/DebuggingProxy.cs

@@ -6,9 +6,10 @@ namespace QuestPDF.Drawing.Proxy
     {
         private DebuggingState DebuggingState { get; }
 
-        public DebuggingProxy(DebuggingState debuggingState)
+        public DebuggingProxy(DebuggingState debuggingState, Element child)
         {
             DebuggingState = debuggingState;
+            Child = child;
         }
         
         internal override SpacePlan Measure(Size availableSpace)

+ 1 - 1
QuestPDF/Elements/AspectRatio.cs

@@ -4,7 +4,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class AspectRatio : ContainerElement
+    internal class AspectRatio : ContainerElement, ICacheable
     {
         public float Ratio { get; set; } = 1;
         public AspectRatioOption Option { get; set; } = AspectRatioOption.FitWidth;

+ 1 - 1
QuestPDF/Elements/Canvas.cs

@@ -6,7 +6,7 @@ namespace QuestPDF.Elements
 {
     public delegate void DrawOnCanvas(SKCanvas canvas, Size availableSpace);
     
-    internal class Canvas : Element
+    internal class Canvas : Element, ICacheable
     {
         public DrawOnCanvas Handler { get; set; }
         

+ 1 - 1
QuestPDF/Elements/Constrained.cs

@@ -5,7 +5,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class Constrained : ContainerElement
+    internal class Constrained : ContainerElement, ICacheable
     {
         public float? MinWidth { get; set; }
         public float? MaxWidth { get; set; }

+ 1 - 1
QuestPDF/Elements/Decoration.cs

@@ -11,7 +11,7 @@ namespace QuestPDF.Elements
         Append
     }
     
-    internal class BinaryDecoration : Element
+    internal class BinaryDecoration : Element, ICacheable
     {
         public Element DecorationElement { get; set; } = Empty.Instance;
         public Element ContentElement { get; set; } = Empty.Instance;

+ 1 - 1
QuestPDF/Elements/EnsureSpace.cs

@@ -3,7 +3,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class EnsureSpace : ContainerElement
+    internal class EnsureSpace : ContainerElement, ICacheable
     {
         public const float DefaultMinHeight = 150;
         public float MinHeight { get; set; } = DefaultMinHeight;

+ 1 - 1
QuestPDF/Elements/Extend.cs

@@ -4,7 +4,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class Extend : ContainerElement
+    internal class Extend : ContainerElement, ICacheable
     {
         public bool ExtendVertical { get; set; }
         public bool ExtendHorizontal { get; set; }

+ 1 - 1
QuestPDF/Elements/Image.cs

@@ -4,7 +4,7 @@ using SkiaSharp;
 
 namespace QuestPDF.Elements
 {
-    internal class Image : Element
+    internal class Image : Element, ICacheable
     {
         public SKImage? InternalImage { get; set; }
 

+ 1 - 1
QuestPDF/Elements/Layers.cs

@@ -11,7 +11,7 @@ namespace QuestPDF.Elements
         public bool IsPrimary { get; set; }
     }
     
-    internal class Layers : Element
+    internal class Layers : Element, ICacheable
     {
         public List<Layer> Children { get; set; } = new List<Layer>();
         

+ 1 - 1
QuestPDF/Elements/Padding.cs

@@ -4,7 +4,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class Padding : ContainerElement
+    internal class Padding : ContainerElement, ICacheable
     {
         public float Top { get; set; }
         public float Right { get; set; }

+ 1 - 1
QuestPDF/Elements/Row.cs

@@ -34,7 +34,7 @@ namespace QuestPDF.Elements
         }
     }
     
-    internal class BinaryRow : Element
+    internal class BinaryRow : Element, ICacheable
     {
         internal Element Left { get; set; }
         internal Element Right { get; set; }

+ 1 - 1
QuestPDF/Elements/Scale.cs

@@ -4,7 +4,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class Scale : ContainerElement
+    internal class Scale : ContainerElement, ICacheable
     {
         public float ScaleX { get; set; } = 1;
         public float ScaleY { get; set; } = 1;

+ 1 - 1
QuestPDF/Elements/ShowEntire.cs

@@ -3,7 +3,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class ShowEntire : ContainerElement
+    internal class ShowEntire : ContainerElement, ICacheable
     {
         internal override SpacePlan Measure(Size availableSpace)
         {

+ 1 - 1
QuestPDF/Elements/ShowOnce.cs

@@ -3,7 +3,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class ShowOnce : ContainerElement, IStateResettable
+    internal class ShowOnce : ContainerElement, IStateResettable, ICacheable
     {
         private bool IsRendered { get; set; }
 

+ 1 - 1
QuestPDF/Elements/SimpleRotate.cs

@@ -4,7 +4,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class SimpleRotate : ContainerElement
+    internal class SimpleRotate : ContainerElement, ICacheable
     {
         public int TurnCount { get; set; }
         public int NormalizedTurnCount => (TurnCount % 4 + 4) % 4;

+ 1 - 1
QuestPDF/Elements/Stack.cs

@@ -9,7 +9,7 @@ using IContainer = QuestPDF.Infrastructure.IContainer;
 
 namespace QuestPDF.Elements
 {
-    internal class BinaryStack : Element, IStateResettable
+    internal class BinaryStack : Element, IStateResettable, ICacheable
     {
         internal Element First { get; set; } = Empty.Instance;
         internal Element Second { get; set; } = Empty.Instance;

+ 1 - 1
QuestPDF/Elements/Unconstrained.cs

@@ -3,7 +3,7 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal class Unconstrained : ContainerElement
+    internal class Unconstrained : ContainerElement, ICacheable
     {
         internal override SpacePlan Measure(Size availableSpace)
         {

+ 7 - 0
QuestPDF/Infrastructure/ICacheable.cs

@@ -0,0 +1,7 @@
+namespace QuestPDF.Infrastructure
+{
+    public interface ICacheable
+    {
+        
+    }
+}