From fcd8b1721bce6a0c18c83f0ec4f323b81eeb5867 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 22 Dec 2021 11:39:48 +0100 Subject: [PATCH] LvlGraph: Speed up TMinXGraph. In StoreAsBest only update nodes that changed. This massively speeds up big graphs with several thousand (including hidden) nodes, were StoreAsBest did always copy all those nodes. --- components/lazcontrols/lvlgraphctrl.pas | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/components/lazcontrols/lvlgraphctrl.pas b/components/lazcontrols/lvlgraphctrl.pas index 9e3ca6a382..d8fd59fc01 100644 --- a/components/lazcontrols/lvlgraphctrl.pas +++ b/components/lazcontrols/lvlgraphctrl.pas @@ -738,6 +738,7 @@ type Graph: TMinXGraph; Index: integer; PrevSameSwitchPair, NextSameSwitchPair: TMinXPair; + NextChangedSinceBestStored: TMinXPair; constructor Create(aLevel: TMinXLevel; aIndex: integer); destructor Destroy; override; procedure UnbindFromSwitchList; @@ -753,11 +754,13 @@ type TMinXGraph = class private FGraphNodeToNode: TPointerToPointerTree; // TLvlGraphNode to TMinXNode + PairsChangedSinceBestStored: TMinXPair; procedure InitPairs; procedure UnbindPairs; procedure BindPairs; function ComputeCrossCount: integer; procedure StoreAsBest(CheckIfBetter: boolean); + procedure StoreAsBest(APair: TMinXPair); function ComputeHighestSwitchDiff(StartAtOld: boolean; IgnorePair: TMinXPair): integer; public Graph: TLvlGraph; @@ -1735,9 +1738,17 @@ var Level: TMinXLevel; n: Integer; begin + if CheckIfBetter then begin // e.g. after Shuffly => a new full StoreAsBest is needed + PairsChangedSinceBestStored := TMinXPair(PtrUInt(-1)); + end; + if CheckIfBetter and (BestCrossCount>=0) and (BestCrossCount=0) and (BestCrossCount nil do begin + with APair.Level do begin + idx := APair.Index; + BestNodes[idx]:=Nodes[idx].GraphNode; + inc(idx); + BestNodes[idx]:=Nodes[idx].GraphNode; + end; + NextPair := APair.NextChangedSinceBestStored; + APair.NextChangedSinceBestStored := nil; + APair := NextPair; + end; +end; + function TMinXGraph.ComputeHighestSwitchDiff(StartAtOld: boolean; IgnorePair: TMinXPair): integer; var @@ -1955,7 +1997,7 @@ begin for j:=0 to length(Node2.InEdges)-1 do UpdateSwitchDiff(Node1.InEdges[i],Node2.InEdges[j]); - StoreAsBest(true); + StoreAsBest(Pair); {$IFDEF CheckMinXGraph} ConsistencyCheck;