^:once is absolutely needed for lazy seqs, because it allows the closed-over variables to get cleared while the lambda is *still running*, rather than waiting for it to finish. You can see the difference in this repl session:
user> (let [x (for [n (range)] (make-array Object 10000)), f (^:once fn* [] (nth x 1e6))] (f)) #<Object[] [Ljava.lang.Object;@402d3105> user> (let [x (for [n (range)] (make-array Object 10000)), f (fn* [] (nth x 1e6))] (f)) ; Evaluation aborted. Of course you may need to tune the numbers to make sure it runs out of space on your machine, but the point is that without ^:once, the lambda f holds onto the head of x, in case f is called again. ^:once constitutes a promise to only ever call it once, and so the sequence is cleaned up as soon as possible. My example doesn't use LazySeq, but I hope you can see how the same considerations apply there. On Tuesday, February 18, 2014 9:39:06 AM UTC-8, pron wrote: > > lazy-seq marks it's supplied lambdata with ^{:once true} to prevent the > memory leak described at the bottom of this page <http://clojure.org/lazy> > . > However, while going over the code for clojure.lang.LazySeq, I noticed > that ever since this commit by > Rich<https://github.com/clojure/clojure/commit/9253928ba2330b9929eb26577ba20047fb24c5de#diff-829faa850c65e040e132cd9243bf7ac2R42>, > > LazySeq doesn't extend AFn, but rather contains a reference to the lambda, > fn, which it nullifies immediately after the first use, supposedly > preventing any leaks caused by closed locals. > So my question is, is ^{:once true} still necessary for lazy-seqs, or does > this (pretty old) change to LazySeq make it redundant? > > Ron > > -- 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/groups/opt_out.