From 64572f9c6301ddf4ed2921597366ff3ebd3d7c0f Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Thu, 4 Feb 2016 22:14:26 +0100 Subject: [PATCH] LayoutingQueue as LinkedList, perf improvments not fantastish --- UnitTest/NUnitCrowWindow.cs | 2 +- src/GraphicObjects/GraphicObject.cs | 34 ++++++++++++---------- src/LayoutingQueue.cs | 45 +++++++++++++++++++---------- src/LayoutingQueueItem.cs | 3 +- src/OpenTKGameWindow.cs | 2 +- 5 files changed, 53 insertions(+), 33 deletions(-) diff --git a/UnitTest/NUnitCrowWindow.cs b/UnitTest/NUnitCrowWindow.cs index 1ba9a32e..999639cd 100644 --- a/UnitTest/NUnitCrowWindow.cs +++ b/UnitTest/NUnitCrowWindow.cs @@ -149,7 +149,7 @@ namespace Crow invGOList = invGOList.Reverse ().ToArray (); //Debug.WriteLine ("======= Layouting queue start ======="); - while (Interface.LayoutingQueue.Count > 0) { + while (Interface.LayoutingQueue.First != null) { // Stopwatch lqiProcTime = new Stopwatch (); // lqiProcTime.Start (); LayoutingQueueItem lqi = Interface.LayoutingQueue.Dequeue (); diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index f6c9c84d..d96bdf29 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -494,7 +494,7 @@ namespace Crow void deleteLQI(int lt){ LayoutingQueueItem[] lqis = this.RegisteredLQIs.Where (lq => (lt & (int)lq.LayoutType) > 0).ToArray (); for (int i = 0; i < lqis.Length; i++) { - Interface.LayoutingQueue.Remove (lqis [i]); + Interface.LayoutingQueue.Remove (lqis [i].Node); lqis [i].DeleteLayoutableRef(); } } @@ -508,33 +508,37 @@ namespace Crow Debug.WriteLine ("RegisterForLayouting => {1}->{0}", layoutType, this.ToString()); #endif - deleteLQI (layoutType); - if ((layoutType & (int)LayoutingType.Width) > 0) { - if (Bounds.Width == 0) //stretch in parent + deleteLQI (layoutType); + if ((layoutType & (int)LayoutingType.Width) > 0) { + if (Bounds.Width == 0) //stretch in parent Interface.LayoutingQueue.EnqueueAfterParentSizing (LayoutingType.Width, this); - else if (Bounds.Width < 0) //fit + else if (Bounds.Width < 0) //fit Interface.LayoutingQueue.EnqueueBeforeParentSizing (LayoutingType.Width, this); - else - Interface.LayoutingQueue.Insert (0, new LayoutingQueueItem (LayoutingType.Width, this)); + else { + LayoutingQueueItem lqi = new LayoutingQueueItem (LayoutingType.Width, this); + lqi.Node = Interface.LayoutingQueue.AddFirst (lqi); } + } - if ((layoutType & (int)LayoutingType.Height) > 0) { - if (Bounds.Height == 0) //stretch in parent + if ((layoutType & (int)LayoutingType.Height) > 0) { + if (Bounds.Height == 0) //stretch in parent Interface.LayoutingQueue.EnqueueAfterParentSizing (LayoutingType.Height, this); - else if (Bounds.Height < 0) //fit + else if (Bounds.Height < 0) //fit Interface.LayoutingQueue.EnqueueBeforeParentSizing (LayoutingType.Height, this); - else - Interface.LayoutingQueue.Insert (0, new LayoutingQueueItem (LayoutingType.Height, this)); + else{ + LayoutingQueueItem lqi = new LayoutingQueueItem (LayoutingType.Height, this); + lqi.Node = Interface.LayoutingQueue.AddFirst (lqi); } + } - if ((layoutType & (int)LayoutingType.X) > 0) + if ((layoutType & (int)LayoutingType.X) > 0) //for x positionning, sizing of parent and this have to be done Interface.LayoutingQueue.EnqueueAfterThisAndParentSizing (LayoutingType.X, this); - if ((layoutType & (int)LayoutingType.Y) > 0) + if ((layoutType & (int)LayoutingType.Y) > 0) //for x positionning, sizing of parent and this have to be done Interface.LayoutingQueue.EnqueueAfterThisAndParentSizing (LayoutingType.Y, this); - } + } /// trigger dependant sizing component update diff --git a/src/LayoutingQueue.cs b/src/LayoutingQueue.cs index 51ec702e..697c59aa 100644 --- a/src/LayoutingQueue.cs +++ b/src/LayoutingQueue.cs @@ -21,17 +21,19 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Diagnostics; namespace Crow { - public class LayoutingQueue : List + public class LayoutingQueue : LinkedList { public LayoutingQueue () { } public void Enqueue(LayoutingType _lt, ILayoutable _object) { - this.Add (new LayoutingQueueItem (_lt, _object)); + LayoutingQueueItem lqi = new LayoutingQueueItem (_lt, _object); + lqi.Node = this.AddLast (lqi); } LayoutingQueueItem searchLqi(ILayoutable go, LayoutingType lt){ return go.RegisteredLQIs.Where(lq => lq.LayoutType == lt).LastOrDefault(); @@ -42,9 +44,9 @@ namespace Crow LayoutingQueueItem parentLqi = searchLqi (_object.Parent, _lt); if (parentLqi == null) - this.Insert (0, lqi); + lqi.Node = this.AddFirst (lqi); else - this.Insert (this.IndexOf (parentLqi) + 1, lqi); + lqi.Node = this.AddAfter (parentLqi.Node, lqi); } public void EnqueueBeforeParentSizing (LayoutingType _lt, ILayoutable _object) { @@ -52,9 +54,9 @@ namespace Crow LayoutingQueueItem parentLqi = searchLqi (_object.Parent, _lt); if (parentLqi == null) - this.Add (lqi); + lqi.Node = this.AddLast (lqi); else - this.Insert (this.IndexOf (parentLqi), lqi); + lqi.Node = this.AddBefore (parentLqi.Node, lqi); } public void EnqueueAfterThisAndParentSizing (LayoutingType _lt, ILayoutable _object) { @@ -66,25 +68,38 @@ namespace Crow LayoutingQueueItem parentLqi = searchLqi (_object.Parent, sizing); LayoutingQueueItem thisLqi = searchLqi (_object, sizing); - int idx = -1; if (parentLqi == null) { if (thisLqi != null) - idx = this.IndexOf (thisLqi); + lqi.Node = this.AddAfter (thisLqi.Node, lqi); + else + lqi.Node = this.AddLast (lqi); } else { if (thisLqi == null) - idx = this.IndexOf (parentLqi); - else - idx = Math.Max(this.IndexOf (parentLqi), this.IndexOf (thisLqi)); + lqi.Node = this.AddAfter (parentLqi.Node, lqi); + else { + switch (sizing) { + case LayoutingType.Width: + if (_object.Parent.getBounds().Width<0) + lqi.Node = this.AddAfter (parentLqi.Node, lqi); + else + lqi.Node = this.AddAfter (thisLqi.Node, lqi); + break; + case LayoutingType.Height: + if (_object.Parent.getBounds().Height<0) + lqi.Node = this.AddAfter (parentLqi.Node, lqi); + else + lqi.Node = this.AddAfter (thisLqi.Node, lqi); + break; + } + } } - - this.Insert (idx + 1, lqi); } public LayoutingQueueItem Dequeue() { - LayoutingQueueItem tmp = this [0]; + LayoutingQueueItem tmp = this.First.Value; tmp.DeleteLayoutableRef (); - this.RemoveAt (0); + this.RemoveFirst (); return tmp; } } diff --git a/src/LayoutingQueueItem.cs b/src/LayoutingQueueItem.cs index d1cfaeb9..816d1923 100644 --- a/src/LayoutingQueueItem.cs +++ b/src/LayoutingQueueItem.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . using System; using System.Diagnostics; +using System.Collections.Generic; namespace Crow { @@ -37,7 +38,7 @@ namespace Crow public class LayoutingQueueItem { - + public LinkedListNode Node; public ILayoutable GraphicObject; public LayoutingType LayoutType; diff --git a/src/OpenTKGameWindow.cs b/src/OpenTKGameWindow.cs index 6726a87c..fcd08f95 100644 --- a/src/OpenTKGameWindow.cs +++ b/src/OpenTKGameWindow.cs @@ -261,7 +261,7 @@ namespace Crow #endif //Debug.WriteLine ("======= Layouting queue start ======="); - while (Interface.LayoutingQueue.Count > 0) { + while (Interface.LayoutingQueue.First != null) { // Stopwatch lqiProcTime = new Stopwatch (); // lqiProcTime.Start (); LayoutingQueueItem lqi = Interface.LayoutingQueue.Dequeue (); -- 2.47.3