News for hackers. Stuff that matters.
Re:Ruby's Proposed STM by AaronFriel on Friday October 30, 2020 @02:57AM (#24939815)
Now what happens when you add STM to these languages? You've just created an enormous footgun by creating another color of function (https://journal.stuffwithstuff.com/2015/02/01/what-color-is-..., see also: https://news.ycombinator.com/item?id=8984648).

What this proposal misses is that reasoning about software transactional memory in functions all but demands being able to abort and retry code to make progress, and that therefore means having a high level separation between transactional code and IO code.

Suppose you had a banking application, you would like to use STM (as in the "ractor" example in this article) to move balances from one to another. In Haskell, you'd perform STM actions on those TVars (transactional vars) and use the default retry policy of STM to automatically construct an order of operations such that each operation appears to occur atomically. (STM doesn't impose a clock based ordering but I don't think it would be tremendously difficult to do so.)

If you don't use Haskell's "check" or "retry", you find that your code will abort and fail with near certainty. You can't perform lots of transactions on tvars and not, occasionally, read from a var another transaction is writing.

So you add "check" calls to verify your preconditions and postconditions such as balances cannot go negative, and you add "retry" calls to automatically retry transactions that fail because of concurrency.

Now imagine doing that in a modern Ruby codebase, or a modern JavaScript codebase. There's so much mutation that happens in these environments, can you guarantee within a Ruby function that it hasn't mutated another variable with ease? That it hasn't written to disk, made an API call, or changed some global state?

Haskell pioneered the use of STM. It's easier to reason about in Haskell because the world of the STM can be isolated from the world of effects due to the IO monad. It's easy to declare "this function is blue" and "this function is red" and the never the twain shall meet.

In any other language, all bets are off.

Re:Ruby's Proposed STM by thaumasiotes on Friday October 30, 2020 @05:00AM (#24940404)
> That’s why C# has that little caveat. You can avoid the pain of async in C# by using threads.

This is the first time I've seen threads referred to as a way to avoid pain.

Re:Ruby's Proposed STM by ancarda on Friday October 30, 2020 @06:21AM (#24940815)
I honestly prefer threads and mutex locks to async/async.

Feel free to burn me at the stake.

Re:Ruby's Proposed STM by tzs on Friday October 30, 2020 @10:38AM (#24942673)
Same here. I'm curious how you got there.

I think a large part of that for me is because early in my career I got involved in Unix kernel work, first writing device drivers and later doing Unix ports, including both porting a swapping Unix to paged hardware (and rewriting pretty much all process and memory management from scratch) and porting a paging Unix to swapping hardware.

That was followed by a few years largely doing embedded stuff on systems too small to have operating systems or memory management units. For those I'd write a small kernel that supported threads and scheduling (sometimes cooperative and sometimes preemptive) and then write the embedded applications much like I would have written them on Unix, using threads instead of processes.

So for me threads are easy and things like async/wait seem overly complicated and less clear because I came at concurrency from the operating system side of things and the hardware side of things (e.g., dealing with interrupts) where thread-like thinking is natural and perhaps required.

Maybe it is conceptually harder for people who come at it from the application side of things?

Re:Ruby's Proposed STM by mumblemumble on Friday October 30, 2020 @03:04PM (#24945693)
I think it's more that threads are a poorer fit for the application side of things.

On the app side, "I want to do this thing that might take a while without blocking the UI, and then do something with the result, and I have zero need to communicate with the code that's running the task beyond giving it its arguments and getting its result back," is the most common use case. Task parallelism is tailor-made for that kind of problem.

Using a dedicated thread, on the other hand, gives you some raw material you can use to solve that problem, but leaves you to handle all the messy details on your own. And doing it the easy way - one short-lived thread for every thing you need to do in the background - is expensive, while doing it the correct way and implementing a thread pool is essentially just committing to Greenspunning your own personal task parallelism library.

Re:Ruby's Proposed STM by formerly_proven on Friday October 30, 2020 @09:00AM (#24941743)
Whether async or threads are preferable depends a lot on context and often the answer will be "both".

