major optimization to multi-path by removing sequence operations at runtime

This commit is contained in:
Nathan Marz 2016-06-23 12:35:47 -04:00
parent 930724b85b
commit b060339573
2 changed files with 38 additions and 20 deletions

View file

@ -706,26 +706,30 @@
(defpathedfn multi-path (defpathedfn multi-path
"A path that branches on multiple paths. For updates, "A path that branches on multiple paths. For updates,
applies updates to the paths in order." applies updates to the paths in order."
[& paths] ([] STAY)
(let [paths-comp (mapv i/comp-paths* (vec paths)) ([path] (i/comp-paths* path))
all-needed (mapv i/num-needed-params paths-comp) ([path1 path2]
idx-deltas (vec (cons 0 (reductions + all-needed))) (let [comp1 (i/comp-paths* path1)
extracted (mapv i/extract-rich-tfns paths-comp) comp2 (i/comp-paths* path2)
sel-info (mapv vector (mapv first extracted) idx-deltas) comp1-needed (i/num-needed-params comp1)
tran-info (mapv vector (mapv second extracted) idx-deltas)] [s1 t1] (i/extract-rich-tfns comp1)
(richnav (reduce + 0 all-needed) [s2 t2] (i/extract-rich-tfns comp2)
(select* [params params-idx vals structure next-fn] ]
(doseqres NONE [[s delta] sel-info] (richnav (+ comp1-needed (i/num-needed-params comp2))
(s params (+ params-idx delta) vals structure next-fn) (select* [params params-idx vals structure next-fn]
)) (let [res1 (s1 params params-idx vals structure next-fn)
(transform* [params params-idx vals structure next-fn] res2 (s2 params (+ params-idx comp1-needed) vals structure next-fn)]
(reduce (if (identical? NONE res2)
(fn [structure [t delta]] res1
(t params (+ params-idx delta) vals structure next-fn) res2
) )))
structure (transform* [params params-idx vals structure next-fn]
tran-info (let [s1 (t1 params params-idx vals structure next-fn)]
))))) (t2 params (+ params-idx comp1-needed) vals s1 next-fn)
)))))
([path1 path2 & paths]
(reduce multi-path (multi-path path1 path2) paths)
))
(defpathedfn stay-then-continue (defpathedfn stay-then-continue
"Navigates to the current element and then navigates via the provided path. "Navigates to the current element and then navigates via the provided path.

View file

@ -1327,3 +1327,17 @@
(is (= 3 (multi-transform (s/terminal-val 3) 2))) (is (= 3 (multi-transform (s/terminal-val 3) 2)))
(is (= 3 (multi-transform [s/VAL (s/terminal-val 3)] 2))) (is (= 3 (multi-transform [s/VAL (s/terminal-val 3)] 2)))
) )
(deftest multi-path-order-test
(is (= 102
(multi-transform
(s/multi-path
[odd? (s/terminal #(* 2 %))]
[even? (s/terminal-val 100)]
[#(= 100 %) (s/terminal inc)]
[#(= 101 %) (s/terminal inc)]
)
1
))))