New reflection warning in r1381

2 views
Skip to first unread message

kotor

unread,
May 28, 2009, 4:03:06 PM5/28/09
to Clojure
After updating to r1381, the following code produces a "reflection
warning - call to divide can't be resolved" on the last line. This
warning did not occur at r1376 or earlier.

(defn prime-frequency [x y]
"integer integer -> real. frequency of primes in range"
(let [xy (range x y)]
(/ (count (filter prime? xy)) (count xy))))

Dex Wood

unread,
May 28, 2009, 11:13:59 PM5/28/09
to Clojure
After seeing this issue, I brought it up in #clojure. After
experimenting with it in both 1376 and earlier, it seems that the same
behaviour for stuff like (/ (int 1) (int 2)) exist, where it is about
5 times slower than (/ 1 2), which is kind of a strange result.

The solution is to create a method in Number.java that accepts two
ints as arguments. Right now there is a method with (int Object) and
(Object int), but not (int int). While we are at it, we should
probably create the same method for other numeric types. Just like
the other methods that call divide by changing the primitive to an
object and calling divide on the two, just make a call to divide with
both of them being objects, and you will get the same performance
without the reflection. I would post the code, but I don't have a CA
turned in.

Dex Wood

unread,
May 28, 2009, 11:24:22 PM5/28/09
to Clojure
The change that caused this problem between 1376 and 1386 was that
count got an inline definition, which allowed it to return a
primitive. In this case, count is returning an int in both places,
which brought to light the problem that I stated in the previous
post. The divide issue has been around since Clojure 1.0.

On May 28, 3:03 pm, kotor <cnbe...@netscape.net> wrote:

kotor

unread,
May 30, 2009, 9:09:30 PM5/30/09
to Clojure
Dex,

Thanks for the info. It's nice to understand what's going on. I am
trying to dig into the source more to get a deeper understanding of
the language, but Clojure is my first exposure to Java, so that part
is slow going.
Reply all
Reply to author
Forward
0 new messages