Skip to content

Commit 1346c3f

Browse files
committed
CLJS-1483: Minor DCE regression with advanced compilation mode
change :def case in compiler so that we once again only emit if init supplied. The REPL is the only exception. Without the above change foward declarations would prevent DCE. fix minor DCE issue around murmur3 hashing.
1 parent 30e302c commit 1346c3f

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,8 +1198,13 @@
11981198
(next coll))
11991199
(mix-collection-hash hash-code n))))
12001200

1201-
(def ^:private empty-ordered-hash
1202-
(mix-collection-hash 1 0))
1201+
(def ^{:private true :jsdoc ["@type {*}"]}
1202+
EMPTY_ORDERED_HASH nil)
1203+
1204+
(defn- empty-ordered-hash []
1205+
(when (nil? EMPTY_ORDERED_HASH)
1206+
(set! EMPTY_ORDERED_HASH (mix-collection-hash 1 0)))
1207+
EMPTY_ORDERED_HASH)
12031208

12041209
(defn ^number hash-unordered-coll
12051210
"Returns the hash code, consistent with =, for an external unordered
@@ -1213,8 +1218,13 @@
12131218
(recur (inc n) (bit-or (+ hash-code (hash (first coll))) 0) (next coll))
12141219
(mix-collection-hash hash-code n))))
12151220

1216-
(def ^:private empty-unordered-hash
1217-
(mix-collection-hash 0 0))
1221+
(def ^{:private true :jsdoc ["@type {*}"]}
1222+
EMPTY_UNORDERED_HASH nil)
1223+
1224+
(defn- empty-unordered-hash []
1225+
(when (nil? EMPTY_UNORDERED_HASH)
1226+
(set! EMPTY_UNORDERED_HASH (mix-collection-hash 0 0)))
1227+
EMPTY_UNORDERED_HASH)
12181228

12191229
;;;;;;;;;;;;;;;;;;; protocols on primitives ;;;;;;;;
12201230
(declare hash-map list equiv-sequential)
@@ -2801,7 +2811,7 @@ reduces them without incurring seq initialization"
28012811
false))
28022812

28032813
IHash
2804-
(-hash [coll] empty-ordered-hash)
2814+
(-hash [coll] (empty-ordered-hash))
28052815

28062816
ISeqable
28072817
(-seq [coll] nil)
@@ -4916,7 +4926,7 @@ reduces them without incurring seq initialization"
49164926
(set! (.-EMPTY-NODE PersistentVector) (VectorNode. nil (make-array 32)))
49174927

49184928
(set! (.-EMPTY PersistentVector)
4919-
(PersistentVector. nil 0 5 (.-EMPTY-NODE PersistentVector) (array) empty-ordered-hash))
4929+
(PersistentVector. nil 0 5 (.-EMPTY-NODE PersistentVector) (array) (empty-ordered-hash)))
49204930

49214931
(set! (.-fromArray PersistentVector)
49224932
(fn [xs ^boolean no-clone]
@@ -5466,7 +5476,7 @@ reduces them without incurring seq initialization"
54665476
ICounted
54675477
(-count [coll] count))
54685478

5469-
(set! (.-EMPTY PersistentQueue) (PersistentQueue. nil 0 nil [] empty-ordered-hash))
5479+
(set! (.-EMPTY PersistentQueue) (PersistentQueue. nil 0 nil [] (empty-ordered-hash)))
54705480

54715481
(es6-iterable PersistentQueue)
54725482

@@ -5643,7 +5653,7 @@ reduces them without incurring seq initialization"
56435653
(-as-transient [coll]
56445654
(transient (into (hash-map) coll))))
56455655

5646-
(set! (.-EMPTY ObjMap) (ObjMap. nil (array) (js-obj) 0 empty-unordered-hash))
5656+
(set! (.-EMPTY ObjMap) (ObjMap. nil (array) (js-obj) 0 (empty-unordered-hash)))
56475657

56485658
(set! (.-HASHMAP_THRESHOLD ObjMap) 8)
56495659

@@ -5995,7 +6005,7 @@ reduces them without incurring seq initialization"
59956005
(-as-transient [coll]
59966006
(TransientArrayMap. (js-obj) (alength arr) (aclone arr))))
59976007

5998-
(set! (.-EMPTY PersistentArrayMap) (PersistentArrayMap. nil 0 (array) empty-unordered-hash))
6008+
(set! (.-EMPTY PersistentArrayMap) (PersistentArrayMap. nil 0 (array) (empty-unordered-hash)))
59996009

60006010
(set! (.-HASHMAP-THRESHOLD PersistentArrayMap) 8)
60016011

@@ -6931,7 +6941,7 @@ reduces them without incurring seq initialization"
69316941
(-as-transient [coll]
69326942
(TransientHashMap. (js-obj) root cnt has-nil? nil-val)))
69336943

