On all the cores
Skill ferret-scheduler · download all skills · SKILL.md
When to use#
The game's thread is the narrow place and the work is a calculation over many things (units, bodies, cells); writing an engine module that should spread over the cores; finding what holds the frame.
The model#
Work runs in tasks: pure functions on plain data (numbers, strings, lists, plain objects, typed arrays) registered by name (registerKernels, in src/kernels.ts of a package or the game; the build puts that file in the page and the workers). world.scheduler lays the due tasks of a step over the cores: one that reads what another writes waits, the others run together, the step ends at a barrier, so the result is the same for any number of cores (none: they run in the game's thread). Workers start by themselves (cores less two, 1 to 7; ?workers=N or the workers option sets it). Pages are cross-origin isolated (COOP/COEP), so data is shared memory; without it, copies in messages, same result.
An engine module#
A service or component has schedule: ModuleDecl: name, parts (each a KernelDecl: task name, sync 'step' (the step waits) or 'frame' (answer applied a step later), reads/writes, split (independent items cut in pieces, merged in order), resident (a state kept in the worker), gather (data out of the world), apply (answer back)) and stays: in words what stays in the game's thread. Service.whyHere() says why it stays right now. scheduler.info lists it all.
Splitting your own calculation#
- In
src/kernels.ts:defineTask('flight.step', (items: {x: number; v: number}[]) => items.map(...)). - In the game:
world.scheduler.task({ name: 'flight.step', each: true, gather: w => items, apply: (w, out) => { items = out; } }).each: true: a list of independent items, cut over the cores, answers joined in order. Alsoreads/writes,sync: 'frame',every: n. - A task given a node, a mesh, the world, a function or an object of a class cannot leave the game's thread. The engine says so once ("the task "flight.step" runs in the game's thread: the input.node is a Mesh object of the scene ..."), runs it there and lists it in
scheduler.infounderstays. Take the numbers out ingather, write the answer into the nodes inapply.
Where the frame goes#
world.perf.enabled = true (or ?perf); time your functions with world.perf.own('flight', () => ...); perf.report gives rows and own: every row of at least 2 % of the frame with its share and a sentence (an own function: how to make a task; a module: what stays and why; other code: no module is declared). Compare times only within one run.
Check afterwards#
scheduler.info: threads above 0, your task in declared, stats.inWorkers growing, stays empty or as meant. With ?workers=0 the result is the same.
What usually goes wrong#
- "no kernel ... is registered":
defineTaskis not insrc/kernels.ts(main.ts is not in the worker bundle). - Items that read each other's new values must not use
each. - A service property named
moduleis not the scheduler's; it isschedule.
Files#
reference/world.md— World, the fixed step, entities and components, services, the event bus and registries.reference/api-core.md— every export of@ferret/corewith signature and doc commentreference/api-core-2.md— every export of@ferret/corewith signature and doc commentreference/commands.md— 21 commands of these packages with their arguments