E.g. GUI code -> async, because you must do everything on the UI thread. Heavy lifting in a GUI app -> threads. Traditionally you'd do this with a huge mess of callbacks, but these days it's reasonably easy to just write the GUI portion using async and have background computations run in threads signal their results through futures into the async code, which makes everything so much easier.

Re:Ruby's Proposed STM by andrewzah on Friday October 30, 2020 @01:59PM (#24945048)
From what I can tell, both solve different problems.

There are times when one might want async executors like node's event loop or rust's tokio/async-std, but in other situations one might want os threads or "green threads" with a RwLock or Mutex.

What about a few long running threads that use async internally? ;-)

Re:Ruby's Proposed STM by mumblemumble on Friday October 30, 2020 @05:06PM (#24946895)
That's another thing that C# (or, rather, .NET) handles really well. I can't remember the exact syntax, but the Task API has a way of declaring a background task as "long-running". The key difference is, long-running tasks get a dedicated thread; short-running tasks execute on the thread pool.
Re:Ruby's Proposed STM by dullgiulio on Friday October 30, 2020 @03:10PM (#24945733)
That's the Go runtime.
Re:Ruby's Proposed STM by FpUser on Friday October 30, 2020 @10:59AM (#24942909)
Count me in your camp (with the couple of exceptions)
Re:Ruby's Proposed STM by tasogare on Friday October 30, 2020 @09:16AM (#24941889)
They aren’t even solving the same problem. Threads exist to use multiple CPUs while async/await are here not to waste time waiting on I/O.
Re:Ruby's Proposed STM by coder543 on Friday October 30, 2020 @10:07AM (#24942305)
Threads existed long before multi-CPU computers or multi-core CPUs were common, so no... they don’t exist for that reason.

Parallelism and concurrency are orthogonal concerns. Multiple threads can run on the same core, while concurrent awaits can run on separate cores (depending on the language). Or the opposite!

Threads definitely solve wasted time waiting on I/O, but OS-level threads are a very limited resource in many environments, which can cause problems more quickly in programs with unbounded concurrency, and they tend to have a lot of startup cost, so they have to be created judiciously to avoid hurting performance.

Languages like Go and Elixir give the programmer access to unlimited lightweight “threads”, and that works great for concurrency, without requiring developers to re-color their functions.

One additional note is that some OSes (including Linux) have struggled to support true nonblocking File I/O for many years, so you literally have had to use multiple OS threads for I/O concurrency... one for each file operation in progress. Otherwise you would block the async executor! Languages often handle this implementation detail for the programmer, so you just don’t realize you’re using a thread-per-await sometimes.

(io_uring seems to finally solve this in a meaningful way on Linux)

Re:Ruby's Proposed STM by taffer on Friday October 30, 2020 @05:32AM (#24940544)
Threads are fine as long as they do not share mutable data structures. For example, multithreaded application servers are a rather painless experience.
Re:Ruby's Proposed STM by eyelidlessness on Friday October 30, 2020 @10:54AM (#24942850)
Since all of these concurrency approaches’ pain points are around shared mutable state, maybe it’s shared mutable state that’s the pain point.
Re:Ruby's Proposed STM by bglusman on Friday October 30, 2020 @11:37AM (#24943338)
Yup, this is the conclusion of Joe Armstrong and why Erlang/Elixir/BEAM just doesn't allow mutable state, other than process dictionary which is inherently non-shared.
Re:Ruby's Proposed STM by mumblemumble on Friday October 30, 2020 @09:16AM (#24941885)
That observation doesn't really contribute to a comparison of dedicated threads and async/await, though, because the same is true of async routines.
Re:Ruby's Proposed STM by mumblemumble on Friday October 30, 2020 @09:13AM (#24941848)
For a long time I experienced excessive pain with both threads and task parallelism. It took a good year or two to realize the problem was that I didn't have a good instinct for figuring out which technology is more appropriate for a given situation.
Re:Ruby's Proposed STM by rurban on Friday October 30, 2020 @08:35AM (#24941567)
Not only bets are off in non-functional language, it's the wrong solution.

STM puts the cost of locking into everything, ie slower default operations, whilst still being concurrency unsafe. Deadlocks can occur.

