Skip to content

Report event dispatches to the dependency graph as emitEvent - #242

Merged
Domiii merged 3 commits into
mainfrom
dominik/node-dg-events
Nov 17, 2026
Merged

Domiii merged 3 commits into
mainfrom
dominik/node-dg-events

Conversation

@Domiii

@Domiii Domiii commented Nov 17, 2026 •

Copy link
Copy Markdown

🤖

Summary

  • Problem: node event dispatches are not in the dependency graph.
    • Work a listener schedules depends on whatever execution happens to enclose the emit.
  • Solution: every dispatch with listeners runs its listener calls in an execution of a new emitEvent{type} node.
    • Chains through stream and socket events now name the event, e.g. emitEvent(data).

Kind

  • emitEvent{type}, added to the backend by backend#13927.
  • Not Chromium's dispatchEvent, because the backend reads dispatchEvent as DOM input.
    • dependencyGraph.ts knownOriginNode ends chains at dispatchEvent of input types: click, change, keypress, input, submit, …
    • Node emits those names outside of input: fs.watch 'change', readline 'keypress', user emitters.
  • emitEvent is not an origin. Chains continue through its execution parent, e.g. asyncCallback.

Sites

  • EventEmitter.prototype.emit, once a handler exists
    • Every node-style emitter dispatches here, including streams, sockets and process.
  • EventTarget kHybridDispatch, once listeners exist
    • dispatchEvent, NodeEventTarget#emit and MessagePort messages all call it.
  • Both: begin(type); try { listener calls } finally { end() }.

Dependency executions

  • One primitive, DependencyExecution, in src/node_dependency_graph.h:
    • Begin: save the execution depth. Begin an execution only if there is a node.
    • End: unwind to the saved depth.
      • This also ends executions begun inside it whose own end was skipped.
  • The async_hooks windows of node#240 and the dispatches both use it.
  • Dispatches live on a C++ stack.
    • Nothing is returned to JS. So JS sees the same values when recording and replaying.
    • A dispatch without a node, before the first checkpoint, keeps an entry that never began. So an end always pops its own entry.
  • A window pop also drops the dispatches its unwind ended.
    • Example: a termination skips the finally of a dispatch. The dispatch then ends with its window.
  • An end first drops dispatches that an unwind already ended. Then it ends the innermost remaining one.

Event type

  • Strings are reported as is.
  • Symbols are reported by their description, or "" without one.
  • Other values are reported as "".
    • Converting them could run user toString in replay only.

Gates

  • Same as node#240.
  • JS gate: the boolean in internal/async_hooks, read once in prepareExecution.
    • Unrecorded runs: 2 checks of a false boolean per dispatch with listeners. 0 binding calls.
    • Recordings: 2 binding calls per dispatch with listeners.
  • C++ gate: DependencyGraphEnabled().
    • Nodes and dispatch entries exist only while it holds.

Upstream merge surface

  • emit: the single-listener branch is re-indented into a try. The multi-listener branch ends the dispatch in its existing finally.
  • kHybridDispatch: the listener loop is re-indented into a try.
    • The other options both cost something:
      • A helper function adds a call and a JS-visible stack frame on the unrecorded path.
      • Dropping the finally leaves the dispatch open after an exception.
  • Expect conflicts there when merging upstream node.

Data

run emit ×1e6 dispatchEvent ×1e5
recorded 78 89
recorded, RECORD_REPLAY_DISABLE_FEATURES=["dependency-graph"] 59 82
unrecorded (RECORD_REPLAY_DONT_RECORD=1), main nodejs#596, median of 5 23 17
unrecorded, nodejs#603, median of 5 24 15
  • Cost, as rate × scale:
    • Recordings: about 19 ns more per emit and about 70 ns more per dispatchEvent.
    • Unrecorded runs: within noise.
  • Replay of dependency-graph/await-fetch in test-node-recording, ms:
build case time
main nodejs#596, node#240 only 2718
nodejs#602, this PR 2697
nodejs#603, top of the stack 2607
  • Not measured: the dependency graph's node count.

The listener calls of EventEmitter#emit and of EventTarget dispatches run
in a dependency execution of a dispatchEvent{type} node, the kind Chromium
reports synchronous event dispatch with. So the work a listener schedules
depends on the dispatch, and the chain continues to the callback that
emitted it.

- Sites: EventEmitter.prototype.emit once a handler exists, and
  EventTarget's kHybridDispatch once listeners exist, which dispatchEvent,
  NodeEventTarget#emit and MessagePort messages share. The calls end in a
  finally.
