| @@ -0,0 +1,109 @@ |
| 1 | +// Structured messages (issue #156): orders, each a record holding a list |
| 2 | +// of items and a string, passed along a pipeline of 8 actors, on all |
| 3 | +// threads. Three ways `send` handles a message (docs/runtime-abi.md): |
| 4 | +// - moved: each order is built for the send (count 1), so every hop takes |
| 5 | +// it over without copying, though the walk still visits each block; |
| 6 | +// - copied: the sender keeps every order, so the first hop copies it; |
| 7 | +// - promoted: kept orders of more than 64 blocks, which the first hop |
| 8 | +// makes shared, and later hops pass on without walking. |
| 9 | +import std/io |
| 10 | +import std/list |
| 11 | +import std/process.{type proc, type Subject} |
| 12 | + |
| 13 | +pub type Item { Item(sku: Int, qty: Int) } |
| 14 | +pub type Order { Order(id: Int, items: List<Item>, note: String) } |
| 15 | + |
| 16 | +fn items(id: Int, n: Int, acc: List<Item>) -> List<Item> { |
| 17 | + if n == 0 { acc } else { items(id, n - 1, [Item(id + n, n), ..acc]) } |
| 18 | +} |
| 19 | + |
| 20 | +fn order(id: Int, size: Int) -> Order { |
| 21 | + Order(id, items(id, size, []), "order " <> show(id)) |
| 22 | +} |
| 23 | + |
| 24 | +fn value(o: Order) -> Int { |
| 25 | + match o { |
| 26 | + Order(id, its, _) -> list.fold(its, id, fn(acc, it) { match it { Item(sku, qty) -> acc + sku * qty } }), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Forward every order to `next`. |
| 31 | +fn stage(inbox: Subject<Order>, next: Subject<Order>) -> () / <proc> { |
| 32 | + process.send(next, process.receive_forever(inbox)) |
| 33 | + stage(inbox, next) |
| 34 | +} |
| 35 | + |
| 36 | +/// Add up batches of `batch` orders, reporting each batch's total. |
| 37 | +fn sink(inbox: Subject<Order>, batch: Int, left: Int, total: Int, report: Subject<Int>) -> () / <proc> { |
| 38 | + if left == 0 { |
| 39 | + process.send(report, total) |
| 40 | + sink(inbox, batch, batch, 0, report) |
| 41 | + } else { |
| 42 | + sink(inbox, batch, left - 1, total + value(process.receive_forever(inbox)), report) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// A pipeline of `n` stages ending at `last`; its first inbox. |
| 47 | +fn pipeline(n: Int, last: Subject<Order>) -> Subject<Order> / <proc> { |
| 48 | + if n == 0 { |
| 49 | + last |
| 50 | + } else { |
| 51 | + let ready = process.subject() |
| 52 | + let _ = process.spawn(fn() { |
| 53 | + let inbox = process.subject() |
| 54 | + process.send(ready, inbox) |
| 55 | + stage(inbox, last) |
| 56 | + }) |
| 57 | + pipeline(n - 1, process.receive_forever(ready)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Send `n` orders built for the send. |
| 62 | +fn send_fresh(first: Subject<Order>, i: Int, n: Int, size: Int) -> () / <proc> { |
| 63 | + if i < n { |
| 64 | + process.send(first, order(i, size)) |
| 65 | + send_fresh(first, i + 1, n, size) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Send each of `orders`, which the caller keeps. |
| 70 | +fn send_kept(first: Subject<Order>, orders: List<Order>) -> () / <proc> { |
| 71 | + match orders { |
| 72 | + [] -> (), |
| 73 | + [o, ..rest] -> { |
| 74 | + process.send(first, o) |
| 75 | + send_kept(first, rest) |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn build(i: Int, n: Int, size: Int, acc: List<Order>) -> List<Order> { |
| 81 | + if i == n { acc } else { build(i + 1, n, size, [order(i, size), ..acc]) } |
| 82 | +} |
| 83 | + |
| 84 | +/// Send `orders` and wait for the sink's total; the orders are used again |
| 85 | +/// after the send, so the sender keeps them. |
| 86 | +fn kept(first: Subject<Order>, report: Subject<Int>, orders: List<Order>) -> Int / <proc> { |
| 87 | + send_kept(first, orders) |
| 88 | + let total = process.receive_forever(report) |
| 89 | + total - list.fold(orders, 0, fn(acc, o) { acc + value(o) }) |
| 90 | +} |
| 91 | + |
| 92 | +pub fn main() -> () / <io, proc> { |
| 93 | + let batch = 8000 |
| 94 | + let report = process.subject() |
| 95 | + let ready = process.subject() |
| 96 | + let _ = process.spawn(fn() { |
| 97 | + let inbox = process.subject() |
| 98 | + process.send(ready, inbox) |
| 99 | + sink(inbox, batch, batch, 0, report) |
| 100 | + }) |
| 101 | + let first = pipeline(8, process.receive_forever(ready)) |
| 102 | + send_fresh(first, 0, batch, 20) |
| 103 | + let moved = process.receive_forever(report) |
| 104 | + // Copied and promoted: the difference with the sender's own sum is 0. |
| 105 | + let copied = kept(first, report, build(0, batch, 20, [])) |
| 106 | + let promoted = kept(first, report, build(0, batch, 100, [])) |
| 107 | + io.println(show(moved) <> " " <> show(copied) <> " " <> show(promoted)) |
| 108 | +} |
| 109 | +//> 6774116000 0 0 |