perf: create the AbortController, drain, and binding Map only when used - #26
Merged
Merged
Conversation
Closes #25. The AbortController is created by cancel() or the first signal read; internal checks treat an absent controller as not aborted. drain() runs only when the Job has children. A single-binding ContextFrame holds one key/value instead of a Map. Per-Job (Bun): plain 1.12 -> 0.71 us, HandoffJob with signal and one provided value 1.67 -> 1.20 us. Node: 0.97 -> 0.79 us and 1.41 -> 1.20 us.
Closes #25. Incoming cancellation is now one component. Cancellation owns a Job's signal, the sources it follows, and its deadline, each created on demand; Job keeps ownership, phases, failures, children, and the result, and every delivered cancellation still goes through Job.cancel() so cascades stay the Job's. CancellationBindings is folded into it. drain() runs only when the Job has children. A single-binding ContextFrame holds one key/value instead of a Map.
Merged
miinhho
added a commit
that referenced
this pull request
Sep 12, 2026
Breaking since 0.2.1: Job is a single-result lifetime (#22) — the Published type parameter, the publisher body argument, JobPublisher, and Job.value() are gone; HandoffJob is the one-shot handoff. #24 and #26 change no public behavior. The README is rewritten for a first-time reader: the model in one paragraph, Jobs grouped by ownership / lifecycle / results / failure, cancellation before context, Supervisor and TaskGroup with their policies, HandoffJob last, and an API summary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #25.
Three eager resources every Job paid for without using them are now created on demand, and the state behind them lives in one place.
Cancellation(replacesCancellationBindings) holds one Job's incoming cancellation. After #24 the pieces of "incoming cancellation" had spread acrossJob: the controller, two pending-source fields, the bindings bag, and ~10 methods (aborted,abortReason,throwIfAborted,isCancellation,isExternalCancellationSource,observeCancellation,recheckCancellation, deadline scheduling insideprepare, controller creation insidecancel). They are now one component with one contract:signal— the Job's signal; reading it subscribes to followed sources. TheAbortControlleris created here or byabort(), never before.follow(inherited, external)/recheck()/observe()— record, honor without subscribing, subscribe.deadline(at)— owns the timer; elapsing cancels the target with aTimeoutError.aborted/reason/current/throwIfAborted()/isCancellation(error)— what Job bookkeeping asks.dispose()— releases subscriptions and the timer; the signal keeps its state.Every delivery goes through
CancellationTarget.cancel()— the owning Job — so cascades stay the Job's. The component owns no lifetime and no completion state; AGENTS.md's support-state rule is reworded to say exactly that.Jobkeeps ownership, phases, failures, children, and the result, and holds the component lazily: a plain Job without a seed signal, deadline, or cancellation never creates it.Two consequences of the consolidation:
prepare()no longer readsinherited.signalfor it (which used to materialize the parent's controller). Only an explicitly suppliedoptions.contextis a foreign source, and one that hands back the owner's signal is still excluded.parent.cancellation?.observe()rather than by readingparent.signal, so the parent gains a subscription without gaining a controller.drain()runs only with children. Admission closes whenphasebecomes"closing", so the check cannot race a late child.A single-binding
ContextFrameholdskey/value, not aMap. Frame bindings are an internalBindings { has, get, keys };Mapsatisfies it for several entries,SingleBindingfor one. Lookup order and shadowing are unchanged.No public behavior changes.
Measured
Per-Job microbenchmark (400k iterations,
/tmp/job-bench.mjs), packedmain(post-#24) vs this branch:signal()readvalues: [provide(...)](server shape)A flat variant that kept the fields on
Jobmeasured 1.20 µs on both runtimes for the server shape; the component costs one small allocation per Job that has a seed signal (~0.1 µs). The consolidation is worth that.Verification
pnpm format,pnpm check,pnpm buildpnpm test— 11 files, 131 tests, unchangedpnpm pack; servertypecheck,vitest --project @tiberjs/server(12/12),leak-probe(live 0, graph 0) against the packed tarball