a loop gives boxing warning when it shouldn't

71 views
Skip to first unread message

Albert Cardona

unread,
Jan 1, 2011, 5:47:34 PM1/1/11
to clo...@googlegroups.com
Hi all,

I'd apreciate help on figuring out why a loop gets number boxing
warnings, when it shouldn't:

http://clojure.pastebin.com/9uLZqGhy

I'm using clojure from:

commit f30995c86056959abca53d0ca35dcb9cfa73e6e6
Author: Stuart Halloway <s...@thinkrelevance.com>
Date: Fri Dec 17 15:17:20 2010 -0500

Thanks.

Albert

--
http://albert.rierol.net

Allen Johnson

unread,
Jan 2, 2011, 12:58:11 PM1/2/11
to clo...@googlegroups.com
Here is my attempt. I changed the divide to use unchecked-divide-int
and explicitly cast recur values to long.

http://clojure.pastebin.com/xs79ruw1

Allen

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Albert Cardona

unread,
Jan 2, 2011, 10:50:32 PM1/2/11
to clo...@googlegroups.com
2011/1/2 Allen Johnson <akjoh...@gmail.com>:

> Here is my attempt. I changed the divide to use unchecked-divide-int
> and explicitly cast recur values to long.
>
> http://clojure.pastebin.com/xs79ruw1
>
> Allen


Hi Allen,

Thanks for the effort.

What I don't understand is why one should cast to int or long at all
if the variables "l" and "r" of the loop are ints, and will remain
ints throughout. And k is an int since both r and l are ints, and i
and j will be ints too.

I can't see where the variables are boxed into objects. Both the loop
and the let declare them as primitives if they are given primitives.
Perhaps they are already objects before they reach the inc and dec
function calls in the recur, but if so, it's a mystery to me why.

Albert

--
http://albert.rierol.net

Allen Johnson

unread,
Jan 3, 2011, 12:15:02 AM1/3/11
to clo...@googlegroups.com
> I can't see where the variables are boxed into objects. Both the loop
> and the let declare them as primitives if they are given primitives.
> Perhaps they are already objects before they reach the inc and dec
> function calls in the recur, but if so, it's a mystery to me why.

Yeah that is interesting. I'm not sure the _why_ of it but I attempted
to narrow it down and it looks like it has something to do with nested
loops and recur. The code below is basically the same thing except the
first one generates auto-boxing warnings while the latter does not.
I'd also like to know why this is.

(set! *warn-on-reflection* true)

;; generates auto-boxing warnings
(defn example [^long x]
(loop [a x]
(let [i (loop [k a] k)]
(if false (recur i) i))))

;; no warnings
(defn example [^long x]
(loop [a x]
(let [i (let [k a] k)]
(if false (recur i) i))))

Also, I couldn't resist giving qsort another attempt. I got a new
laptop and need to break it in somehow :) So here is a rewritten
version based on an example in "Introduction to Algorithms" without
casts.

https://gist.github.com/763139

Allen

Albert Cardona

unread,
Jan 3, 2011, 9:32:37 AM1/3/11
to clo...@googlegroups.com
2011/1/3 Allen Johnson <akjoh...@gmail.com>:

>> I can't see where the variables are boxed into objects. Both the loop
>> and the let declare them as primitives if they are given primitives.
>> Perhaps they are already objects before they reach the inc and dec
>> function calls in the recur, but if so, it's a mystery to me why.
>
> Yeah that is interesting. I'm not sure the _why_ of it but I attempted
> to narrow it down and it looks like it has something to do with nested
> loops and recur. The code below is basically the same thing except the
> first one generates auto-boxing warnings while the latter does not.
> I'd also like to know why this is.
>
> (set! *warn-on-reflection* true)
>
> ;; generates auto-boxing warnings
> (defn example [^long x]
>  (loop [a x]
>    (let [i (loop [k a] k)]
>      (if false (recur i) i))))
>
> ;; no warnings
> (defn example [^long x]
>  (loop [a x]
>    (let [i (let [k a] k)]
>      (if false (recur i) i))))

Very nice minimal example. If someone has access for filing bugs to
assembla, could this one please be filed in?


> Also, I couldn't resist giving qsort another attempt. I got a new
> laptop and need to break it in somehow :) So here is a rewritten
> version based on an example in "Introduction to Algorithms" without
> casts.
>
> https://gist.github.com/763139


Nice traditional example, with swap and partition functions. Very
pretty, and readable!

Albert
--
http://albert.rierol.net

Chouser

unread,
Jan 3, 2011, 12:00:29 PM1/3/11
to clo...@googlegroups.com
On Sat, Jan 1, 2011 at 5:47 PM, Albert Cardona <sapr...@gmail.com> wrote:
> Hi all,
>
> I'd apreciate help on figuring out why a loop gets number boxing
> warnings, when it shouldn't:
>
> http://clojure.pastebin.com/9uLZqGhy

I just filed this as:
http://dev.clojure.org/jira/browse/CLJ-701

--Chouser
http://joyofclojure.com/

Reply all
Reply to author
Forward
0 new messages