chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Fwd: Re: Continuation finalization and delimited continu


From: megane
Subject: [Chicken-users] Fwd: Re: Continuation finalization and delimited continuations
Date: Sat, 06 Oct 2012 23:32:35 +0300
User-agent: mu4e 0.9.9-dev4; emacs 24.1.1

Hi all.

Here's a short discussion I had with Kon Lovett. I'm forwarding it here
for anyone interested. With Kon's kind permission, of course.

---

megane writes:

Kon Lovett writes:

> On Oct 4, 2012, at 12:50 PM, megane wrote:
>
>> 
>> Kon Lovett writes:
>> 
>>> On Oct 4, 2012, at 11:45 AM, megane wrote:
>>> 
>>>> 
>>>> Hi there,
>>>> 
>>>> I've been playing with the F-operator egg a bit. Specifically, I've made
>>>> some simple tests using the %shift/%reset construction. These are the
>>>> ones that avoid calling any dynamic wind thunks when calling a
>>>> continuation that's not the current one or sth.
>>> 
>>> Actually none call dynamic-wind, just that the '%' versions don't play well 
>>> with the construct.
>> 
>> I mean the installed dynamic-wind thunks don't get called. I don't
>> expect %shift or %reset to call dynamic-wind.
>> 
>> And if you change the %shift/%resets to shift/reset then you can see
>> that the after thunk does get called, printing 'out'.
>
> The '%' versions will not invoke the after thunks.
>
>> 
>>> 
>>> I really should remove the '%' versions since they are based on 
>>> "##sys#call-with-direct-continuation", literally access to the internal 
>>> continuation. My use is a bad idea.
>>> 
>>> I think your "real" continuation, "k", isn't being gc'ed because its' 
>>> dynamic-extent is still active. The continuation never returns.
>> 
>> But the continuations *do* get gc'd! At least it seems so if you
>> uncomment the set-finalizer! line. Or maybe it's something else that
>> gets collected.
>
> So I am wrong again.
>
>> 
>> It's the installed dynamic-wind thunks that don't seem to get gc'd.
>
> I suspect the dynamic-wind chain is just elongating w/o any cleanup. The 
> "%shift" will escape the dynamic-wind body w/o invoking the 'results' 
> procedure, which would pop the dynamic-winds stack.

Indeed that seems to be the case when looking at the dw implementation.

>
> I just think the '%' versions are too restricted in their utility due to the 
> use of direct-return.

It's just that the semantics are not correct if they do dynamic wind, at
least according to Oleg Kiesylow [1].

(reset (foo (shift f f))) should behave like (lambda (v) (foo v))

Consider this snippet:

(let ((captured-k
       (reset (print (shift f f)))))
  (print "str: '"
         (with-output-to-string
           (lambda ()
             (captured-k "this should go to the string")))
         "'"))

It should print str:'this should go to the string', but instead it
prints two lines.

However, if you add the %s it works as expected.

>
>> 
>>> 
>>>> 
>>>> Now, does anybody have any idea why this simple example wants to eat all
>>>> my memory?
>>>> 
>>>> ---
>>>> (use shift-reset)
>>>> 
>>>> (let lp [(i 0)]
>>>> (if (< i 100000000)
>>>>     (begin
>>>>    (%reset
>>>>     (call/cc
>>>>      (lambda [k]
>>>>           ;; uncomment the next line to see they're getting finalized
>>>>        ;;(set-finalizer! k (lambda [k] (print k " is being finalized")))
>>>>        (if #t
>>>>            (dynamic-wind
>>>>                (lambda [] (begin))
>>>>                (lambda []
>>>>                  (if (= 0 (remainder i 100))
>>>>                      (print i))
>>>>                  (%shift _ #f))
>>>>                (lambda [] (print "out")))
>>>>            (%shift _ #f)))))
>>>>    (lp (add1 i)))))
>>>> ---
>>>> 
>>>> If you change the #t to #f it doesn't appear to do that.
>>>> It seems like the installed dw-thunks are not getting gc'd. 
>>>> 
>>>> Should they be?
>>>> 
>>>> A related question: Should the dynamic wind thunks be called when the
>>>> continuation gets gc'd?
>>>> 
>>>> AFAIK the %reset/%shift semantics cannot be implemented in R5RS, and you
>>>> cannot escape from a continuation without calling the thunks in R5RS, so
>>>> this a Chicken specific question.
>>>> 
>>>> I _think_ it would be useful if the out-thunks get called at
>>>> continuation gc. I mean, if this could be implemented at all. I guess
>>>> you would need to track if the out-thunk was already called or
>>>> something.
>>>> 
>>>> Currently, if you jump out of, say with-output-to-file, with %shift, and
>>>> never return, you would leak file handles. Moreover, you cannot pass
>>>> your function that escapes with %shift to an arbitrary function. I mean,
>>>> you shouldn't.
>>>> 
>>>> Are there other solutions to this resource leak problem other that "use
>>>> shift/reset instead of %shift/%reset"?
>>>> 
>>>> Kind regards
>>>> 
>>>> AC
>>>> 
>>>> _______________________________________________
>>>> Chicken-users mailing list
>>>> address@hidden
>>>> https://lists.nongnu.org/mailman/listinfo/chicken-users
>> 


Footnotes: 
[1]  http://okmij.org/ftp/continuations/against-callcc.html




reply via email to

[Prev in Thread] Current Thread [Next in Thread]