Skip to content

Commit 19bdbd1

Browse files
committed
Optimize generated JS code by declaring fn arities (closes #197)
1 parent 7d2fe9e commit 19bdbd1

13 files changed

Lines changed: 122 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fixed handling of false values in entity cache (PR #198, thx [Brandon Bloom](https://github.com/brandonbloom))
66
- Fixed issue when string values were interpreted as lookup refs (#214)
77
- Speed up rschema calculation (#192, thx [Andre R.](https://github.com/rauhs))
8+
- Optimize generated JS code by declaring fn arities (#197)
89

910
# 0.15.5
1011

src/datascript/arrays.cljc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
(defn- if-cljs [env then else]
99
(if (:ns env) then else))
1010

11-
#?(:cljs (def make-array cljs.core/make-array)
11+
#?(:cljs (defn ^array make-array [size] (js/Array. size))
1212
:clj (defn make-array ^{:tag "[[Ljava.lang.Object;"}
1313
[size]
1414
(clojure.core/make-array java.lang.Object size)))
1515

16-
#?(:cljs (def into-array cljs.core/into-array)
16+
#?(:cljs (defn ^array into-array [aseq]
17+
(reduce (fn [a x] (.push a x) a) (js/Array.) aseq))
1718
:clj (defn into-array ^{:tag "[[Ljava.lang.Object;"}
1819
[aseq]
1920
(clojure.core/into-array java.lang.Object aseq)))
@@ -80,6 +81,8 @@
8081
#?(:cljs (.sort arr cmp)
8182
:clj (doto arr (java.util.Arrays/sort cmp))))
8283

83-
(def array?
84-
#?(:cljs cljs.core/array?
85-
:clj (fn array? [^Object x] (-> x .getClass .isArray))))
84+
#?(:cljs (defn ^boolean array? [x]
85+
(if (identical? *target* "nodejs")
86+
(.isArray js/Array x)
87+
(instance? js/Array x)))
88+
:clj (defn array? [^Object x] (-> x .getClass .isArray)))

src/datascript/btset.cljc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@
413413

414414
;; BTSet
415415

