Skip to content

Commit

Permalink
CLJS-1256 cache UUID hash value
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky authored and swannodette committed May 12, 2015
1 parent 3173645 commit bc4f3b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9502,7 +9502,7 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."

;; UUID

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

IHash
(-hash [this]
(goog.string/hashCode (pr-str this)))
(when (nil? __hash)
(set! __hash (goog.string/hashCode uuid)))
__hash)

IComparable
(-compare [_ other]
(garray/defaultCompare uuid (.-uuid other))))

(defn uuid [s]
(UUID. s nil))

;;; ExceptionInfo

(defn- pr-writer-ex-info [obj writer opts]
Expand Down
2 changes: 1 addition & 1 deletion src/main/cljs/cljs/reader.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ nil if the end of stream has been reached")
(defn ^:private read-uuid
[uuid]
(if (string? uuid)
(UUID. uuid)
(cljs.core/uuid uuid)
(reader-error nil "UUID literal expects a string as its representation.")))

(def ^:dynamic *tag-table*
Expand Down
39 changes: 23 additions & 16 deletions src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@
inst (str "2010-" (pad month) "-" (pad day) "T" (pad hour) ":14:15.666-00:00")]
(is (= (pr-str (js/Date. inst)) (str "#inst \"" inst "\"")))))
(let [uuid-str "550e8400-e29b-41d4-a716-446655440000"
uuid (UUID. uuid-str)]
uuid (cljs.core/uuid uuid-str)]
(is (= (pr-str uuid) (str "#uuid \"" uuid-str "\""))))
;; pr-str PersistentQueueSeq - CLJS-800
(is (= (pr-str (rest (conj cljs.core.PersistentQueue.EMPTY 1 2 3))) "(2 3)"))
Expand Down Expand Up @@ -1623,24 +1623,31 @@

(deftest test-uuid
(testing "Testing UUID"
(is (= (UUID. "550e8400-e29b-41d4-a716-446655440000")
(UUID. "550e8400-e29b-41d4-a716-446655440000")))
(is (not (identical? (UUID. "550e8400-e29b-41d4-a716-446655440000")
(UUID. "550e8400-e29b-41d4-a716-446655440000"))))
(is (= 42 (get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
(UUID. "550e8400-e29b-41d4-a716-446655440000")
(is (= (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))
(is (not (identical? (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000"))))
(is (= 42 (get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
:not-at-all-found)))
(is (= :not-at-all-found
(get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
(UUID. "666e8400-e29b-41d4-a716-446655440000")
(get {(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000") 42}
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000")
:not-at-all-found)))
(is (= -1 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
(UUID. "666e8400-e29b-41d4-a716-446655440000"))))
(is (= 1 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
(UUID. "550e8400-a29b-41d4-a716-446655440000"))))
(is (= 0 (compare (UUID. "550e8400-e29b-41d4-a716-446655440000")
(UUID. "550e8400-e29b-41d4-a716-446655440000"))))
))
(is (= -1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "666e8400-e29b-41d4-a716-446655440000"))))
(is (= 1 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-a29b-41d4-a716-446655440000"))))
(is (= 0 (compare (cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")
(cljs.core/uuid "550e8400-e29b-41d4-a716-446655440000")))))
(testing "UUID hashing"
(let [id "550e8400-e29b-41d4-a716-446655440000"
uuid (cljs.core/uuid id)
expected (goog.string/hashCode id)]
(is (= expected (hash uuid)))
;; checking hash cache
(is (= expected (.-__hash uuid)))
(is (= expected (hash uuid))))))

(deftest test-comparable
(testing "Testing IComparable"
Expand Down

0 comments on commit bc4f3b5

Please sign in to comment.