Re: Avoid duplicate computation in commute?
I think part of the problem is that commute should not return any result. You are not sure this is actually the value that will be returned, this is costly and misleading. There should be at least a variant of commute that return nil. -- 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
Re: Avoid duplicate computation in commute?
Aren't alter and dosync just using LockingTransactions, which use the Java locking stuff under the hood? On Fri, May 18, 2012 at 9:28 AM, Warren Lynn wrote: > > But I thought for a ref, all the "checking if an another thread has > changed the value behind my back" facility/logic is already there > (otherwise how can "dosync" and "alter" work, right?). So why not > using that? > > I did send the question to Rich but not sure he will have time to > attend this. I am still learning Clojure and so far like it > (especially compared to the bad experience with Common Lisp), but my > perfectionist side pokes me sometimes. > > On May 17, 6:42 pm, DAemon wrote: > > The reason that comes to mind most easily for me is that of deciding > which > > notion of equality to use for 'the value of Ref hasn't changed'. > > > > Also, short of keeping a counter on the Ref of the number of times it's > > been changed, and comparing on that, there's no other way to tell that no > > other thread has changed the Ref, as far as I know. Although I could be > > wrong, *throws question to the Gods of clojure* > > > > > > > > > > > > > > > > On Fri, May 18, 2012 at 8:03 AM, Warren Lynn > wrote: > > > Thanks and these are certainly workable solutions. But is there any > > > particular reason why with commute the update function is ALWAYS > > > called twice, even when no other thread changed the ref value at the > > > commit time? That is what bothers me. > > > > > On May 17, 5:46 pm, DAemon wrote: > > > > Would some of this difficulty be ameliorated by calling memoize on > the > > > > function that you use? That way, if it's an expensive function, and > the > > > > value hasn't changed, it's just looked up rather than recalculated. > > > > > > - DAemon > > > > > > On Fri, May 18, 2012 at 6:28 AM, Stuart Sierra > > > > wrote: > > > > > > > I think the point with `commute` is to allow for more concurrency > at > > > the > > > > > expense of more computation. > > > > > > > If you want assurance that your function is only called once, you > can > > > use > > > > > `alter`. > > > > > > > Keep in mind that *any* code in a Ref transaction has the > potential to > > > be > > > > > called more than once if there's a conflict. > > > > > > > All this doesn't mean that it's impossible to avoid the duplicate > > > > > computation on `commute`. The code to study would be here: > > > > > > > > https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe. > > > .. > > > > > > > -S > > > > > > > -- > > > > > 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 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 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 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
Re: Avoid duplicate computation in commute?
But I thought for a ref, all the "checking if an another thread has changed the value behind my back" facility/logic is already there (otherwise how can "dosync" and "alter" work, right?). So why not using that? I did send the question to Rich but not sure he will have time to attend this. I am still learning Clojure and so far like it (especially compared to the bad experience with Common Lisp), but my perfectionist side pokes me sometimes. On May 17, 6:42 pm, DAemon wrote: > The reason that comes to mind most easily for me is that of deciding which > notion of equality to use for 'the value of Ref hasn't changed'. > > Also, short of keeping a counter on the Ref of the number of times it's > been changed, and comparing on that, there's no other way to tell that no > other thread has changed the Ref, as far as I know. Although I could be > wrong, *throws question to the Gods of clojure* > > > > > > > > On Fri, May 18, 2012 at 8:03 AM, Warren Lynn wrote: > > Thanks and these are certainly workable solutions. But is there any > > particular reason why with commute the update function is ALWAYS > > called twice, even when no other thread changed the ref value at the > > commit time? That is what bothers me. > > > On May 17, 5:46 pm, DAemon wrote: > > > Would some of this difficulty be ameliorated by calling memoize on the > > > function that you use? That way, if it's an expensive function, and the > > > value hasn't changed, it's just looked up rather than recalculated. > > > > - DAemon > > > > On Fri, May 18, 2012 at 6:28 AM, Stuart Sierra > > > wrote: > > > > > I think the point with `commute` is to allow for more concurrency at > > the > > > > expense of more computation. > > > > > If you want assurance that your function is only called once, you can > > use > > > > `alter`. > > > > > Keep in mind that *any* code in a Ref transaction has the potential to > > be > > > > called more than once if there's a conflict. > > > > > All this doesn't mean that it's impossible to avoid the duplicate > > > > computation on `commute`. The code to study would be here: > > > > >https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe. > > .. > > > > > -S > > > > > -- > > > > 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 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 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
Re: Avoid duplicate computation in commute?
The reason that comes to mind most easily for me is that of deciding which notion of equality to use for 'the value of Ref hasn't changed'. Also, short of keeping a counter on the Ref of the number of times it's been changed, and comparing on that, there's no other way to tell that no other thread has changed the Ref, as far as I know. Although I could be wrong, *throws question to the Gods of clojure* On Fri, May 18, 2012 at 8:03 AM, Warren Lynn wrote: > Thanks and these are certainly workable solutions. But is there any > particular reason why with commute the update function is ALWAYS > called twice, even when no other thread changed the ref value at the > commit time? That is what bothers me. > > On May 17, 5:46 pm, DAemon wrote: > > Would some of this difficulty be ameliorated by calling memoize on the > > function that you use? That way, if it's an expensive function, and the > > value hasn't changed, it's just looked up rather than recalculated. > > > > - DAemon > > > > On Fri, May 18, 2012 at 6:28 AM, Stuart Sierra > > wrote: > > > > > > > > > > > > > > > > > I think the point with `commute` is to allow for more concurrency at > the > > > expense of more computation. > > > > > If you want assurance that your function is only called once, you can > use > > > `alter`. > > > > > Keep in mind that *any* code in a Ref transaction has the potential to > be > > > called more than once if there's a conflict. > > > > > All this doesn't mean that it's impossible to avoid the duplicate > > > computation on `commute`. The code to study would be here: > > > > >https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe. > .. > > > > > -S > > > > > -- > > > 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 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 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
Re: Avoid duplicate computation in commute?
Thanks and these are certainly workable solutions. But is there any particular reason why with commute the update function is ALWAYS called twice, even when no other thread changed the ref value at the commit time? That is what bothers me. On May 17, 5:46 pm, DAemon wrote: > Would some of this difficulty be ameliorated by calling memoize on the > function that you use? That way, if it's an expensive function, and the > value hasn't changed, it's just looked up rather than recalculated. > > - DAemon > > On Fri, May 18, 2012 at 6:28 AM, Stuart Sierra > wrote: > > > > > > > > > I think the point with `commute` is to allow for more concurrency at the > > expense of more computation. > > > If you want assurance that your function is only called once, you can use > > `alter`. > > > Keep in mind that *any* code in a Ref transaction has the potential to be > > called more than once if there's a conflict. > > > All this doesn't mean that it's impossible to avoid the duplicate > > computation on `commute`. The code to study would be here: > > >https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe... > > > -S > > > -- > > 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 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
Re: Avoid duplicate computation in commute?
Would some of this difficulty be ameliorated by calling memoize on the function that you use? That way, if it's an expensive function, and the value hasn't changed, it's just looked up rather than recalculated. - DAemon On Fri, May 18, 2012 at 6:28 AM, Stuart Sierra wrote: > I think the point with `commute` is to allow for more concurrency at the > expense of more computation. > > If you want assurance that your function is only called once, you can use > `alter`. > > Keep in mind that *any* code in a Ref transaction has the potential to be > called more than once if there's a conflict. > > All this doesn't mean that it's impossible to avoid the duplicate > computation on `commute`. The code to study would be here: > > > https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe49b74605553/src/jvm/clojure/lang/LockingTransaction.java#L459 > > -S > > > -- > 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 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
Re: Avoid duplicate computation in commute?
I think the point with `commute` is to allow for more concurrency at the expense of more computation. If you want assurance that your function is only called once, you can use `alter`. Keep in mind that *any* code in a Ref transaction has the potential to be called more than once if there's a conflict. All this doesn't mean that it's impossible to avoid the duplicate computation on `commute`. The code to study would be here: https://github.com/clojure/clojure/blob/8fda34e4c77cac079b711da59d5fe49b74605553/src/jvm/clojure/lang/LockingTransaction.java#L459 -S -- 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
Avoid duplicate computation in commute?
Hi, The duplicate computation in "commute" seems to me quite a blemish on clojure's beauty. I even see somebody's code with comments "pre- compute x because otherwise commute will call it twice". So I need to keep that fact in the back of my mind when using it? That is not what I was hoping for. I found this post where somebody else seems to be bothered enough to raise the same question: http://groups.google.com/group/clojure/browse_thread/thread/8b4560096018cd1f/bb2f9b05930a12de?lnk=gst&q=commute%2C+twice#bb2f9b05930a12de Can we do the computation twice *only* when at the commit time the ref was actually changed in another thread? Thanks. -- 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