leonoel / cloroutine

Coroutine support for clojure
Eclipse Public License 2.0
228 stars 10 forks source link

Algorithm explanation #16

Closed darkleaf closed 3 years ago

darkleaf commented 4 years ago

Hi! Could you please provide the algorithm explanation? It may be just a draft or a code sample or links.

What does the color mean? Why the compiler use hint transformation via the hint function? What the blocks and places are?


In the darkleaf/effect library I always clone a coroutine. Is it possible to reimplement the emit function to produce stateless code in the following form?

(letfn ["a lot of blocks" ""
        (some-block [...]
          [other-effect (fn [coeffect]
                          (other-block ...))])]
  [effect (fn [coeffect]
            (some-block ...))])

I wrote the draft of the readme. It's in Russian currently but if you please you can use google translate.

leonoel commented 4 years ago

In the darkleaf/effect library I always clone a coroutine. Is it possible to reimplement the emit function to produce stateless code in the following form?

I'm not sure to understand your intent. If you want to expose an immutable multi-shot continuation, single-shot + clone-first is the way to go. What limitations do you see with current behavior ?

darkleaf commented 4 years ago

I'm not sure to understand your intent.

Your compiler preserves the coroutine state as an array. This array holds the next block and its dependencies. This is a dynamic approach. What if the JVM can statically analyze block dependencies? Maybe the JVM will use optimizations?

What limitations do you see with current behavior?

Unnecessary array copying and (possible?) unreachable optimizations.

leonoel commented 4 years ago

I'm not willing to introduce breaking changes in public API (non-breaking performance improvements are welcome, of course). Current design was motivated by the possibility to nullify allocation overhead when reentrancy is not needed, with cloning to support other cases. Returning a lambda and a vector at each step would make extra allocations systematically, including in situations that don't require it.

Various libraries have taken a path closer to what you suggest :

I think a good start would be to check if one of them meets your performance requirements, and to gather some benchmarks to substantiate these optimization insights.

darkleaf commented 4 years ago

Thanks. Get me right I don’t offer any changes into this library. The initial question was about the algorithm. And I want to understand how it works. Maybe cloning is a good solution for me. Anyway it’s very interesting.