// Structured messages (issue #156): orders, each a record holding a list // of items and a string, passed along a pipeline of 8 actors, on all // threads. Three ways `send` handles a message (docs/runtime-abi.md): // - moved: each order is built for the send (count 1), so every hop takes // it over without copying, though the walk still visits each block; // - copied: the sender keeps every order, so the first hop copies it; // - promoted: kept orders of more than 64 blocks, which the first hop // makes shared, and later hops pass on without walking. import std/io import std/list import std/process.{type Subject, type proc} pub type Item { Item(sku: Int, qty: Int) } pub type Order { Order(id: Int, items: List, note: String) } fn items :: id: Int n: Int acc: List -> List { if n == 0 { acc } else { items id (n - 1) [Item(id + n, n), ..acc] } } fn order :: id: Int size: Int -> Order { Order(id, items id size [], "order " <> show id) } fn value :: o: Order -> Int { match o { Order(id, its, _) -> list.fold its id fn acc it { match it { Item(sku, qty) -> acc + sku * qty } } } } /// Forward every order to `next`. fn stage :: inbox: Subject next: Subject -> () / { process.send next (process.receive_forever inbox) stage inbox next } /// Add up batches of `batch` orders, reporting each batch's total. fn sink :: inbox: Subject batch: Int left: Int total: Int report: Subject -> () / { if left == 0 { process.send report total sink inbox batch batch 0 report } else { sink inbox batch (left - 1) (total + value (process.receive_forever inbox)) report } } /// A pipeline of `n` stages ending at `last`; its first inbox. fn pipeline :: n: Int last: Subject -> Subject / { if n == 0 { last } else { let ready = process.subject () let _ = process.spawn fn { let inbox = process.subject () process.send ready inbox stage inbox last } pipeline (n - 1) (process.receive_forever ready) } } /// Send `n` orders built for the send. fn send_fresh :: first: Subject i: Int n: Int size: Int -> () / { if i < n { process.send first (order i size) send_fresh first (i + 1) n size } } /// Send each of `orders`, which the caller keeps. fn send_kept :: first: Subject orders: List -> () / { match orders { [] -> () , [o, ..rest] -> { process.send first o send_kept first rest } } } fn build :: i: Int n: Int size: Int acc: List -> List { if i == n { acc } else { build (i + 1) n size [order i size, ..acc] } } /// Send `orders` and wait for the sink's total; the orders are used again /// after the send, so the sender keeps them. fn kept :: first: Subject report: Subject orders: List -> Int / { send_kept first orders let total = process.receive_forever report total - list.fold orders 0 fn acc o { acc + value o } } pub fn main :: -> () / { let batch = 8000 let report = process.subject () let ready = process.subject () let _ = process.spawn fn { let inbox = process.subject () process.send ready inbox sink inbox batch batch 0 report } let first = pipeline 8 (process.receive_forever ready) send_fresh first 0 batch 20 let moved = process.receive_forever report // Copied and promoted: the difference with the sender's own sum is 0. let copied = kept first report (build 0 batch 20 []) let promoted = kept first report (build 0 batch 100 []) io.println (show moved <> " " <> show copied <> " " <> show promoted) } //> 6774116000 0 0