There are proven good solutions for fast and safe concurrency with ownership tracking and a good compiler/scheduler. No, not Rust. And not STM. We fought with this for years.

Re:Ruby's Proposed STM by rrss on Friday October 30, 2020 @08:45AM (#24941636)
> There are proven good solutions for fast and safe concurrency with ownership tracking and a good compiler/scheduler.

Do you have any pointers to these solutions for the uninitiated?

Re:Ruby's Proposed STM by rurban on Friday October 30, 2020 @04:35PM (#24946630)
All concurrency-safe languages. Eg pony, concurrent pascal, the parrot vm, ...
Re:Ruby's Proposed STM by amelius on Friday October 30, 2020 @11:19AM (#24943154)
Eh, STM is often used as a solution to avoid locking.

https://en.wikipedia.org/wiki/Software_transactional_memory

Re:Ruby's Proposed STM by brandmeyer on Friday October 30, 2020 @12:22PM (#24943843)
If you read into the implementation papers a little more deeply, you'll find that they are typically implemented with locks under the covers.
Re:Ruby's Proposed STM by chrisseaton on Friday October 30, 2020 @07:00AM (#24941016)
It’d be good if all writes in a transaction were automatically transactional and we had no TVars, and also if all IO automatically made a transaction irrevocable.

But I’m not sure this is tractable in either implementation effort or runtime overhead.

Re:Ruby's Proposed STM by marcosdumay on Friday October 30, 2020 @03:36PM (#24946047)
Are you talking about the Haskell one?

If so, you can't do IO inside a transaction (it won't compile), and any value not on a TVar is thread-local.

Re:Ruby's Proposed STM by chrisseaton on Friday October 30, 2020 @06:44PM (#24947567)
> Are you talking about the Haskell one?

No?

I'm responding to the idea of the 'colour' of functions and suggesting how I think it should be in Ruby - all side effects transactional, but IO irrevocable. Drop the idea of TVar and make all mutable locations transactional.

Re:Ruby's Proposed STM by a1369209993 on Friday October 30, 2020 @02:08PM (#24945153)
Honestly the main feature of Haskell in this regard isn't STM-colored functions (as you note, that's a bad thing), but rather that it almost supports color-generic functions in the form of `Monad f => A -> f B -> f C`. Unfortunately, it doesn't correctly handle the identity monad `type Id a = a`, instead requiring `newtype Id a = Id a`, so it's impossible to write a function that matches both (eg) `A -> B` and `Monad f => A -> f B`.
Re:Ruby's Proposed STM by AaronFriel on Friday October 30, 2020 @05:05PM (#24946889)
I think Haskell enforcing a separation between the "color" (or monad) of functions is generally good.
Re:Ruby's Proposed STM by kccqzy on Friday October 30, 2020 @03:05PM (#24945696)
I don't understand your argument.

It is correct that the standard library provides you with a newtype-based identity monad. But as you have said, you can also write own `type Id a = a`. What's wrong with that?

Re:Ruby's Proposed STM by a1369209993 on Friday October 30, 2020 @04:08PM (#24946378)
> What's wrong with [`type Id = a`]?

The fact that it doesn't work:

  > - The type synonym 'Id' should have 1 argument, but has been given none
  > - In the instance declaration for 'Monad Id'
  > | instance Monad Id where
Re:Ruby's Proposed STM by kccqzy on Saturday October 31, 2020 @04:21PM (#24954889)
Of course that won't work because that type synonym is far from a monad. I think you meant to write `type Id a = a` or `type Id f a = f a` instead.

But I get your argument now. What you are saying is that we want some kind of a way to define functions that are either pure or return a monadic value. Currently that's not doable because these two have distinct types. It may be difficult to unify them via the type system. This is even more difficult for higher-order functions that take other functions.

Re:Ruby's Proposed STM by a1369209993 on Saturday October 31, 2020 @07:10PM (#24955982)
> I think you meant to write `type Id a = a`

