Skip to content

Commit

Permalink
Optimize generated JS code by declaring fn arities (closes #197)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Apr 9, 2017
1 parent 7d2fe9e commit ada9497
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed handling of false values in entity cache (PR #198, thx [Brandon Bloom](https://github.com/brandonbloom))
- Fixed issue when string values were interpreted as lookup refs (#214)
- Speed up rschema calculation (#192, thx [Andre R.](https://github.com/rauhs))
- Optimize generated JS code by declaring fn arities (#197)

# 0.15.5

Expand Down
23 changes: 17 additions & 6 deletions src/datascript/btset.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,11 @@

;; BTSet

(declare btset-conj btset-disj btset-iter)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared btset-conj [set key cmp])
(defn ^:declared btset-disj [set key cmp])
(defn ^:declared btset-iter [set])

(def ^:const uninitialized-hash #?(:cljs nil :clj -1))

(deftype BTSet [root shift cnt comparator meta #?(:cljs ^:mutable __hash
Expand Down Expand Up @@ -687,9 +691,18 @@
^long [^BTSet set ^long path]
(-prev-path (.-root set) path (.-shift set)))



(declare iter riter iter-first iter-next iter-chunk iter-chunked-next iter-rseq iter-reduce)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared iter [set left right])
(defn ^:declared iter-first [iter])
(defn ^:declared iter-next [iter])
(defn ^:declared iter-chunk [iter])
(defn ^:declared iter-chunked-next [iter])
(defn ^:declared iter-rseq [iter])
(defn ^:declared iter-reduce ([iter f]) ([iter f start]))
(defn ^:declared riter [set left right])
(defn ^:declared riter-first [riter])
(defn ^:declared riter-next [ri])
(defn ^:declared riter-rseq [riter])

(defn btset-iter
"Iterator that represents whole set"
Expand Down Expand Up @@ -837,8 +850,6 @@

;; reverse iteration

(declare riter-first riter-next riter-rseq)

(deftype ReverseIter [set ^long left ^long right keys ^long idx]
#?@(:cljs [
ISeqable
Expand Down
27 changes: 23 additions & 4 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@

;; ----------------------------------------------------------------------------

(declare hash-datom equiv-datom seq-datom val-at-datom nth-datom assoc-datom)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn- ^:declared hash-datom [d])
(defn- ^:declared equiv-datom [a b])
(defn- ^:declared seq-datom [d])
(defn- ^:declared nth-datom ([d i]) ([d i nf]))
(defn- ^:declared assoc-datom [d k v])
(defn- ^:declared val-at-datom [d k nf])

(deftype Datom [e a v tx added]
#?@(:cljs
Expand Down Expand Up @@ -368,7 +374,16 @@

;; ----------------------------------------------------------------------------

(declare hash-db hash-fdb equiv-db empty-db pr-db resolve-datom validate-attr components->pattern indexing?)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn- ^:declared hash-db [db])
(defn- ^:declared hash-fdb [db])
(defn- ^:declared equiv-db [a b])
(defn- ^:declared empty-db [db])
#?(:cljs (defn ^:declared pr-db [db w opts]))
(defn- ^:declared resolve-datom [db e a v t])
(defn- ^:declared validate-attr [attr at])
(defn- ^:declared components->pattern [db index cs])
(defn ^:declared indexing? [db attr])

(defrecord-updatable DB [schema eavt aevt avet max-eid max-tx rschema hash]
#?@(:cljs
Expand Down Expand Up @@ -681,7 +696,10 @@

;; ----------------------------------------------------------------------------

(declare entid-strict entid-some ref?)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared entid-strict [db eid])
(defn ^:declared entid-some [db eid])
(defn ^:declared ref? [db attr])

(defn- resolve-datom [db e a v t]
(when a (validate-attr a (list 'resolve-datom 'db e a v t)))
Expand Down Expand Up @@ -985,7 +1003,8 @@
~expr
(cond-let ~@rest)))))

(declare transact-tx-data)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared transact-tx-data [report es])

(defn retry-with-tempid [report es tempid upserted-eid]
(if (contains? (:tempids report) tempid)
Expand Down
7 changes: 6 additions & 1 deletion src/datascript/impl/entity.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
(:require [#?(:cljs cljs.core :clj clojure.core) :as c]
[datascript.db :as db]))

(declare entity ->Entity equiv-entity lookup-entity touch)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared entity [db eid])
(defn ^:declared ->Entity [db eid touched cache])
(defn- ^:declared equiv-entity [this that])
(defn- ^:declared lookup-entity ([this attr]) ([this attr not-found]))
(defn ^:declared touch [e])

(defn- entid [db eid]
(when (or (number? eid)
Expand Down
2 changes: 1 addition & 1 deletion src/datascript/js.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(reduce-kv
(fn [m k v] (assoc m k (walk/postwalk keywordize v))) {})))

(declare entities->clj)
(defn- ^:declared entities->clj [entities])

(defn- entity->clj [e]
(cond (map? e)
Expand Down
4 changes: 3 additions & 1 deletion src/datascript/lru.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns datascript.lru)

(declare assoc-lru cleanup-lru)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared assoc-lru [lru k v])
(defn ^:declared cleanup-lru [lru])

#?(:cljs
(deftype LRU [key-value gen-key key-gen gen limit]
Expand Down
9 changes: 5 additions & 4 deletions src/datascript/parser.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

;; utils

(declare collect-vars-acc)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn- ^:declared collect-vars-acc [acc form])
(defn ^:declared parse-clause [form])
(defn ^:declared parse-clauses [clauses])
(defn ^:declared parse-binding [form])

(defprotocol ITraversable
(-collect [_ pred acc])
Expand Down Expand Up @@ -178,8 +182,6 @@
(deftrecord BindTuple [bindings])
(deftrecord BindColl [binding])

(declare parse-binding)

(defn parse-bind-ignore [form]
(when (= '_ form)
(with-source (BindIgnore.) form)))
Expand Down Expand Up @@ -406,7 +408,6 @@
(deftrecord Or [source rule-vars clauses])
(deftrecord And [clauses])

(declare parse-clause parse-clauses)

(defn parse-pattern-el [form]
(or (parse-placeholder form)
Expand Down
3 changes: 2 additions & 1 deletion src/datascript/pull_parser.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
spec (reduce aggregate-specs init specs)]
[:subpattern (update spec :attrs persistent!)])))

(declare parse-pattern)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared parse-pattern [pattern])

(def ^:private wildcard? #{'* :* "*"})

Expand Down
8 changes: 4 additions & 4 deletions src/datascript/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
;; ----------------------------------------------------------------------------

(def ^:const lru-cache-size 100)
(declare built-ins)

;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared -collect ([context symbols]) ([acc rels symbols]))
(defn ^:declared -resolve-clause [context clause])

;; Records

Expand Down Expand Up @@ -494,9 +497,6 @@
(second clause)
(first clause)))))

(declare -collect)
(declare -resolve-clause)

(def rule-seqid (atom 0))

(defn expand-rule [clause context used-args]
Expand Down
8 changes: 7 additions & 1 deletion src/datascript/query_v3.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
[datascript.db Datom]
[datascript.btset Iter])))

(declare resolve-clauses collect-rel-xf collect-to)
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
(defn ^:declared resolve-clauses [context clauses])
(defn ^:declared collect-rel-xf [syms-indexed rel])
(defn ^:declared collect-to
([context syms acc])
([context syms acc xfs])
([context syms acc xfs specimen]))

(def ^:const lru-cache-size 100)

Expand Down

0 comments on commit ada9497

Please sign in to comment.