- The depths before each dispatch begins stay on a C++ stack. An end drops
  the dispatches an outer unwind already ended and unwinds to the innermost
  remaining one. Nothing is returned to JS.
- type: a string, or a Symbol's description. Other values are not
  converted, which could run JS.

Prod behavior. Unrecorded runs: two checks of a false boolean per dispatch
with listeners, no binding calls. Recordings: two binding calls per
dispatch, which return after checking a cached flag; nodes and depths only
exist when replaying. No JS-visible change.
…tion primitive

- Kind emitEvent{type} instead of dispatchEvent{type}. The backend reads
  dispatchEvent of input types (click, change, keypress, ...) as a DOM
  input origin and ends chains there; node emits those names outside of
  input (fs.watch 'change', readline 'keypress', user emitters).
- DependencyExecution: save the depth, begin iff there is a node, end by
  unwinding to the depth. Windows and dispatches use it.
- A window pop also drops the dispatches its unwind ended, so a dispatch
  whose finally a termination skipped does not outlive its window.
- Dispatches without a node keep a nullopt entry, so an end pops exactly
  its own entry.
- EventEmitter#emit: no re-indent of the multi-listener body; the dispatch
  ends in its existing finally.
@Domiii Domiii changed the title Report event dispatches to the dependency graph Report event dispatches to the dependency graph as emitEvent Nov 17, 2026
@Domiii
Domiii merged commit 7f7fff0 into main Nov 17, 2026
3 checks passed
@Domiii
Domiii deleted the dominik/node-dg-events branch November 17, 2026 19:44
Domiii added a commit that referenced this pull request Nov 18, 2026
… [kHybridDispatch]

[kHybridDispatch] is the pre-#242 code again. When instrumented,
pre-execution installs hybridDispatchInDependencyGraph(), which runs the
original in the emitEvent node's Begin/End if it has listeners.
Domiii added a commit that referenced this pull request Nov 18, 2026
…and [kHybridDispatch]

EventEmitter#emit and EventTarget[kHybridDispatch] are the pre-#242 code
again. When instrumented, pre-execution installs wrappers from
internal/events/dependency_graph that run the original in the emitEvent
node's Begin/End iff it calls listeners. emit('error') with kErrorMonitor
listeners now emits kErrorMonitor inside the 'error' dispatch.
Domiii added a commit that referenced this pull request Nov 18, 2026
… in uninstrumented processes (#250)

* Dependency graph: fold the instrumented checks of uninstrumented processes

The checks read a let of internal/async_hooks, which optimized code loads
and converts to a boolean at every check: in EventEmitter#emit, twice per
call. A field of a constant object, written only when it changes, is folded
to its value instead. emit() of a single listener moves its try/finally to
a function called only when instrumented.

* EventTarget: end the dispatch in a catch, not a finally

A finally costs on the normal path also when not instrumented. Listener
errors are caught in the loop, so only internal errors reach the catch.

* EventTarget: keep the finally; emit(): explain the duplicated listener call

The catch ending an EventTarget dispatch rethrew with a new message
location. With the gates folded, the finally remains the only cost.

* EventTarget: dispatch outside of the try/finally when not instrumented

The listener loop moves to dispatchListeners(). Instrumented, it runs in
dispatchInDependencyGraph()'s try/finally; else kHybridDispatch calls it
directly.

* EventTarget: keep the dispatch loop in kHybridDispatch

dispatchListeners() was inlined into dispatchEvent(), which slowed
dispatches down.

* EventTarget: install the dependency graph dispatch over an unmodified [kHybridDispatch]

[kHybridDispatch] is the pre-#242 code again. When instrumented,
pre-execution installs hybridDispatchInDependencyGraph(), which runs the
original in the emitEvent node's Begin/End if it has listeners.

* Events: install the dependency graph dispatches over unmodified emit and [kHybridDispatch]

EventEmitter#emit and EventTarget[kHybridDispatch] are the pre-#242 code
again. When instrumented, pre-execution installs wrappers from
internal/events/dependency_graph that run the original in the emitEvent
node's Begin/End iff it calls listeners. emit('error') with kErrorMonitor
listeners now emits kErrorMonitor inside the 'error' dispatch.

* Events: define the dependency graph dispatch rule in the wrapper

A call dispatches iff type is a string or symbol and the emitter or target
has a listener for type at call time. Deciding it runs no user code.

* Events: state the dependency graph dispatch decision's reads exactly
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