A fn that loops or recurses, and uses only its parameters and other fns, runs in a worker when an action, handler, task or resource calls it. Nothing is written for that: no messages, no async. main fn keeps one on the page; off fn sends one that would have stayed.
The same work, three ways
fn, off fn, main fnCount the primes up to n with each. The walking light is moved by a timer on the page's own thread: it keeps walking while a worker counts, and stands still while main fn does.
main thread tick 0
nprimes up to 8,000,000
fn primes(n: number): numbera worker, by itself: it loops
not run yet
off fn primes(n: number): numbera worker, because it says so
not run yet
main fn primes(n: number): numberthe page's own thread
not run yet
Run one.Try to move the slider or scroll while each runs.
Heavier things
A picture, computed into numbersThe Mandelbrot set, 180 by 120 points in ten bands. Each band is one call of a fn that returns number[]; a surface draws what has arrived. Press the picture to zoom in there.
main thread tick 0
main fn instead
band 1 of 10
most steps for a point40000: the black middle costs this much each
centre -0.60000, 0.00000 · 3.20 wide. With the worker the bands arrive one by one and the light walks on; with main fn the page waits for all ten.
A fn that calls itselfHow many ways n queens stand on an n by n board, none attacking another. It recurses, so it is a worker's too.
main thread tick 0
n13 by 13
One way to stand them is drawn. Count them all.
A resource over a worker fnIt loads again when the state its call reads changes. A run that is overtaken is called off: its worker calls not yet started never start, and its answer is never shown.
main thread tick 0
thousandsdrag it about: every stop starts a run
…primes under 400,000
pending(under)
The slider has moved 0 times; only the last value's answer is kept.
Only the newest
action … newestEvery move of the slider starts a run, and each waits for a worker. With newest, a run that is still waiting when the next starts is called off: what it did before its wait stays (it counted itself), nothing after it is done, so only the last value's answer is shown, whatever order the answers come in.
main thread tick 0
hundred thousandsprimes under 3,000,000
0runs started
0answers shown
0called off
–primes under 3,000,000
Drag the slider about: many runs start, one answers. Without the word, every run would come to its end and write what it found, the last to arrive winning.
wasm fn
Experimental. A fn that says wasm is compiled to WebAssembly as well as JavaScript, and gives the same answers.
The same kernel, both waysEach kernel is written twice in pages/lang_wasm.mote, once with the word and once without, and both are kept on the page (fns main), so now() times the work alone. Each press runs both three times, in turn, and shows the best of each. The page stands still meanwhile: these are on its own thread on purpose. What it shows is this browser on this machine, today: on the machine bench/README.md was measured on, sorting was 1.34x faster as WebAssembly in Chrome and the matrix 0.81x (slower); the first calls, made before the module is ready, run as JavaScript.
Not run yet. Pick a kernel and run both.
Mote's numbers are all 64-bit floats; a wasm fn keeps a number that is only ever whole as a whole number, and checks each step stays exact (the call is run again as JavaScript if one does not). See docs/language.md, wasm fn, for what can go in one.
What it costs
A fn too small for the tripIt loops, so it is sent; but it does three additions. Called 400 times, the messages cost far more than the work. This is the one to mark main fn.
–through a worker
–on the page
Run both.
fns main, once for a fileA file of many small fns says it once, before its first fn: fns main keeps every plain fn below it on the page, fns off sends each that can leave, fns auto is the rule. A fn's own main or off still wins. In mote.toml, fns = "main" is the same for the project's files, and [fns] for a package.
The same 400 calls as the card beside, with no worker trip each: compare its time for the worker.
mote check --perfThe compiler says which named values are copied to workers, and what mote dev measured on the pages that ran: a fn whose messaging costs more than its work is named. This is what it said of this page's fns after one visit (the names carry the page's prefix).
copied to a worker: nothing with a name. Every value passed is moved, or made where it is passed.
measured in `mote dev`, for each call:
fn langWorkersPrimes 5 calls work 313 ms messaging 0.10 ms
fn langWorkersPrimesOff 1 call work 1454 ms messaging 0.20 ms
fn langWorkersQueens 1 call work 679 ms messaging 0.10 ms
fn langWorkersMandel 10 calls work 54 ms messaging 1.23 ms
fn langWorkersTiny 400 calls work 0.00 ms messaging 0.01 ms messaging dominates: `main fn langWorkersTiny` keeps it on the page
work is time inside the fn; messaging is what it took to send the arguments and bring the answer back. A fn that is called often and does little is better as main fn.