Indeed I did, sorry, unfortunately too late to edit. The following reproduces the error:

  {-# LANGUAGE TypeSynonymInstances #-}
  type Id a = a
  instance Monad Id where
    x >>= f = f x
    return = id
(The last two lines can be ommitted, since GHC aborts before checking the instance body. Edit: which is why I didn't notice I had the arguments to >>= backward; fixed?)

> It may be difficult to unify them via the type system. This is even more difficult for higher-order functions that take other functions.

IMO, the main difficulty in unifying `a ~ Monad m => m a` is inferring `m ~ \a1-> a1` in a way that allows `\a1-> a1` to inherit `Id`'s instances. It's a understandable deficiency, but it is a deficiency.

Re:Ruby's Proposed STM by jpcooper on Friday October 30, 2020 @05:06AM (#24940432)
There’s no guarantee that you’re not calling unsafePerformIO in Haskell, which a lot of outwardly pure libraries do. In fact, wouldn’t STM be doing this under the hood? Even the FFI in Haskell allows you to import a function without the return type being in IO as long as you pinky promise that it is pure.

The idea in the end is to provide a language for working on memory concurrently. You either choose speak it or not.

Re:Ruby's Proposed STM by kpmah on Friday October 30, 2020 @06:08AM (#24940742)
As with Rust's 'unsafe', the fact that you can explicitly circumvent checks doesn't make those checks useless. If something goes wrong you know exactly where to look.
Re:Ruby's Proposed STM by jpcooper on Friday October 30, 2020 @06:19AM (#24940807)
That may be the case for Rust. I am not sure what the implications of unsafe are for the type system.

In any case, the unsafeness of unsafePerformIO and importing an FFI function as a pure function are not reflected in the type in Haskell. These are things that can and do happen.

My point was that the ability to go outside the bounds of a language does not necessarily make the language useless.

Re:Ruby's Proposed STM by a1369209993 on Friday October 30, 2020 @03:04PM (#24945687)
> I am not sure what the implications of unsafe are for the type system.

There are no implications, that's the point.

> the unsafeness of unsafePerformIO and importing an FFI function as a pure function are not reflected in the type in Haskell

Yes, that is correct behaviour; if it was reflected in the type it'd be call safePerformIO.

Re:Ruby's Proposed STM by nextaccountic on Friday October 30, 2020 @04:22PM (#24946511)
> In any case, the unsafeness of unsafePerformIO and importing an FFI function as a pure function are not reflected in the type in Haskell.

It's reflected in safe Haskell annotations. Unfortunately nobody cares about those. Haskell has a culture problem regarding safety, just like most other languages, both high level and low level (with the exception of Java, that don't make data races UB)

Re:Ruby's Proposed STM by funcDropShadow on Sunday November 01, 2020 @07:01AM (#24958887)
Another reason why it is easier to integrate and use STM in Haskell than in most other languages, is the prevalence of immutable types. When only a tiny fraction of all memory structures needs to be tracked by STM as TVars, it becomes orders of magnitudes cheaper than in languages with predominantly mutable data structures.
Re:Ruby's Proposed STM by cat199 on Friday October 30, 2020 @07:12AM (#24941069)
> In any other language, all bets are off.

No, in any other language, one has to have an understanding of the codebase and what is mutating and what is not instead of relying on the language to do this work for them, which, granted, can be a lot more work.

dismissing things because 'i dont want to deal with it because it is hard' is not the same as dismissing it because 'this is not possible', this argument is the former.

see also: ocaml / scheme style FP, which can be 99.9% as pure-functional and type-checked as haskell if one wants to build their system that way.

Re:Ruby's Proposed STM by marcosdumay on Friday October 30, 2020 @03:41PM (#24946106)
Well, if you don't like the wording, translate "all bets are off" into "the programmer(s) must never make a mistake on those 10k to 10M lines on the code".

That's not a reasonable thing to expect.

Re:Ruby's Proposed STM by orthoxerox on Friday October 30, 2020 @10:26AM (#24942530)
Haskell should just have transactional IO, then. RDBMSs have had it since god knows how long ago.
Re:Ruby's Proposed STM by AaronFriel on Friday October 30, 2020 @10:48AM (#24942783)
Haskell is a general purpose programming language, so I'm not sure what you mean. You can't in the general case have "transactional IO".