Concurrency in Haskell: Fast, Simple, Correct

https://news.ycombinator.com/rss Hits: 14
Summary

After nearly a decade of building embedded systems in C, C++, and Rust, I’ve somehow ended up writing Haskell for a living. If you’d asked me about functional programming a few years ago, I would have told you it was self-indulgent academic baloney—and then I stumbled into people using it for real-time systems where microseconds can mean literal life or death. I’m too old to try to convince people what tools they should use, but Haskell has some features that might interest anyone who cares about fast, correct code. Let’s talk about them. We’ll start with concurrency. Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now they have two problems. —Jamie Zawinski Some people, when confronted with a problem, think, “I know, I’ll use threads,” and then two they hav erpoblesms. —Ned Batchelder Like we’ve previously discussed, we have two main concerns when going fast: Your computer (even the one in your pocket) has many cores. To use the whole computer, you need to distribute work across them. The outside world is slow—networking and disk IO are many thousands of times slower than computing. Keep computing while you wait! And so, we need to break work into independent tasks, usually one of two ways: Compose the program into several threads of execution, traditionally scheduled and ran by the operating system. Compose the program as a series of callbacks, or continuations, that run once some other action (e.g., IO) completes. Option 2 has some nice performance benefits, especially when paired with event-driven IO. Watch Ryan Dhall introduce Node.js to the world—he doesn’t especially care about Javascript; he’s just trying to make this sort of concurrency more accessible. But continuation passing has its own problems. Even when syntactic sugar like async/await makes it appear to run sequentially, debugging can be a frustrating experience. Traditional stack traces go out the window, and you may ask yourself, “well, how did I get here...

First seen: 2025-04-17 03:23

Last seen: 2025-04-17 17:12