Repository navigation
Description
Context
Years ago, there was talk of using Hopac inside Suave. I use Hopac in my own apps, so I tried a breaking, 4.0-style rewrite on a fork — not a drop-in for master, just a curiosity I wanted to share.
Fork / branch: eacasanovaspedre/suave@hopac
The interesting part is not raw RPS (plaintext is a wash). It is that WebParts can be nacked, which original Suave never did. Also, if like me, you also have a lot of code using Hopac or just like it, it's a natural fit to WebParts based on Hopac.
What the experiment is
master (today) |
this fork | |
|---|---|---|
WebPart<'a> |
'a -> Async<'a option> |
'a -> Hopac.Alt<'a option> |
| Sequential routing | choose |
fallback (choose kept as an alias) |
| Concurrent race | not first-class | Hopac Alt.choose / `< |
| Timeouts | Async.WithTimeout — child keeps running |
timeoutWebPart nacks the child |
| Connection abort | close the socket | HttpContext.abort (IVar) + abortToken |
| Socket I/O | ValueTask / SocketOp |
unchanged (ValueTask) |
| Connection loop | Task |
Hopac Job (so waits park the Job, not a thread) |
choose is still sequential first-match. Concurrent choice is Hopac’s Alt.choose, which is a different operator on purpose.
I also tried turning SocketOp into Job. Plaintext dropped (~0.67× at 50 connections). I reverted that; sockets stay ValueTask.
Benchmarks vs master
Same machine, same load generator, same apps.
- CPU: Intel Core Ultra 9 285K (24 cores), Windows
- Client: bombardier 2.0.2,
--http1, keep-alive, loopback:3000 - Server: Release, Server GC, 1 acceptor
- Baseline:
master@edb9c743 - Fork:
hopac(Jobs for connections, ValueTask sockets)
Plaintext — benchmarks/PongServer (OK "PONG", no routing tree)
Warmup 10 s discarded, median of 3 × 30 s.
| Connections | hopac | master | hopac / master |
|---|---|---|---|
| 50 | 116.1k RPS · 417 µs mean | 112.0k RPS · 431 µs mean | 1.04× |
| 200 | 173.4k RPS · 1.15 ms mean | 177.7k RPS · 1.12 ms mean | 0.98× |
Zero non-2xx on both. Within noise: the extra abort Alt.choose + Hopac scheduler on an already-completed OK does not show up.
Mixed handlers — benchmarks/LoadMix
Warmup 5 s, average of 2 × 15 s. This is the workload that actually uses timers / races.
| Route | What it does | Conns | hopac | master | master / hopac |
|---|---|---|---|---|---|
/ok |
OK "PONG" through choose |
200 | 178k | 177k | 1.00× |
/compute |
small CPU loop | 200 | 151k | 158k | 0.96× |
/sleep |
10 ms wait | 500 | 29.3k · ~17 ms | 32.1k · ~15.6 ms | 0.91× |
/race |
5 ms vs 50 ms, loser cancelled | 200 | 12.2k · ~16.3 ms | 12.9k · ~15.4 ms | 0.95× |
/fanin |
8 × 5 ms in parallel | 200 | 12.1k · ~16.8 ms | 12.9k · ~15.4 ms | 0.94× |
Master’s ~15.5 ms floor on 5–10 ms sleeps is Windows timer resolution (200 conn / 15 ms ≈ 13.3k, which is exactly /race and /fanin on master). Hopac’s 10 ms timer is a bit more accurate when the request completes; RPS is still in the same band.
/timeout (20 ms nack of a 200 ms child) returns 408 in ~30 ms on both. On the fork the child Alt is actually nacked; on master, Async.WithTimeout still leaves the child running.
How to reproduce
# hopac tree
dotnet build -c Release benchmarks/PongServer
dotnet run -c Release --no-build --project benchmarks/PongServer -- 1
# other shell:
bombardier --http1 -c 200 -d 30s -l http://127.0.0.1:3000/
dotnet run -c Release --project benchmarks/LoadMix -- 1
bombardier --http1 -c 200 -d 15s -l http://127.0.0.1:3000/race
bombardier --http1 -c 500 -d 15s -l http://127.0.0.1:3000/sleep
Against master, benchmarks/LoadMix/Program.Master.fs is the Async twin of the same routes (timers via Async.Sleep / Async.Choice / Async.Parallel).
Try the nack demo
dotnet run --project examples/HopacDemo
# listens on http://127.0.0.1:8080/
┌────────┬──────────┬────────────────────────────────────────────────────────────────────────────────┐
│ Method │ Path │ What it shows │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ / │ listing │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /hello │ combinators (succeed / >=>) │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /job │ job { do! timeOutMillis 150; … } │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /race │ Alt.choose of 40 ms cache vs 400 ms db — cache wins, db is nacked (server log) │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /add/2/3 │ pathScan + job │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /timeout │ timeoutWebPart 2 s over a 30 s child → 408, child logs nacked │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /long │ 20 s Alt │
├────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┤
│ GET │ /cancel │ nacks every waiter on /long over a Ch<string> │
└────────┴──────────┴────────────────────────────────────────────────────────────────────────────────┘
# one-shell nack (no extra client)
curl -i http://127.0.0.1:8080/timeout
# expect HTTP/1.1 408 after ~2s, not 30s
# server: [timeout-child #1] nacked
# two-shell nack
curl http://127.0.0.1:8080/long
# other terminal:
curl http://127.0.0.1:8080/cancel
# first curl returns: long #1 nacked: GET /cancel
# server: [long #1] timer nacked
timeoutWebPart is a real Alt.choose of the child against timeOut; the loser is nacked. /cancel is the same idea with an explicit channel, plus ctx.abort if the connection dies.Takeaway
For Suave’s current “parse, OK, write” hot path, Hopac is not a speed win — and turning sockets into Jobs was a loss. Where it is different is cancellation of in-flight handlers, which Async WebParts never had.
I am not proposing this as a PR. It is a breaking rewrite (WebPart, choose vs Alt.choose, Hopac 0.5.1, no more >>=). Sharing in case anyone still curious about that old discussion wants to poke the demo or the numbers.
Happy to answer questions or close this if it is noise.
Activity
ademar commentedon Sep 17, 2026
Interesting. You didn't touch
HttpReaderwhere the bulk of the parsing work happens; same withHttpOutput, all the upstream writing is done there. I wonder how it will react if you propagate Hopac further into these classes.Though I am afraid the impedance on the Hopac/Task boundary it is always gonna be a problem.
eacasanovaspedre commentedon Sep 17, 2026
HttpOutput.runis written in Hopac. I pushed the migration further, but deeper in the stack there are ValueTasks that Hopac cannot match, because they avoid allocations. Hopac would need something like aValueJobfor values that are "almost always already computed".I have been making some improvements to Hopac for my own use, and I have been considering a struct-based Job for cases like this. I stopped converting to Hopac whenever I hit ValueTasks or BCL Tasks from C# that I could not replace.
If the whole stack—including the low-level BCL pieces—were written in Hopac, performance would likely be even better, but that is never going to happen.
The real gain is replacing F# Async with Hopac. Instead of mixing Tasks and Asyncs, this experimental branch now uses Tasks and Hopac. Performance is preserved, and you get a more powerful concurrency library.
I'll create a package and use it in one of my personal Suave projects. I'll see how it behaves in a real-life scenario. If I ever manage to create a
ValueJob, I'll push deeper and see what comes out.