On Sat May  5 14:53 2012, David Sletten wrote:
> Can anyone explain this change?
> (clojure-version)  => "1.2.0"
> (let [x 8.9] (identical? x x))  => true
> 
> Compared to:
> (clojure-version) => "1.4.0"
> (let [x 8.9] (identical? x x)) => false

Well, this is certainly an interesting phenomenon.  What is happening
here is part of Clojure's primitive optimisations introduced in Clojure
1.3.

Before Clojure 1.3, the code:

(let [x 8.9] (identical? x x))

Roughly can be thought of expanding to something like:

(let [x (Float. 8.9)] (identical? x x))

Since x is an object, it is indeed identical to itself.

In Clojure 1.3, the code could be thought of evaluating to something
like:

(let [x (float 8.9)] (identical? (Double. x) (Double. x)))

The x remains unboxed, so that when it is passed to the function call,
which presumably has no primitive-hinted forms, it much be boxed each
time it is an argument, and the resulting objects are not identical.

To see more of the same in action (in Clojure 1.3 and above):

(let [x 127] (identical? x x))  ;=> true
(let [x 128] (identical? x x))  ;=> false

The JVM has an optimisation such that boxed versions of small integers
get boxed to cached instances.  However for integers with large enough
magnitudes, the JVM produces new objects.

I hope this clears things up.

Sincerely,

Daniel

Attachment: signature.asc
Description: Digital signature

Reply via email to