huge speedup to if-path when condition is a single statically known function, big optimization for all transforms that don't use value collection by using identical? instead of empty?

This commit is contained in:
Nathan Marz 2016-06-04 20:49:57 -04:00
parent 94d5d2021a
commit 2d3902f478
3 changed files with 40 additions and 24 deletions

View file

@ -1,7 +1,9 @@
(def VERSION (.trim (slurp "VERSION"))) (def VERSION (.trim (slurp "VERSION")))
(defproject com.rpl/specter VERSION (defproject com.rpl/specter VERSION
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"] ; this prevents JVM from doing optimizations which can remove stack traces from NPE and other exceptions :jvm-opts ["-XX:-OmitStackTraceInFastThrow" ; this prevents JVM from doing optimizations which can remove stack traces from NPE and other exceptions
;"-agentpath:/Applications/YourKit_Java_Profiler_2015_build_15056.app/Contents/Resources/bin/mac/libyjpagent.jnilib"
]
:source-paths ["src/clj"] :source-paths ["src/clj"]
:java-source-paths ["src/java"] :java-source-paths ["src/java"]
:test-paths ["test", "target/test-classes"] :test-paths ["test", "target/test-classes"]

View file

@ -506,20 +506,23 @@
(defpathedfn if-path (defpathedfn if-path
"Like cond-path, but with if semantics." "Like cond-path, but with if semantics."
([cond-p then-path] ([cond-p then-path]
(fixed-pathed-nav [late-cond cond-p (if-path cond-p then-path STOP))
late-then then-path]
(select* [this structure next-fn]
(i/if-select structure next-fn late-cond late-then STOP))
(transform* [this structure next-fn]
(i/if-transform structure next-fn late-cond late-then STOP))))
([cond-p then-path else-path] ([cond-p then-path else-path]
(fixed-pathed-nav [late-cond cond-p (if-let [afn (i/extract-basic-filter-fn cond-p)]
late-then then-path (fixed-pathed-nav [late-then then-path
late-else else-path] late-else else-path]
(select* [this structure next-fn] (select* [this structure next-fn]
(i/if-select structure next-fn late-cond late-then late-else)) (i/if-select structure next-fn afn late-then late-else))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(i/if-transform structure next-fn late-cond late-then late-else))))) (i/if-transform structure next-fn afn late-then late-else)))
(fixed-pathed-nav [late-cond cond-p
late-then then-path
late-else else-path]
(select* [this structure next-fn]
(i/if-select structure next-fn #(i/selected?* late-cond %) late-then late-else))
(transform* [this structure next-fn]
(i/if-transform structure next-fn #(i/selected?* late-cond %) late-then late-else))
))))
(defpathedfn cond-path (defpathedfn cond-path
"Takes in alternating cond-path path cond-path path... "Takes in alternating cond-path path cond-path path...

View file

@ -93,11 +93,11 @@
(fn [params params-idx selector structure] (fn [params params-idx selector structure]
(selector params params-idx [] structure (selector params params-idx [] structure
(fn [_ _ vals structure] (fn [_ _ vals structure]
(if-not (empty? vals) [(conj vals structure)] [structure])))) (if-not (identical? [] vals) [(conj vals structure)] [structure]))))
(fn [params params-idx transformer transform-fn structure] (fn [params params-idx transformer transform-fn structure]
(transformer params params-idx [] structure (transformer params params-idx [] structure
(fn [_ _ vals structure] (fn [_ _ vals structure]
(if (empty? vals) (if (identical? [] vals)
(transform-fn structure) (transform-fn structure)
(apply transform-fn (conj vals structure)))))) (apply transform-fn (conj vals structure))))))
)) ))
@ -757,17 +757,28 @@
(next-fn structure) (next-fn structure)
)) ))
(defn if-select [structure next-fn late-cond late-then late-else] (defn extract-basic-filter-fn [path]
(let [apath (if (empty? (compiled-select* late-cond structure)) (cond (fn? path)
late-else path
late-then)]
(and (coll? path)
(= 1 (count path))
(fn? (first path)))
(first path)
))
(defn if-select [structure next-fn then-tester late-then late-else]
(let [apath (if (then-tester structure)
late-then
late-else)]
(doall (mapcat next-fn (compiled-select* apath structure))) (doall (mapcat next-fn (compiled-select* apath structure)))
)) ))
(defn if-transform [structure next-fn late-cond late-then late-else] (defn if-transform [structure next-fn then-tester late-then late-else]
(let [apath (if (empty? (compiled-select* late-cond structure)) (let [apath (if (then-tester structure)
late-else late-then
late-then)] late-else)]
(compiled-transform* apath next-fn structure) (compiled-transform* apath next-fn structure)
)) ))