Skip to content

Worker threads: closures as task callables - #83

Merged
nvms merged 1 commit into
mainfrom
closures
Sep 14, 2026
Merged

nvms merged 1 commit into
mainfrom
closures

Conversation

@nvms

@nvms nvms commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Part of #68.

A closure submitted to a pool travels as its compiled function, with every closure its body creates, serialized through the bytecode format and cached on the pool by compile name, plus its captures as a payload: use variables, $this, the scope markers, and for an arrow function the caller's variables its body names, snapshotted at submit because the VM resolves those at call time from a frame that does not exist on the other thread. Each worker loads the code once and binds a fresh instance per task. By-reference captures are refused; captures follow the same transfer rules as arguments.

Closure and anonymous class ids now come from a process-wide atomic. Worker threads compile their own bootstrap and load closures compiled on the main thread, so two compile units must never mint the same __closure_N.

Tests: tests/workers/closures.php in make workers (use captures, arrow functions, static closures, bound $this with private access, closures creating closures, repeated submits, exceptions, coercion, refusals, a closure consuming a channel). The memory soak gains repeated closure submits.

a closure submitted to a pool travels as its compiled function, with every
closure its body creates, serialized through the bytecode format and
cached on the pool by compile name, plus its captures as a payload: use
variables, $this, the scope markers, and for an arrow function the
caller's variables its body names, snapshotted at submit because the vm
resolves those at call time. each worker loads the code once and binds a
fresh instance per task. by-reference captures are refused.

closure and anonymous class ids come from a process-wide atomic now:
worker threads compile their own bootstrap and load closures compiled on
the main thread, so two compile units must never mint the same name
@nvms
nvms merged commit b2911b2 into main Sep 14, 2026
20 checks passed
@nvms
nvms deleted the closures branch September 14, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant