Thank you, that explains the failure of the lazy-cseq using top-level defs.
I really hope it gets fixed, Clojure is going to be a hard sell if I have to
explain things like this to my coworkers :(

Anyhow there is still the problem with nonlazy cseq blowing the heap.

(defn cseq [n]
  (if (= 1 n)
    [1]
    (cons n (cseq (if (even? n)
            (/ n 2)
            (+ (* 3 n) 1 ))))))

(apply max-key count (map cseq (range 1 1000000)))

*BOOM*

2011/1/20 David Powell <djpow...@djpowell.net>

>
> This same problem was raised recently:
>
>
> https://groups.google.com/group/clojure/browse_thread/thread/df4ae16ab0952786?tvc=2&q=memory
>
> It isn't a GC problem, it is an issue in the Clojure compiler.
>
>
> The issue seems to only affect top-level defs.  At the top-level:
>
> (reduce + (range 10000000))
>
>  - runs quickly without using lots of memory
>
>
> (def x (#(reduce + (range 10000000))))
>
>  - also runs quickly without using lots of memory
>
>
> (def x (reduce + (range 10000000)))
>
>  - uses lots of memory due to the sequence getting forced
>
>
> The problem seems to be caused by the clojure compiler forcing the sequence
> when trying to figure out how to compile it, not
> by the compiled code retaining the sequence at runtime.
>
> It probably isn't a good idea to put expensive top-level defs in your code,
> because they get run when you just try to aot
> compile your code; but if you need to, a workaround at the moment is to
> wrap your value in a function and call it as above.
>
> I suspect that it is possible for us to fix the compiler not to force these
> sequences.  I think it is happening in the
> InvokeExpr class.
>
> --
> Dave
>
> --
> 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<clojure%2bunsubscr...@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 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

Reply via email to