416-
(declare btset-conj btset-disj btset-iter)
416+
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
417+
(defn ^:declared btset-conj [set key cmp])
418+
(defn ^:declared btset-disj [set key cmp])
419+
(defn ^:declared btset-iter [set])
420+
417421
(def ^:const uninitialized-hash #?(:cljs nil :clj -1))
418422

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

690-
691-
692-
(declare iter riter iter-first iter-next iter-chunk iter-chunked-next iter-rseq iter-reduce)
694+
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
695+
(defn ^:declared iter [set left right])
696+
(defn ^:declared iter-first [iter])
697+
(defn ^:declared iter-next [iter])
698+
(defn ^:declared iter-chunk [iter])
699+
(defn ^:declared iter-chunked-next [iter])
700+
(defn ^:declared iter-rseq [iter])
701+
(defn ^:declared iter-reduce ([iter f]) ([iter f start]))
702+
(defn ^:declared riter [set left right])
703+
(defn ^:declared riter-first [riter])
704+
(defn ^:declared riter-next [ri])
705+
(defn ^:declared riter-rseq [riter])
693706

694707
(defn btset-iter
695708
"Iterator that represents whole set"
@@ -837,8 +850,6 @@
837850

838851
;; reverse iteration
839852

840-
(declare riter-first riter-next riter-rseq)
841-
842853
(deftype ReverseIter [set ^long left ^long right keys ^long idx]
843854
#?@(:cljs [
844855
ISeqable

src/datascript/core.cljc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@
1414

1515
;; SUMMING UP
1616

17+
(defn ^:declared q [q & inputs])
1718
(def ^:export q dq/q)
19+
20+
(defn ^:declared entity [db eid])
1821
(def ^:export entity de/entity)
22+
1923
(defn ^:export entity-db [^Entity entity]
2024
{:pre [(de/entity? entity)]}
2125
(.-db entity))
2226

27+
(defn ^:declared datom ([e a v]) ([e a v tx]) ([e a v tx added]))
2328
(def ^:export datom db/datom)
2429

30+
(defn ^:declared pull [db selector eid])
2531
(def ^:export pull dp/pull)
32+
33+
(defn ^:declared pull-many [db selector eids])
2634
(def ^:export pull-many dp/pull-many)
35+
36+
(defn ^:declared touch [e])
2737
(def ^:export touch de/touch)
2838

39+
(defn ^:declared empty-db ([]) ([schema]))
2940
(def ^:export empty-db db/empty-db)
41+
42+
(defn ^:declared init-db ([datoms]) ([datoms schema]))
3043
(def ^:export init-db db/init-db)
3144

45+
(defn ^:declared datom? [x])
3246
(def ^:export datom? db/datom?)
47+
48+
(defn ^:declared db? [x])
3349
(def ^:export db? db/db?)
3450

3551
(def ^:export ^:const tx0 db/tx0)
@@ -81,6 +97,7 @@
8197
{:pre [(db/db? db)]}
8298
(db/-index-range db attr start end))
8399

100+
(defn ^:declared entid [db eid])
84101
(def ^:export entid db/entid)
85102

86103
;; Conn

src/datascript/db.cljc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
data (last fragments)]
2727
`(throw (ex-info (str ~@(map (fn [m#] (if (string? m#) m# (list 'pr-str m#))) msgs)) ~data)))))
2828

29-
(defn seqable? [x]
29+
(defn #?@(:clj [^Boolean seqable?]
30+
:cljs [^boolean seqable?])
31+
[x]
3032
(and (not (string? x))
3133
#?(:cljs (or (cljs.core/seqable? x)
3234
(da/array? x))
@@ -115,7 +117,13 @@
115117

116118
;; ----------------------------------------------------------------------------
117119

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

120128
(deftype Datom [e a v tx added]
121129
#?@(:cljs
@@ -368,7 +376,16 @@
368376

369377
;; ----------------------------------------------------------------------------
370378

371-
(declare hash-db hash-fdb equiv-db empty-db pr-db resolve-datom validate-attr components->pattern indexing?)
379+
;; using defn instead of declare because of http://dev.clojure.org/jira/browse/CLJS-1871
380+
(defn- ^:declared hash-db [db])
381+
(defn- ^:declared hash-fdb [db])
382+
(defn- ^:declared equiv-db [a b])
383+
(defn- ^:declared empty-db ([]) ([schema]))
384+
#?(:cljs (defn ^:declared pr-db [db w opts]))
385+
(defn- ^:declared resolve-datom [db e a v t])
386+
(defn- ^:declared validate-attr [attr at])
387+
(defn- ^:declared components->pattern [db index cs])
388+
(defn ^:declared indexing? [db attr])
372389

373390
(defrecord-updatable DB [schema eavt aevt avet max-eid max-tx rschema hash]
374391
#?@(:cljs
@@ -681,7 +698,10 @@
681698

682699
;; ----------------------------------------------------------------------------
683700

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

686706
(defn- resolve-datom [db e a v t]
687707
(when a (validate-attr a (list 'resolve-datom 'db e a v t)))
@@ -985,7 +1005,8 @@
9851005
~expr
9861006
(cond-let ~@rest)))))
9871007

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

9901011
(defn retry-with-tempid [report es tempid upserted-eid]
9911012
(if (contains? (:tempids report) tempid)

src/datascript/impl/entity.cljc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
(:require [#?(:cljs cljs.core :clj clojure.core) :as c]
44
[datascript.db :as db]))
55

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

813
(defn- entid [db eid]
914
(when (or (number? eid)

src/datascript/js.cljs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
(reduce-kv
1818
(fn [m k v] (assoc m k (walk/postwalk keywordize v))) {})))
1919

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

2222
(defn- entity->clj [e]
2323
(cond (map? e)
@@ -90,15 +90,23 @@
9090
(defn ^:export entity [db eid]
9191
(d/entity db (js->clj eid)))
9292

93-
(def ^:export touch d/touch)
94-
(def ^:export entity_db d/entity-db)
95-
(def ^:export filter d/filter)
96-
(def ^:export is_filtered d/is-filtered)
93+
(defn ^:export ^:declared touch [e])
94+
(def ^:export touch d/touch)
95+
96+
(defn ^:export ^:declared entity_db [entity])
97+
(def ^:export entity_db d/entity-db)
98+
99+
(defn ^:export ^:declared filter [db pred])
100+
(def ^:export filter d/filter)
101+
102+
(defn ^:export ^:declared is-filtered [x])
103+
(def ^:export is_filtered d/is-filtered)
97104

98105
(defn ^:export create_conn [& [schema]]
99106
(d/create-conn (schema->clj schema)))
100107

101-
(def ^:export conn_from_db d/conn-from-db)
108+
(defn ^:export ^:declared conn-from-db [db])
109+
(def ^:export conn_from_db d/conn-from-db)
102110

103111
(defn ^:export conn_from_datoms
104112
([datoms] (conn_from_db (init_db datoms)))
@@ -128,9 +136,11 @@
128136
(callback report))
129137
db))
130138

131-
(def ^:export listen d/listen!)
139+
(defn ^:export ^:declared listen ([conn callback]) ([conn key callback]))
140+
(def ^:export listen d/listen!)
132141

133-
(def ^:export unlisten d/unlisten!)
142+
(defn ^:export ^:declared unlisten [conn key])
143+
(def ^:export unlisten d/unlisten!)
134144

135145
(defn ^:export resolve_tempid [tempids tempid]
136146
(aget tempids (str tempid)))

src/datascript/lru.cljc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns datascript.lru)
22

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

57
#?(:cljs
68
(deftype LRU [key-value gen-key key-gen gen limit]

src/datascript/parser.cljc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
;; utils
99

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

1216
(defprotocol ITraversable
1317
(-collect [_ pred acc])
@@ -178,8 +182,6 @@
178182
(deftrecord BindTuple [bindings])
179183
(deftrecord BindColl [binding])
180184

181-
(declare parse-binding)
182-
183185
(defn parse-bind-ignore [form]
184186
(when (= '_ form)
185187
(with-source (BindIgnore.) form)))
@@ -406,7 +408,6 @@
406408
(deftrecord Or [source rule-vars clauses])
407409
(deftrecord And [clauses])
408410

409-
(declare parse-clause parse-clauses)
410411

411412
(defn parse-pattern-el [form]
412413
(or (parse-placeholder form)

src/datascript/pull_api.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
[key frame]
6363
(some-> (:kvps frame) persistent! (get key)))
6464

65-
(def ^:private recursion-result
66-
(partial single-frame-result ::recursion))
65+
(defn- recursion-result [frame]
66+
(single-frame-result ::recursion frame))
6767

6868
(defn- recursion-frame
6969
[parent eid]

0 commit comments

Comments
 (0)