Thanks all, that makes a lot of sense.

To summarise, if I understand correctly, (on the JVM) comparing with = :
- first checks if the objects are one and the same, returns true if so
- if not, checks whether they are numbers or persistent collections, which 
have special-case checks
- if neither of the above, delegate to the object's .equals method.
Keywords, as best I can tell, don't override Object/equals, so the test is 
the same as identical? in the end i.e. whether they are the same object.

It makes sense, then, that comparing keywords that are the same is fast 
(~4ns, same as an identical? comparison on my machine), as this returns on 
the first step above. And comparison of differing keywords is slower as it 
goes through all of the steps above, as noted by @Mikera.

I also notice that the code for core/case seems to suggest that if the 
cases are labelled by keyword, then an "identical" check will be used 
(although, I don't yet understand the compiler code well enough to verify 
that this is true in the implementation of case*).

I guess a question that remains is whether this behaviour of keywords, that 
identical? and = give the same result, is something that is guaranteed, or 
whether it should be viewed as an implementation detail? If it is a 
guarantee I wonder whether there should be something on the data structures 
page of clojure.org indicating this, and noting how to do fast keyword 
comparison?

Another question, which is more for my understanding, is given that is 
currently the case that identical? and = are equivalent for keywords (and 
some core code depends on this behaviour), why does it work? Looking at 
Keyword.java I see that weak references are stored to interned keywords in 
a table. But if any of these Keyword objects were garbage-collected, I 
think it would break the parity between identical? and =. So it must be the 
case that something else is holding on to strong references somewhere. 
Which makes me wonder why weak references are used in the table. Anyone 
familiar with the compiler able to shed light on this for my edification? 
:-)


Jony

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to