by pron · 11y ago · 0 comments
What we have here isn't reactive programming at all, but a few patterns (or, rather, anti-patterns) devised to avoid blocking kernel threads (only some of them are related to reactive programming). They manage to avoid blocking but at great cost. If the overhead caused by blocking bothers you, you need to understand why and realize there are far better ways of avoiding it than these anti-patterns.
Functional Reactive Programming is a programming model invented by Paul Hudak and Conal Elliott. As Conal says on C2. There are other reactive programming systems that are functional but not FRP.
Also note that people often use the term "reactive programming" to mean "the programming of reactive applications." Obviously, data flow doesn't have to be used to make something reactive, which is a property of the artifact and does not depend on how it was built.
Could you describe or at least point to some of them?
Instead of reaching out for ways to avoid blocking, we just make blocking free.
There is actually a library for java that adds it via byte code instrumentation (quasar or something) although not sure it will work for scala.
But saying that actor model is bad practice, I'm not sure that I accept it. Maybe on a single muticore but once you start talking distributed computing (e.g. Spark which is Akka based) then all this "avoid crossing to the kernel" optimization is becoming a drop in the ocean.
Here is an example of a small single threaded program beating out a number of distributed graph frameworks running on 128 cores, with a 128 billion edge dataset.
http://www.frankmcsherry.org/graph/scalability/cost/2015/02/...
Performance matters because it enables simplicity. If your language forces you to pull in multiple machines to solve your problem then its turned a simple program into a distributed system and life gets complicated fast. Just throwing more cores at a program without understanding why its slow will just get you into trouble.
Multithreaded programs and distributed programs should be a scary last resort after making absolutely sure you can't get away with the simple solution.
Yes, but in this context, they provide the same performance benefits as the asynchronous techniques mentioned in the article, without all the drawbacks of the cumbersome asynchronous style.
> But saying that actor model is bad practice
The actor model is a general technique for fault-tolerance, and it's great. How it handles concurrency, though, is an orthogonal concern. Akka has asynchronous (callback based) actors, and callbacks are an anti-pattern. Erlang has synchronous (blocking) actors, which are so much simpler, and don't have all the complications associated with asynchronous code.
"Free blocking" was what attracted me to Erlang's actors in green threads as opposed to Akka's actors that block entire threads when they block.
However, there is one thread you don't want to block: the UI thread. We do need ways of getting things to the UI asynchronously.
Well, not if you need tens-of-thousands of them or more.
> However, there is one thread you don't want to block: the UI thread.
No problem. You simply multiplex fibers onto the UI thread, where they can block all they want. Here's an example in Quasar (Java):
FiberScheduler uiScheduler = new FiberExecutorScheduler("UI-scheduler",
task -> EventQueue.invokeLater(task)); // schedule on the UI thread
new Fiber(uiScheduler, () -> {
for (i = 0;; i++) {
assert EventQueue.isDispatchThread(); // I'm on the UI thread!
uiLabel.setText("num: " + i); // I can update the ui
Fiber.sleep(1000); // ... yet I can block all I want
}
}).start();