Skip to content

Commit bc4f3b5

Browse files
tonskyswannodette
authored andcommitted
CLJS-1256 cache UUID hash value
1 parent 3173645 commit bc4f3b5

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9502,7 +9502,7 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
95029502

95039503
;; UUID
95049504

9505-
(deftype UUID [uuid]
9505+
(deftype UUID [uuid ^:mutable __hash]
95069506
Object
95079507
(toString [_] uuid)
95089508
(equiv [this other]
@@ -9518,12 +9518,17 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
95189518

95199519
IHash
95209520
(-hash [this]
9521-
(goog.string/hashCode (pr-str this)))
9521+
(when (nil? __hash)
9522+
(set! __hash (goog.string/hashCode uuid)))
9523+
__hash)
95229524

95239525
IComparable
95249526
(-compare [_ other]
95259527
(garray/defaultCompare uuid (.-uuid other))))
95269528

9529+
(defn uuid [s]
9530+
(UUID. s nil))
9531+
95279532
;;; ExceptionInfo
95289533

95299534
(defn- pr-writer-ex-info [obj writer opts]

src/main/cljs/cljs/reader.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ nil if the end of stream has been reached")
561561
(defn ^:private read-uuid
562562
[uuid]
563563
(if (string? uuid)
564-
(UUID. uuid)
564+
(cljs.core/uuid uuid)
565565
(reader-error nil "UUID literal expects a string as its representation.")))
566566

567567
(def ^:dynamic *tag-table*

src/test/cljs/cljs/core_test.cljs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@
15631563
inst (str "2010-" (pad month) "-" (pad day) "T" (pad hour) ":14:15.666-00:00")]
15641564
(is (= (pr-str (js/Date. inst)) (str "#inst \"" inst "\"")))))
15651565
(let [uuid-str "550e8400-e29b-41d4-a716-446655440000"
1566-
uuid (UUID. uuid-str)]
1566+
uuid (cljs.core/uuid uuid-str)]
15671567
(is (= (pr-str uuid) (str "#uuid \"" uuid-str "\""))))
15681568
;; pr-str PersistentQueueSeq - CLJS-800
15691569
(is (= (pr-str (rest (conj cljs.core.PersistentQueue.EMPTY 1 2 3))) "(2 3)"))
@@ -1623,24 +1623,31 @@
16231623

16241624
(deftest test-uuid
16251625
(testing "Testing UUID"
1626-
(is (= (UUID. "550e8400-e29b-41d4-a716-446655440000")
1627-
(UUID. "550e8400-e29b-41d4-a716-446655440000")))
1628-
(is (not (identical? (UUID. "550e8400-e29b-41d4-a716-446655440000")
1629-
(UUID. "550e8400-e29b-41d4-a716-446655440000"))))
1630-
(is (= 42 (get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
1631-
(UUID. "550e8400-e29b-41d4-a716-446655440000")
1626+
(is (= (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
1627+
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))
1628+
(is (not (identical? (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
1629+
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000"))))
1630+
(is (= 42 (get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
1631+
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
16321632
:not-at-all-found)))
16331633
(is (= :not-at-all-found
1634-
(get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
1635-
(UUID. "666e8400-e29b-41d4-a716-446655440000")
1634+
(get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
1635+
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000")
16361636
:not-at-all-found)))
1637-
(is (= -1 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
1638-
(UUID. "666e8400-e29b-41d4-a716-446655440000"))))
1639-
(is (= 1 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
1640-
(UUID. "550e8400-a29b-41d4-a716-446655440000"))))
1641-
(is (= 0 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
1642-
(UUID. "550e8400-e29b-41d4-a716-446655440000"))))
1643-
))
1637+
(is (= -1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
1638+
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000"))))
1639+
(is (= 1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
1640+
(cljs.core/uuid "550e8400-a29b-41d4-a716-446655440000"))))
1641+
(is (= 0 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
1642+
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))))
1643+
(testing "UUID hashing"
1644+
(let [id "550e8400-e29b-41d4-a716-446655440000"
1645+
uuid (cljs.core/uuid id)
1646+
expected (goog.string/hashCode id)]
1647+
(is (= expected (hash uuid)))
1648+
;; checking hash cache
1649+
(is (= expected (.-__hash uuid)))
1650+
(is (= expected (hash uuid))))))
16441651

16451652
(deftest test-comparable
16461653
(testing "Testing IComparable"

0 commit comments

Comments
 (0)