Repository navigation
Report event dispatches to the dependency graph as emitEvent - #242
Merged
Merged
Conversation
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.
…nd#13927) and B1b (backend#13925)
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
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.
🤖
REPLAY_BACKEND_REV→ backend master5d351da35be9: theemitEventkind (backend#13927) and node-recording on the shared fixture (backend#13925).Summary
emit.emitEvent{type}node.emitEvent(data).Kind
emitEvent{type}, added to the backend by backend#13927.dispatchEvent, because the backend readsdispatchEventas DOM input.dependencyGraph.tsknownOriginNodeends chains atdispatchEventof input types:click,change,keypress,input,submit, …fs.watch'change', readline'keypress', user emitters.emitEventis not an origin. Chains continue through its execution parent, e.g.asyncCallback.Sites
EventEmitter.prototype.emit, once a handler existsprocess.EventTargetkHybridDispatch, once listeners existdispatchEvent,NodeEventTarget#emitandMessagePortmessages all call it.begin(type); try { listener calls } finally { end() }.Dependency executions
DependencyExecution, insrc/node_dependency_graph.h:finallyof a dispatch. The dispatch then ends with its window.Event type
""without one."".toStringin replay only.Gates
internal/async_hooks, read once inprepareExecution.DependencyGraphEnabled().Upstream merge surface
emit: the single-listener branch is re-indented into atry. The multi-listener branch ends the dispatch in its existingfinally.kHybridDispatch: the listener loop is re-indented into atry.finallyleaves the dispatch open after an exception.Data
linux-node-20261117-ba57434f2507-5d351da35be9-dev, from BKruntime-node-build-and-testWindows Installer does not broadcast a WM_SETTINGCHANGE nodejs/node#603, the top of this stack.EventEmitter#emitand 1e5EventTarget#dispatchEvent, one listener each. Recorded. Medians of 3 runs, ms:RECORD_REPLAY_DISABLE_FEATURES=["dependency-graph"]RECORD_REPLAY_DONT_RECORD=1), main nodejs#596, median of 5emitand about 70 ns more perdispatchEvent.dependency-graph/await-fetchintest-node-recording, ms: