FERRETGame engine over Babylon.js
Ferret

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#

  1. In src/kernels.ts: defineTask('flight.step', (items: {x: number; v: number}[]) => items.map(...)).
  2. 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. Also reads/writes, sync: 'frame', every: n.
  3. 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.info under stays. Take the numbers out in gather, write the answer into the nodes in apply.

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": defineTask is not in src/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 module is not the scheduler's; it is schedule.

Files#