Skip to content

let a host count batches and continuations - #50

Merged
hcollinsblox merged 9 commits into
mainfrom
scheduler-profiling-hooks
Oct 27, 2026
Merged

hcollinsblox merged 9 commits into
mainfrom
scheduler-profiling-hooks

Conversation

@hcollinsblox

Copy link
Copy Markdown
Contributor

Summary

SignalsScheduler.setHooks takes onBatch and onContinuations. Unset hooks are noops bound as upvalues, so an uninstalled hook costs what a constant did. runWork no longer calls the batch function itself, so the continuation count is taken after the batch has finished scheduling and before the drain starts.

Why this is useful on its own

A runtime that drives this scheduler wants to know how much work each batch turned into, and it cannot see that from outside. Putting a profiler inside the scheduler would couple the two. Host-supplied hooks keep the package ignorant of whoever is counting. The drain-by-index fix is the base because the count has to include work scheduled during the drain.

Use in Blox

Installed at module load when Debug.PROFILE is on:

SignalsScheduler.setHooks({
    onBatch = Profiler.onBatch,
    onContinuations = Profiler.onContinuations,
})

That is how the Blox profiler attributes “this frame spent N batches / M continuations”. Without the hooks, profiling the scheduler means forking it.

hcollinsblox and others added 2 commits August 6, 2026 15:00
Work run during a drain can call `schedule`, which appends to the table the
drain is walking. Generalised iteration is not guaranteed to reach entries
added after it started, so an effect scheduled by another effect could be
skipped for the rest of the batch and then discarded by the `table.clear`
that ends it.

Both drains now index by position, which re-reads the length each time round.

Co-authored-by: Cursor <cursoragent@cursor.com>
A runtime that drives signals wants to know how much work each batch turned
into, and it cannot see that from outside. `setHooks` takes the counters it
cares about, so nothing here needs to know about the profiler reading them.

They are bound as upvalues rather than read out of a table per batch, so a
hook that is not installed costs what a constant did. `runWork` no longer
calls the batch function itself, which lets the continuation count be taken
once the batch has finished scheduling and before the drain starts.

Co-authored-by: Cursor <cursoragent@cursor.com>
@hcollinsblox
hcollinsblox requested a review from a team as a code owner September 22, 2026 17:15
@hcollinsblox
hcollinsblox requested review from Gravemind1142 and removed request for a team September 22, 2026 17:15

@MisterUncloaked MisterUncloaked left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by my agent and reviewed by me.

Missing tests plus a few should-fix items inline.

Base automatically changed from scheduler-drain-by-index to main October 19, 2026 16:17
@hcollinsblox
hcollinsblox requested review from MisterUncloaked and removed request for Gravemind1142 October 21, 2026 16:04

@MisterUncloaked MisterUncloaked left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by my agent and reviewed by me.

LGTM, thanks. The hooks-off path now matches main. Two non-blocking notes: the description still says the count is taken before the drain, and doesn't mention that empty batches are no longer reported. Also, calling onBatch next to onContinuations after the drain, under the one hooksInstalled and #continuations > 0 guard, would drop the flag-off path that clears queued work when onBatch throws. Fine as a follow-up.

@hcollinsblox
hcollinsblox merged commit 3c78c7e into main Oct 27, 2026
1 check passed
@hcollinsblox
hcollinsblox deleted the scheduler-profiling-hooks branch October 27, 2026 18:39

This branch was successfully deployed

1 active deployment
integration — 5d75136c Deployed Oct 21, 2026 by hcollinsblox via ci #108
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.

2 participants