ClojureScript

Unsigned hash for keywords produced with (keyword s)

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

ClojureScript will produce unsigned hash values at times (in particular for keywords produced with the keyword fn), and this is inconsistent with hashes produced on literal keywords and inconsistent with Clojure. The hash values will have the same 32-bit hex value, so this is perhaps not that critical for many uses.

Examples:

cljs.user=> (hash :op)
;=> -1882987955

cljs.user=> (hash (keyword "op"))
2411979341

This unsigned value is produced on Safari (shipping and WebKit nightly), and thus is in theory produced via both variants of the imul implementation (see CLJS-823). The unsigned value is also produced by Firefox.

In either case the value is equivalent to 0x8fc3e24d and works for many use cases, but it fails an = test.

user=> (= (hash :op) (hash (keyword "op")))
;=> true

cljs.user=> (= (hash :op) (hash (keyword "op")))
;=> false

  1. cljs_824.patch
    06/Jul/14 8:27 PM
    1 kB
    Mike Fikes
  2. cljs_824-2.patch
    06/Jul/14 10:04 PM
    1 kB
    Mike Fikes

Activity

Hide
Mike Fikes added a comment -

Not sure of the perf, but the attached cljs_824.patch appears to resolve the issue.

Show
Mike Fikes added a comment - Not sure of the perf, but the attached cljs_824.patch appears to resolve the issue.
Hide
Mike Fikes added a comment -

Attached a simpler cljs_824-2.patch:

The JavaScript idiom to coerce to signed 32-bit is x|0. The revised patch does this, but by simply invoking the cljs core int fn (which in turn does a bit-or with 0).

Show
Mike Fikes added a comment - Attached a simpler cljs_824-2.patch: The JavaScript idiom to coerce to signed 32-bit is x|0. The revised patch does this, but by simply invoking the cljs core int fn (which in turn does a bit-or with 0).

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: