<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">And one more thing, you mentioned the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Synchronized</code> implementation at:</p>
<p dir="auto"><a href="https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala" style="color:#3983C4">https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala</a></p>
<p dir="auto">N.B. I happen to believe this is harmful because:</p>
<ol>
<li value="1">Mutexes are in general bad, a solution of last resort, because it prevents threads from making progress in case the scheduler pauses the OS thread that holds the lock; for this reason we want non-blocking, even wait-free algorithms, whenever possible</li>
<li value="2">This implementation itself has performance characteristics that are less than ideal — could be much better as it could use platform intrinsics for spin-locking and it could be biased for single threaded uses</li>
</ol>
<p dir="auto">On point number 2, this is important because it shows that <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Ref</code> isn’t very adequate to build <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Synchronized</code>.</p>
<p dir="auto">On point number 1 … this extends to <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> usage too. If you have blocking behaviour in a way that prevents threads from making progress, such solutions don’t scale and it doesn’t matter how you build it (<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> or <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">IORef</code> or whatever), it’s still going to be a bottleneck.</p>
<p dir="auto">-- <br>
Alexandru Nedelcu<br>
<a href="https://alexn.org" style="color:#3983C4">https://alexn.org</a></p>
<p dir="auto">On 2 Jan 2019, at 2:44, Alexandru Nedelcu wrote:</p>
</div>
<div style="white-space:normal"></div>
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><div id="1D24CD04-9E60-4D47-863B-937E511310D6">
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Hello,</p>
<p dir="auto">I’m the author of the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> implementation in Cats-Effect.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">Recently I had an interesting discussion on MVars with cats-effect library<br>
designers. Cats-effect brings MVar synchronization primitive along with<br>
other IO stuff to the Scala programming language. I tried to persuade them<br>
to include some Control.Concurrent.MVar’s functions to the library but has<br>
failed completely. Moreover, now I think that MVar is a poor choice for<br>
basic synchronization primitive.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">I believe <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> is a superb choice for synchronisation, because it behaves like a blocking queue, which in computer science is a pretty fundamental tool.</p>
<p dir="auto">It is true that in Cats-Effect an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> might not be a primitive, but as I explained in that thread, on top of the JVM the real primitives are the low level building blocks like <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">AtomicReference</code>.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">1. It’s complex. Each MVar has 2 state transitions, each may block.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">Blocking is a feature. If you have to build that logic via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Ref</code> (<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">IORef</code>), you’d have to model the state machine by yourself and that’s complexity being pushed to the user.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">2. It does not play well in presence of asynchronous exceptions. More<br>
specifically, `take` and `put` operations should be balanced (each `take`<br>
must be followed by `put`)</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">The <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> followed by a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">put</code> rule is only for your “modify” use-case. </p>
<p dir="auto">The problem is that a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">modify</code> function that’s described via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> + <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">put</code> is not an atomic operation and this is a problem, but only if any of the actors accessing the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> are doing so in a different order.</p>
<p dir="auto">This isn’t a problem for other use-cases tough.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">this force programmer to mask asynchronous<br>
exceptions during the MVar acquisition and since `take` function may block,<br>
this will delay task cancelation.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">I don’t have much experience with Haskell’s async exceptions, but if you mean that <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> must be executed as <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">uncancelable</code>, via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">bracket</code>, then this is a problem that we can fix in Cats-Effect 2.x, as mentioned in that thread.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">What could be the sensible alternative? Guy from the cats-effect suggested<br>
me IVar + atomic reference (IORef). This pattern separates concern of<br>
blocking (synchronization) from the atomic mutation. So everything can be<br>
represented as atomic reference with IVar inside. Just look at this<br>
beautiful mutex implementation<br>
<a href="https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala" style="color:#777">https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala</a></p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">As I said above, most things on top of the JVM can be implemented via an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">AtomicReference</code> due to its memory model and this is no exception.</p>
<p dir="auto">But it’s not elegant, or simple ;-)</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">(By ”beautiful” I mean approach itself of course, but not the Scala’s<br>
syntax. Scala is one of most ugliest girls after C++ I was forced to date<br>
with by my employer for money. Thankfully he didn’t force me to do the same<br>
things with her grandma Java).</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">That’s unnecessary and TBH a turnoff.</p>
<p dir="auto">Cheers,</p>
<p dir="auto">-- <br>
Alexandru Nedelcu<br>
<a href="https://alexn.org" style="color:#3983C4">https://alexn.org</a></p>
</div>
</div></div></blockquote>
<div style="white-space:normal">
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px">
</blockquote></div>
<div style="white-space:normal">
</div>
</div>
</body>
</html>