Browse Source

Merge pull request #219 from maartenba/mb-perf-2

Reduce creation of new arrays in Queue<T>
Marcin Ziąbek 3 years ago
parent
commit
abe7b2f14c
2 changed files with 17 additions and 2 deletions
  1. 3 1
      QuestPDF/Elements/Table/Table.cs
  2. 14 1
      QuestPDF/Elements/Text/TextBlock.cs

+ 3 - 1
QuestPDF/Elements/Table/Table.cs

@@ -42,7 +42,9 @@ namespace QuestPDF.Elements.Table
 
 
         public void ResetState()
         public void ResetState()
         {
         {
-            Cells.ForEach(x => x.IsRendered = false);
+            foreach (var x in Cells)
+                x.IsRendered = false;
+            
             CurrentRow = 1;
             CurrentRow = 1;
         }
         }
 
 

+ 14 - 1
QuestPDF/Elements/Text/TextBlock.cs

@@ -20,7 +20,20 @@ namespace QuestPDF.Elements.Text
 
 
         public void ResetState()
         public void ResetState()
         {
         {
-            RenderingQueue = new Queue<ITextBlockItem>(Items);
+            // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
+            // Create queue when we don't have one yet, otherwise clear the existing one so it re-uses the internal array under the hood.
+            if (RenderingQueue == null)
+            {
+                RenderingQueue = new Queue<ITextBlockItem>(Items);
+            }
+            else
+            {
+                RenderingQueue.Clear();
+            
+                foreach (var item in Items)
+                    RenderingQueue.Enqueue(item);
+            }
+            
             CurrentElementIndex = 0;
             CurrentElementIndex = 0;
         }
         }