6934-
(set! (.-EMPTY PersistentHashMap) (PersistentHashMap. nil 0 nil false nil empty-unordered-hash))
6944+
(set! (.-EMPTY PersistentHashMap) (PersistentHashMap. nil 0 nil false nil (empty-unordered-hash)))
69356945

69366946
(set! (.-fromArray PersistentHashMap)
69376947
(fn [arr ^boolean no-clone]
@@ -7724,7 +7734,7 @@ reduces them without incurring seq initialization"
77247734

77257735
(-comparator [coll] comp))
77267736

7727-
(set! (.-EMPTY PersistentTreeMap) (PersistentTreeMap. compare nil 0 nil empty-unordered-hash))
7737+
(set! (.-EMPTY PersistentTreeMap) (PersistentTreeMap. compare nil 0 nil (empty-unordered-hash)))
77287738

77297739
(es6-iterable PersistentTreeMap)
77307740

@@ -8045,7 +8055,7 @@ reduces them without incurring seq initialization"
80458055
(-as-transient [coll] (TransientHashSet. (-as-transient hash-map))))
80468056

80478057
(set! (.-EMPTY PersistentHashSet)
8048-
(PersistentHashSet. nil (.-EMPTY PersistentArrayMap) empty-unordered-hash))
8058+
(PersistentHashSet. nil (.-EMPTY PersistentArrayMap) (empty-unordered-hash)))
80498059

80508060
(set! (.-fromArray PersistentHashSet)
80518061
(fn [items ^boolean no-clone]
@@ -8191,7 +8201,7 @@ reduces them without incurring seq initialization"
81918201
(-lookup coll k not-found)))
81928202

81938203
(set! (.-EMPTY PersistentTreeSet)
8194-
(PersistentTreeSet. nil (.-EMPTY PersistentTreeMap) empty-unordered-hash))
8204+
(PersistentTreeSet. nil (.-EMPTY PersistentTreeMap) (empty-unordered-hash)))
81958205

81968206
(es6-iterable PersistentTreeSet)
81978207

src/main/clojure/cljs/compiler.cljc

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -602,37 +602,40 @@
602602

603603
(defmethod emit* :def
604604
[{:keys [name var init env doc jsdoc export test var-ast]}]
605-
(let [mname (munge name)]
606-
(emit-comment env doc (concat jsdoc (:jsdoc init)))
607-
(when (:def-emits-var env)
608-
(when (= :return (:context env))
609-
(emitln "return ("))
610-
(emitln "(function (){"))
611-
(emits var)
612-
(when init
613-
(emits " = "
614-
(if-let [define (get-define mname jsdoc)]
615-
define
616-
init)))
617-
(when (:def-emits-var env)
618-
(emitln "; return (")
619-
(emits (merge
620-
{:op :var-special
621-
:env (assoc env :context :expr)}
622-
var-ast))
623-
(emitln ");})()")
624-
(when (= :return (:context env))
625-
(emitln ")")))
626-
;; NOTE: JavaScriptCore does not like this under advanced compilation
627-
;; this change was primarily for REPL interactions - David
628-
;(emits " = (typeof " mname " != 'undefined') ? " mname " : undefined")
629-
(when-not (= :expr (:context env)) (emitln ";"))
630-
(when export
631-
(emitln "goog.exportSymbol('" (munge export) "', " mname ");"))
632-
(when (and ana/*load-tests* test)
633-
(when (= :expr (:context env))
634-
(emitln ";"))
635-
(emitln var ".cljs$lang$test = " test ";"))))
605+
;; We only want to emit if an init is supplied, this is to avoid dead code
606+
;; elimination issues. The REPL is the exception to this rule.
607+
(when (or init (:def-emits-var env))
608+
(let [mname (munge name)]
609+
(emit-comment env doc (concat jsdoc (:jsdoc init)))
610+
(when (:def-emits-var env)
611+
(when (= :return (:context env))
612+
(emitln "return ("))
613+
(emitln "(function (){"))
614+
(emits var)
615+
(when init
616+
(emits " = "
617+
(if-let [define (get-define mname jsdoc)]
618+
define
619+
init)))
620+
(when (:def-emits-var env)
621+
(emitln "; return (")
622+
(emits (merge
623+
{:op :var-special
624+
:env (assoc env :context :expr)}
625+
var-ast))
626+
(emitln ");})()")
627+
(when (= :return (:context env))
628+
(emitln ")")))
629+
;; NOTE: JavaScriptCore does not like this under advanced compilation
630+
;; this change was primarily for REPL interactions - David
631+
;(emits " = (typeof " mname " != 'undefined') ? " mname " : undefined")
632+
(when-not (= :expr (:context env)) (emitln ";"))
633+
(when export
634+
(emitln "goog.exportSymbol('" (munge export) "', " mname ");"))
635+
(when (and ana/*load-tests* test)
636+
(when (= :expr (:context env))
637+
(emitln ";"))
638+
(emitln var ".cljs$lang$test = " test ";")))))
636639

637640
(defn emit-apply-to
638641
[{:keys [name params env]}]

0 commit comments

Comments
 (0)