From 88e58500a85a0ada729623cd50c3dd343d2fdc33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Thu, 7 Oct 2021 21:26:14 +0200 Subject: [PATCH] Improved caching --- QuestPDF.ReportSample/PerformanceTests.cs | 7 ++++++- .../QuestPDF.ReportSample.csproj | 2 +- QuestPDF/Drawing/DocumentGenerator.cs | 17 +++++++++-------- QuestPDF/Drawing/Proxy/CacheProxy.cs | 5 +++++ QuestPDF/Drawing/Proxy/DebuggingProxy.cs | 3 ++- QuestPDF/Elements/AspectRatio.cs | 2 +- QuestPDF/Elements/Canvas.cs | 2 +- QuestPDF/Elements/Constrained.cs | 2 +- QuestPDF/Elements/Decoration.cs | 2 +- QuestPDF/Elements/EnsureSpace.cs | 2 +- QuestPDF/Elements/Extend.cs | 2 +- QuestPDF/Elements/Image.cs | 2 +- QuestPDF/Elements/Layers.cs | 2 +- QuestPDF/Elements/Padding.cs | 2 +- QuestPDF/Elements/Row.cs | 2 +- QuestPDF/Elements/Scale.cs | 2 +- QuestPDF/Elements/ShowEntire.cs | 2 +- QuestPDF/Elements/ShowOnce.cs | 2 +- QuestPDF/Elements/SimpleRotate.cs | 2 +- QuestPDF/Elements/Stack.cs | 2 +- QuestPDF/Elements/Unconstrained.cs | 2 +- QuestPDF/Infrastructure/ICacheable.cs | 7 +++++++ 22 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 QuestPDF/Infrastructure/ICacheable.cs diff --git a/QuestPDF.ReportSample/PerformanceTests.cs b/QuestPDF.ReportSample/PerformanceTests.cs index 0fd0e85..e3b2fcc 100644 --- a/QuestPDF.ReportSample/PerformanceTests.cs +++ b/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(); + var configuration = ManualConfig + .Create(DefaultConfig.Instance) + .WithOptions(ConfigOptions.DisableOptimizationsValidator); + + BenchmarkRunner.Run(configuration); } [IterationSetup] diff --git a/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj b/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj index a4353d5..c208da1 100644 --- a/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj +++ b/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj @@ -11,7 +11,7 @@ - + diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index d53934c..f116137 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/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; } diff --git a/QuestPDF/Drawing/Proxy/CacheProxy.cs b/QuestPDF/Drawing/Proxy/CacheProxy.cs index 92d6020..415f2cc 100644 --- a/QuestPDF/Drawing/Proxy/CacheProxy.cs +++ b/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) { diff --git a/QuestPDF/Drawing/Proxy/DebuggingProxy.cs b/QuestPDF/Drawing/Proxy/DebuggingProxy.cs index d10bd9b..9adb90f 100644 --- a/QuestPDF/Drawing/Proxy/DebuggingProxy.cs +++ b/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) diff --git a/QuestPDF/Elements/AspectRatio.cs b/QuestPDF/Elements/AspectRatio.cs index fe8a3b5..3f1679f 100644 --- a/QuestPDF/Elements/AspectRatio.cs +++ b/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; diff --git a/QuestPDF/Elements/Canvas.cs b/QuestPDF/Elements/Canvas.cs index f79926a..7394882 100644 --- a/QuestPDF/Elements/Canvas.cs +++ b/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; } diff --git a/QuestPDF/Elements/Constrained.cs b/QuestPDF/Elements/Constrained.cs index 40b7876..7c27eff 100644 --- a/QuestPDF/Elements/Constrained.cs +++ b/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; } diff --git a/QuestPDF/Elements/Decoration.cs b/QuestPDF/Elements/Decoration.cs index c946f4b..1a83e41 100644 --- a/QuestPDF/Elements/Decoration.cs +++ b/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; diff --git a/QuestPDF/Elements/EnsureSpace.cs b/QuestPDF/Elements/EnsureSpace.cs index 879c692..2c710dc 100644 --- a/QuestPDF/Elements/EnsureSpace.cs +++ b/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; diff --git a/QuestPDF/Elements/Extend.cs b/QuestPDF/Elements/Extend.cs index dd8c4d5..929a2f5 100644 --- a/QuestPDF/Elements/Extend.cs +++ b/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; } diff --git a/QuestPDF/Elements/Image.cs b/QuestPDF/Elements/Image.cs index ee8c281..b473f84 100644 --- a/QuestPDF/Elements/Image.cs +++ b/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; } diff --git a/QuestPDF/Elements/Layers.cs b/QuestPDF/Elements/Layers.cs index f44091c..24bc356 100644 --- a/QuestPDF/Elements/Layers.cs +++ b/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 Children { get; set; } = new List(); diff --git a/QuestPDF/Elements/Padding.cs b/QuestPDF/Elements/Padding.cs index 74cca61..9007a87 100644 --- a/QuestPDF/Elements/Padding.cs +++ b/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; } diff --git a/QuestPDF/Elements/Row.cs b/QuestPDF/Elements/Row.cs index f779f6c..7d2b293 100644 --- a/QuestPDF/Elements/Row.cs +++ b/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; } diff --git a/QuestPDF/Elements/Scale.cs b/QuestPDF/Elements/Scale.cs index fc768e8..a8f7b85 100644 --- a/QuestPDF/Elements/Scale.cs +++ b/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; diff --git a/QuestPDF/Elements/ShowEntire.cs b/QuestPDF/Elements/ShowEntire.cs index add80bf..65233e6 100644 --- a/QuestPDF/Elements/ShowEntire.cs +++ b/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) { diff --git a/QuestPDF/Elements/ShowOnce.cs b/QuestPDF/Elements/ShowOnce.cs index 310bbb2..af772ec 100644 --- a/QuestPDF/Elements/ShowOnce.cs +++ b/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; } diff --git a/QuestPDF/Elements/SimpleRotate.cs b/QuestPDF/Elements/SimpleRotate.cs index 3fd9211..08d2b3b 100644 --- a/QuestPDF/Elements/SimpleRotate.cs +++ b/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; diff --git a/QuestPDF/Elements/Stack.cs b/QuestPDF/Elements/Stack.cs index c3315eb..40353e4 100644 --- a/QuestPDF/Elements/Stack.cs +++ b/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; diff --git a/QuestPDF/Elements/Unconstrained.cs b/QuestPDF/Elements/Unconstrained.cs index fe53000..a3834b3 100644 --- a/QuestPDF/Elements/Unconstrained.cs +++ b/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) { diff --git a/QuestPDF/Infrastructure/ICacheable.cs b/QuestPDF/Infrastructure/ICacheable.cs new file mode 100644 index 0000000..ccb66e7 --- /dev/null +++ b/QuestPDF/Infrastructure/ICacheable.cs @@ -0,0 +1,7 @@ +namespace QuestPDF.Infrastructure +{ + public interface ICacheable + { + + } +} \ No newline at end of file