As of SCUA 0.43.0, a large frozen value is shared between partitions. Freeze a record of columns, a table or an array of 64 KiB or more and send it to four workers, and all four read the one value in place. A 12.8 MB record of 24 columns reaches four workers in under a millisecond, and the data the workers read stays in the processor's cache.
#What a fan-out used to cost
Each partition has its own heap, and a message is copied into the partition that receives it. A copy of a few thousand numbers takes a few microseconds and doesn't matter.
A fan-out sends the same data to every worker, so four workers given
a large table get four copies of it, and with the sender's own there are
five in memory. A copy of a hundred thousand numbers takes longer than a
typical job's work, so a fan-out that sends large data with every
ask spends most of each round copying, and adding workers
barely helps. Until this release, a frozen value travelled the same
way.
The manual's advice for that is to send large data once and keep it
in each worker, or to put it in a top-level const, which
every partition reads in place. A const has to be ready
before the partitions start, though, and data you load or compute while
the program runs can't be one. From 0.43.0, freeze gives
that data the sharing a const has.
#freeze now shares anything over 64 KiB
Freeze an array, a table, a record or a string of 64 KiB or more (1
MiB on Windows), counting everything inside it, and every partition you
send it to in a tell, an ask or a reply reads
that one value. Here a report freezes two columns of a million rows and
gives them to four workers, each of which adds up its quarter:
fn columns(n)
let price: { f64 } = []
let qty: { i32 } = []
for i in 0:n do
push(price, (i % 997) * 0.25)
push(qty, i % 13)
end
return { price = price, qty = qty }
end
partition Worker
state cols = nil
ask Load(c)
cols = c
return len(cols.price)
end
ask Revenue(lo, hi)
let s = 0.0
for i in lo:hi do s = s + cols.price[i] * cols.qty[i] end
return s
end
ask Change()
return pcall(fn() cols.price[0] = 1.0 end)
end
end
partition Report
on Go(n)
let cols = freeze(columns(n))
let ws = [Worker(), Worker(), Worker(), Worker()]
print(`loaded {map_all(ws, fn(w) return ask w.Load(cols) timeout 10s end)}`)
let share = n // 4
let parts = map_all(range(0, 4), fn(k) return ask ws[k].Revenue(k * share, (k + 1) * share) timeout 10s end)
print(`revenue {parts}`)
print(ask ws[0].Change() timeout 10s)
end
end
tell Report().Go(1000000)
$ scua columns.scua
loaded [1000000, 1000000, 1000000, 1000000]
revenue [186610382.75, 186701382.0, 186793213.25, 186885876.5]
Error(this array is frozen (it came from a `const` or `freeze`), so it can't be changed. Change a copy instead: `let mine = …[0:]`)
$ scua --mem-stats columns.scua 2>&1 | grep -oE 'shared=[0-9]+|shared-holds=[0-9]+|freezes-[a-z]+=[0-9]+|all-live=[0-9]+'
all-live=63212280
shared=12583560
shared-holds=5
freezes-moved=1
freezes-copied=0
shared= is the 12.6 MB of columns, counted once, and
shared-holds=5 is the report and its four workers holding
it. all-live= still charges the whole record to each
partition that holds it, which is why it is five times as large.
Nothing about how you write the code changes. The workers keep the
record in state and index it like any other, and it is
frozen in every partition, so a write faults with the same message
wherever it is made, as the last line shows. A value that isn't frozen
is still copied, which is what lets the receiver change it.
#A value nothing else holds is moved, not copied
freeze gives you a frozen copy and leaves the original
as it was, so the original can still change. When nothing else holds the
original, there is nothing to keep, and freeze now freezes
the value where it is. That is the case for a value made in the function
that freezes it and not used after the freeze: a literal, a
fill, a string joined with ..,
bytes read with fs.read, or the result of a
function in the same file that only ever returns values like these.
columns above is one, so the report's freeze
copied nothing.
At 64 KiB or more, a moved value goes into the shared area, and a
large array's or bytes value's storage goes with it
untouched. Freezing a freshly built 3.2 MB array of numbers takes no
measurable time. --mem-stats tells you which way each
freeze went:
fn numbers(n)
let xs: { f64 } = []
for i in 0:n do push(xs, i * 0.5) end
return xs
end
-- built here and not used again: frozen where it is
let fresh = freeze(numbers(400000))
-- `mine` changes after the freeze, so freeze copies it
let mine = numbers(400000)
let kept = freeze(mine)
push(mine, 1.0)
print(len(fresh), len(kept), len(mine))
$ scua moved.scua
400000 400000 400001
$ scua --mem-stats moved.scua 2>&1 | grep -oE 'freezes-[a-z]+=[0-9]+'
freezes-moved=1
freezes-copied=1
When freeze does copy, the copy now goes straight into
the shared area. Freezing the 12.8 MB record that way takes 3 to 4
ms.
#Compiled loops read either kind
A frozen array of 64 KiB or more is shared and a smaller one is not,
so a function can be handed a shared array on one call and an ordinary
one on the next. A loop that reads an array by index or with
for … in stays compiled for both. The optimising JIT
compiles a loop over a shared frozen array as it does a loop over an
array the partition built, and a method call on a shared frozen record
stays in compiled code.
In our tests, a gradient-boosted tree trainer that freezes the list of rows it sends with each split trains a tree in 77 ms on four workers, the same as it takes without the freezes.
#Where SCUA still copies
- A frozen value under 64 KiB (1 MiB on Windows) is copied into each receiver. A copy that size takes microseconds.
- A frozen value that holds an
Okor anErroranywhere inside it is copied. - An embedded script copies every frozen value.
freezeitself copies a parameter, a value stored somewhere else and a local used after thefreeze. To get the move, freeze the value in the function that builds it, asfreeze(columns(n))does above.
If freeze finds no room to share a value, it copies it
instead, and --mem-stats counts that as
shared-fallbacks=.
#Trying it
Install SCUA 0.43.1, freeze the large data your partitions share, and
run once with --mem-stats. shared= shows what
is being read in place, and freezes-copied= shows any
freeze you expected to be free that made a copy.
Send large data once in the manual shows the patterns for a fan-out, and freeze has the exact rules for what is moved and what is shared. The changelog lists everything else in 0.43.0.