diff --git a/scripts/benchmarks.clj b/scripts/benchmarks.clj index e057a92..5dcc561 100644 --- a/scripts/benchmarks.clj +++ b/scripts/benchmarks.clj @@ -90,6 +90,7 @@ (let [data [1 2 3 4 5 6 7 8 9 10]] (run-benchmark "filter a sequence" 1000000 (doall (filter even? data)) + (filterv even? data) (select [ALL even?] data) (select-any (filterer even?) data) )) diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index 341612d..c53766e 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -7,7 +7,7 @@ ) (:use [com.rpl.specter.protocols :only [select* transform* collect-val]] - [com.rpl.specter.util-macros :only [doseqres]] + #+clj [com.rpl.specter.util-macros :only [doseqres]] ) (:require [com.rpl.specter.protocols :as p] [clojure.walk :as walk] @@ -22,12 +22,6 @@ (def NONE ::NONE) -#+clj -(def kw-identical? identical?) - -#+cljs -(def kw-identical? keyword-identical?) - (defn spy [e] (println "SPY:") (println (pr-str e)) @@ -590,8 +584,8 @@ ;; amazingly doing this as a macro shows a big effect in the ;; benchmark for getting a value out of a nested map -(defmacro compiled-traverse* - [path result-fn structure] +#+clj +(defmacro compiled-traverse* [path result-fn structure] `(let [tfns# (transform-fns-field ~path) ex# (executors-field tfns#)] ((traverse-executor-field ex#) @@ -602,6 +596,19 @@ ~structure) )) +#+cljs +(defn compiled-traverse* [path result-fn structure] + (let [tfns (transform-fns-field path) + ex (executors-field tfns)] + ((traverse-executor-field ex) + (params-field path) + (params-idx-field path) + (selector-field tfns) + result-fn + structure) + )) + + (defn compiled-select* [path structure] (let [res (mutable-cell (transient [])) result-fn (fn [structure] @@ -613,44 +620,44 @@ )) (defn compiled-select-one* [path structure] - (let [res (mutable-cell ::NOTHING) + (let [res (mutable-cell NONE) result-fn (fn [structure] (let [curr (get-cell res)] - (if (kw-identical? curr ::NOTHING) + (if (identical? curr NONE) (set-cell! res structure) (throw-illegal "More than one element found in structure: " structure) )))] (compiled-traverse* path result-fn structure) (let [ret (get-cell res)] - (if (kw-identical? ret ::NOTHING) + (if (identical? ret NONE) nil ret )))) (defn compiled-select-one!* [path structure] - (let [res (mutable-cell ::NOTHING) + (let [res (mutable-cell NONE) result-fn (fn [structure] (let [curr (get-cell res)] - (if (kw-identical? curr ::NOTHING) + (if (identical? curr NONE) (set-cell! res structure) (throw-illegal "More than one element found in structure: " structure) )))] (compiled-traverse* path result-fn structure) (let [ret (get-cell res)] - (if (kw-identical? ::NOTHING ret) + (if (identical? NONE ret) (throw-illegal "Found no elements for select-one! on " structure)) ret ))) (defn compiled-select-first* [path structure] - (let [res (mutable-cell ::NOTHING) + (let [res (mutable-cell NONE) result-fn (fn [structure] (let [curr (get-cell res)] - (if (kw-identical? curr ::NOTHING) + (if (identical? curr NONE) (set-cell! res structure))))] (compiled-traverse* path result-fn structure) (let [ret (get-cell res)] - (if (kw-identical? ret ::NOTHING) + (if (identical? ret NONE) nil ret )))) @@ -669,7 +676,7 @@ [compiled-path structure] (->> structure (compiled-select-any* compiled-path) - (kw-identical? NONE))) + (identical? NONE))) (defn selected?* [compiled-path structure] @@ -680,7 +687,7 @@ walker (fn this [structure] (if (pred structure) (let [r (continue-fn structure)] - (if-not (kw-identical? r NONE) + (if-not (identical? r NONE) (set-cell! ret r)) r ) diff --git a/src/clj/com/rpl/specter/util_macros.clj b/src/clj/com/rpl/specter/util_macros.clj index 3a015e5..b4726db 100644 --- a/src/clj/com/rpl/specter/util_macros.clj +++ b/src/clj/com/rpl/specter/util_macros.clj @@ -1,15 +1,13 @@ (ns com.rpl.specter.util-macros) -(defmacro doseqres [backup-res-kw [n aseq] & body] - (let [platform (if (contains? &env :locals) :cljs :clj) - idfn (if (= platform :clj) 'identical? 'keyword-identical?)] - `(reduce - (fn [curr# ~n] - (let [ret# (do ~@body)] - (if (~idfn ret# ~backup-res-kw) - curr# - ret# - ))) - ~backup-res-kw - ~aseq - ))) +(defmacro doseqres [backup-res [n aseq] & body] + `(reduce + (fn [curr# ~n] + (let [ret# (do ~@body)] + (if (identical? ret# ~backup-res) + curr# + ret# + ))) + ~backup-res + ~aseq + ))