Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions bench/datascript/bench/bench.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns datascript.bench.bench
(:require
#?(:clj [clj-async-profiler.core :as clj-async-profiler]))
#?(:cljd nil :clj [clj-async-profiler.core :as clj-async-profiler]))
#?(:cljs (:require-macros datascript.bench.bench)))

; Measure time
Expand All @@ -12,7 +12,8 @@
(def ^:dynamic *profile* false)

#?(:cljs (defn ^number now [] (js/performance.now))
:clj (defn now ^double [] (/ (System/nanoTime) 1000000.0)))
:cljd (defn now [] (/ (.-microsecondsSinceEpoch (DateTime/now)) 1000.0))
:clj (defn now ^double [] (/ (System/nanoTime) 1000000.0)))

#?(:clj
(defmacro dotime
Expand All @@ -27,15 +28,13 @@
(recur (+ *batch* iterations#))
(double (/ (- now# start-t#) iterations#))))))))

(defn- if-cljs [env then else]
(if (:ns env) then else))

(defn median [xs]
(nth (sort xs) (quot (count xs) 2)))

(defn to-fixed [n places]
#?(:cljs (.toFixed n places)
:clj (String/format java.util.Locale/ROOT (str "%." places "f") (to-array [(double n)]))))
:cljd (.toStringAsFixed (double n) places)
:clj (String/format java.util.Locale/ROOT (str "%." places "f") (to-array [(double n)]))))

(defn round [n]
(cond
Expand All @@ -60,7 +59,7 @@
(let [[title body] (if (string? title)
[title body]
["unknown-bench" (cons title body)])]
(if-cljs &env
(if #?(:cljd/clj-host true :default (:ns &env))
`(let [_# (dotime *warmup-ms* ~@body)
times# (mapv
(fn [_#]
Expand Down Expand Up @@ -115,7 +114,7 @@
└ 15"
[id depth width]
(if (pos? depth)
(let [children (map #(+ (* id width) %) (range width))]
(let [children (mapv #(+ (* id width) %) (range width))]
(cons
(assoc (random-man)
:db/id (str id)
Expand Down
32 changes: 23 additions & 9 deletions bench/datascript/bench/datascript.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[datascript.core :as d]
[datascript.bench.bench :as bench]
#?(:clj [jsonista.core :as jsonista])))
#?@(:cljd [cljd.reader :as edn-reader]
:clj [jsonista.core :as jsonista])))

#?(:cljs (enable-console-print!))

Expand Down Expand Up @@ -54,7 +55,7 @@
(defn bench-init []
(let [datoms (into []
(for [p @bench/*people20k
:let [id (#?(:clj Integer/parseInt :cljs js/parseInt) (:db/id p))]
:let [id (#?(:clj Integer/parseInt :cljs js/parseInt :cljd int/parse) (:db/id p))]
[k v] p
:when (not= k :db/id)]
(d/datom id k v)))]
Expand Down Expand Up @@ -219,13 +220,22 @@
(com.fasterxml.jackson.databind.ObjectMapper.)))

(defn bench-freeze []
(bench/bench
(-> @*serialize-db (d/serializable) #?(:clj (jsonista/write-value-as-string mapper) :cljs js/JSON.stringify))))
#?(:cljd
(bench/bench
(-> @*serialize-db d/serializable pr-str))
:default
(bench/bench
(-> @*serialize-db (d/serializable) #?(:clj (jsonista/write-value-as-string mapper) :cljs js/JSON.stringify)))))

(defn bench-thaw []
(let [json (-> @*serialize-db (d/serializable) #?(:clj (jsonista/write-value-as-string mapper) :cljs js/JSON.stringify))]
(bench/bench
(-> json #?(:clj (jsonista/read-value mapper) :cljs js/JSON.parse) d/from-serializable))))
#?(:cljd
(let [edn (-> @*serialize-db d/serializable pr-str)]
(bench/bench
(-> edn edn-reader/read-string d/from-serializable)))
:default
(let [json (-> @*serialize-db (d/serializable) #?(:clj (jsonista/write-value-as-string mapper) :cljs js/JSON.stringify))]
(bench/bench
(-> json #?(:clj (jsonista/read-value mapper) :cljs js/JSON.parse) d/from-serializable)))))

(def benches
{"add-1" bench-add-1
Expand Down Expand Up @@ -261,10 +271,10 @@
"clj -A:bench -M -m datascript.bench.datascript [--profile] (add-1 | add-5 | ...)*"
[& args]
(let [args (or args ())
profile? (.contains ^java.util.List args "--profile")
profile? (boolean (some #{"--profile"} args))
args (remove #{"--profile"} args)
names (or (not-empty args) (sort (keys benches)))
_ (apply println #?(:clj "CLJ:" :cljs "CLJS:") names)
_ (apply println #?(:clj "CLJ:" :cljs "CLJS:" :cljd "CLJD:") names)
longest (last (sort-by count names))]
(binding [bench/*profile* profile?]
(doseq [name names
Expand All @@ -279,6 +289,10 @@
" " (or file ""))))))
#?(:clj (shutdown-agents))))

#?(:cljd
(defn ^:export main [args]
(apply -main args)))

(comment
(require 'datascript.bench.datascript :reload-all)

Expand Down
3 changes: 2 additions & 1 deletion script/bench_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cd "`dirname $0`/.."

./script/bench_clj.sh $@
./script/bench_cljs.sh $@
./script/bench_datomic.sh $@
./script/bench_datomic.sh $@
./script/bench_cljd.sh $@
25 changes: 25 additions & 0 deletions script/bench_cljd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#!/bin/bash
set -o errexit -o nounset -o pipefail
cd "`dirname $0`/.."
PATH="$PWD/.fvm/flutter_sdk/bin:$PATH"

rm -rf tmp/cljdbench
mkdir -p tmp/cljdbench/src/cljd
cat > tmp/cljdbench/deps.edn <<EOF
{:paths ["src" "../../bench"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
tensegritics/clojuredart {:git/url "https://github.com/tensegritics/ClojureDart.git"
:sha "c56e2dc1bef1841a7ce58ce2546946dd0be414e1"}
io.github.wevre/transit-cljd {:git/tag "v0.8.36"
:git/sha "d9541d0"}
metosin/jsonista {:mvn/version "0.3.3"}
datascript/datascript {:local/root "../../"}}
:cljd/opts {:main datascript.bench.datascript
:kind :dart}}
EOF

cd tmp/cljdbench
clojure -M -m cljd.build init
clojure -M -m cljd.build compile datascript.bench.datascript
dart run bin/cljdbench.dart $@
3 changes: 2 additions & 1 deletion script/test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cd "`dirname $0`/.."
EXIT=0
./script/test_clj.sh || EXIT=$((EXIT + $?))
./script/test_cljs.sh || EXIT=$((EXIT + $?))
./script/test_cljd.sh || EXIT=$((EXIT + $?))
./script/test_js.sh || EXIT=$((EXIT + $?))
./script/test_datomic.sh || EXIT=$((EXIT + $?))
exit $EXIT
exit $EXIT
24 changes: 24 additions & 0 deletions script/test_cljd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
set -o errexit -o nounset -o pipefail
cd "`dirname $0`/.."
PATH="$PWD/.fvm/flutter_sdk/bin:$PATH"

rm -rf tmp/cljdtests
mkdir -p tmp/cljdtests/src/cljd
cat > tmp/cljdtests/deps.edn <<EOF
{:paths ["src" "../../test"] ; where your cljd files are
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
tensegritics/clojuredart {:git/url "https://github.com/tensegritics/ClojureDart.git"
:sha "c56e2dc1bef1841a7ce58ce2546946dd0be414e1"}
io.github.wevre/transit-cljd {:git/tag "v0.8.36"
:git/sha "d9541d0"}
datascript/datascript {:local/root "../../"}}
:cljd/opts {:main acme.unused
:kind :dart}}
EOF

cd tmp/cljdtests
clojure -M -m cljd.build init
dart pub add -d test || true
clojure -A:cljd-dev -M -m cljd.build compile datascript.test.serialize datascript.test.core datascript.test.db datascript.test.conn datascript.test.index datascript.test.query datascript.test.transact datascript.test.entity datascript.test.filter datascript.test.ident datascript.test.tuples datascript.test.components datascript.test.components datascript.test.pull-api datascript.test.explode datascript.test.lookup-refs datascript.test.lru datascript.test.parser datascript.test.parser-find datascript.test.parser-rules datascript.test.parser-return-map datascript.test.parser-where datascript.test.pull-parser datascript.test.query-aggregates datascript.test.query-find-specs datascript.test.query-fns datascript.test.query-not datascript.test.query-or datascript.test.query-pull datascript.test.query-return-map datascript.test.query-rules datascript.test.validation datascript.test.issues datascript.test.listen datascript.test.upsert datascript.test.parser-query datascript.test.query-v3
dart test -p vm
11 changes: 6 additions & 5 deletions src/datascript/built_ins.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[clojure.string :as str]
[datascript.db :as db]
[datascript.impl.entity :as de]
[datascript.util :as util]))
[datascript.util :as util]
#?(:cljd ["dart:math" :as Math])))

(defn- -differ? [& xs]
(let [l (count xs)]
Expand Down Expand Up @@ -33,7 +34,7 @@
(defn- and-fn [& args]
(reduce (fn [a b]
(if b b (reduced b))) true args))

(defn- or-fn [& args]
(reduce (fn [a b]
(if b (reduced b) b)) nil args))
Expand Down Expand Up @@ -87,8 +88,8 @@
'rand rand, 'rand-int rand-int,
'true? true?, 'false? false?, 'nil? nil?, 'some? some?, 'not not, 'and and-fn, 'or or-fn,
'complement complement, 'identical? identical?,
'identity identity, 'keyword keyword, 'meta meta, 'name name, 'namespace namespace, 'type type,
'vector vector, 'list list, 'set set, 'hash-map hash-map, 'array-map array-map,
'identity identity, 'keyword keyword, 'meta meta, 'name name, 'namespace namespace, #?@(:cljd [] :default ['type type]),
'vector vector, 'list list, 'set set, 'hash-map hash-map, #?@(:cljd [] :default ['array-map array-map]),
'count count, 'range range, 'not-empty not-empty, 'empty? empty?, 'contains? contains?,
'str str, 'subs, subs, 'get get,
'pr-str pr-str, 'print-str print-str, 'println-str println-str, 'prn-str prn-str,
Expand Down Expand Up @@ -124,7 +125,7 @@
(/ sum (count coll))))

(defn- aggregate-stddev [coll]
(#?(:cljs js/Math.sqrt :clj Math/sqrt) (aggregate-variance coll)))
(#?(:cljs js/Math.sqrt :default Math/sqrt) (aggregate-variance coll)))

(defn- aggregate-min
([coll]
Expand Down
49 changes: 37 additions & 12 deletions src/datascript/conn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@
(:require
[datascript.db :as db #?@(:cljs [:refer [DB FilteredDB]])]
[datascript.storage :as storage]
[extend-clj.core :as extend]
[me.tonsky.persistent-sorted-set :as set])
#?@(:cljd ()
:default [[extend-clj.core :as extend]
[me.tonsky.persistent-sorted-set :as set]]))
#?(:clj
(:import
[datascript.db DB FilteredDB])))

(extend/deftype-atom Conn [atom]
(deref-impl [this]
(:db @atom))
(compare-and-set-impl [this oldv newv]
(compare-and-set!
atom
(assoc @atom :db oldv)
(assoc @atom :db newv))))
#?(:cljd
(deftype Conn [atom]
cljd.core/IDeref
(-deref [this]
(:db @atom))
cljd.core/IReset
(-reset! [this newv]
(:db (swap! atom assoc :db newv)))
cljd.core/ISwap
(-swap! [this f]
(:db (swap! atom update :db f)))
(-swap! [this f a]
(:db (swap! atom update :db f a)))
(-swap! [this f a b]
(:db (swap! atom update :db f a b)))
(-swap! [this f a b xs]
(:db (apply swap! atom update :db f a b xs)))
cljd.core/ILookup
(-lookup [this key]
(case key :atom atom nil))
(-lookup [this key not-found]
(case key :atom atom not-found)))
:default
(extend/deftype-atom Conn [atom]
(deref-impl [this]
(:db @atom))
(compare-and-set-impl [this oldv newv]
(compare-and-set!
atom
(assoc @atom :db oldv)
(assoc @atom :db newv)))))

(defn- make-conn [opts]
(->Conn (atom opts)))
Expand All @@ -36,7 +60,8 @@

(defn conn? [conn]
(and
#?(:clj (instance? clojure.lang.IDeref conn)
#?(:cljd (instance? Conn conn)
:clj (instance? clojure.lang.IDeref conn)
:cljs (satisfies? cljs.core/IDeref conn))
(if-some [db @conn]
(db/db? db)
Expand Down Expand Up @@ -112,7 +137,7 @@
(transact! conn tx-data nil))
([conn tx-data tx-meta]
{:pre [(conn? conn)]}
(locking conn
(#?(:cljd do :default locking) #?(:cljd nil :default conn)
(let [report (-transact! conn tx-data tx-meta)]
(doseq [[_ callback] (:listeners @(:atom conn))]
(callback report))
Expand Down
Loading