@@ -58,64 +58,43 @@ func ApplyAdvisor(co CanonicalOutline, advisor PlanAdvisor) (CanonicalOutline, e
5858 }, nil
5959}
6060
61- // applyAdvisorMutations recursively walks the outline tree bottom-up.
61+ // applyAdvisorMutations walks the outline tree bottom-up via WalkOutlineBottomUp .
6262// For each node it calls GetMutations on the advisor and applies the returned
6363// mutations in sequence. After each mutation it verifies that the resulting
6464// node's ID matches the original node's ID; a mismatch is a programmer bug.
6565func applyAdvisorMutations (outline Outline , co CanonicalOutline , advisor PlanAdvisor ) (Outline , error ) {
66- // Recurse into children first (bottom-up).
67- if len (outline .SubOutlines ) > 0 {
68- newSubs := make ([]Outline , len (outline .SubOutlines ))
69- for i , sub := range outline .SubOutlines {
70- mutated , err := applyAdvisorMutations (sub , co , advisor )
71- if err != nil {
72- return Outline {}, err
73- }
74- newSubs [i ] = mutated
75- }
76- outline = Outline {
77- Type : outline .Type ,
78- Args : outline .Args ,
79- SubOutlines : newSubs ,
80- ID : outline .ID ,
66+ return WalkOutlineBottomUp (outline , func (node Outline ) (Outline , error ) {
67+ mutations , err := advisor .GetMutations (node , co )
68+ if err != nil {
69+ return Outline {}, err
8170 }
82- }
83-
84- mutations , err := advisor .GetMutations (outline , co )
85- if err != nil {
86- return Outline {}, err
87- }
88-
89- result := outline
90- for _ , fn := range mutations {
91- next := fn (result )
92- if next .ID != result .ID {
93- return Outline {}, spiceerrors .MustBugf (
94- "advisor mutation changed outline node ID from %d to %d; mutations must preserve the root node ID" ,
95- result .ID , next .ID ,
96- )
71+ result := node
72+ for _ , fn := range mutations {
73+ next := fn (result )
74+ if next .ID != result .ID {
75+ return Outline {}, spiceerrors .MustBugf (
76+ "advisor mutation changed outline node ID from %d to %d; mutations must preserve the root node ID" ,
77+ result .ID , next .ID ,
78+ )
79+ }
80+ result = next
9781 }
98- result = next
99- }
100- return result , nil
82+ return result , nil
83+ })
10184}
10285
103- // collectAdvisorHints recursively walks the outline tree and calls GetHints on
104- // the advisor for each node, storing any returned hints in the hints map keyed
105- // by the node's ID.
86+ // collectAdvisorHints walks the outline tree pre-order via WalkOutlinePreOrder,
87+ // calling GetHints on the advisor for each node and storing any returned hints
88+ // in the hints map keyed by the node's ID.
10689func collectAdvisorHints (outline Outline , co CanonicalOutline , advisor PlanAdvisor , hints map [OutlineNodeID ][]Hint ) error {
107- nodeHints , err := advisor .GetHints (outline , co )
108- if err != nil {
109- return err
110- }
111- if len (nodeHints ) > 0 {
112- hints [outline .ID ] = nodeHints
113- }
114-
115- for _ , sub := range outline .SubOutlines {
116- if err := collectAdvisorHints (sub , co , advisor , hints ); err != nil {
90+ return WalkOutlinePreOrder (outline , func (node Outline ) error {
91+ nodeHints , err := advisor .GetHints (node , co )
92+ if err != nil {
11793 return err
11894 }
119- }
120- return nil
95+ if len (nodeHints ) > 0 {
96+ hints [node .ID ] = nodeHints
97+ }
98+ return nil
99+ })
121100}
0 commit comments