refactor(loop): extract per-frame work into tick() so try/catch stays in a thin shell#116
Open
chiefcll wants to merge 1 commit into
Open
refactor(loop): extract per-frame work into tick() so try/catch stays in a thin shell#116chiefcll wants to merge 1 commit into
chiefcll wants to merge 1 commit into
Conversation
… in a thin shell The render-loop keep-alive guard added in #115 wraps the whole frame body in a try/catch inside the rAF callback. Some optimizing compilers — notably Chrome 38's Crankshaft, our language floor — refuse to optimize any function that contains a try/catch, so the entire hot per-frame function was being de-optimized on the embedded targets we care about. Split startLoop into two closures: - tick(currentTime): all per-frame work — context-lost check, frame limiting, updateAnimations, the idle path, and the active drawFrame/flushFrameEvents path. This is where every client callback (the only code that can actually throw) and every hot stage.* call runs. No try/catch, so it stays fully optimizable. - runLoop(currentTime): a near-empty shell that is just `try { tick(t) } catch`. It carries the only try/catch, so it's the only function de-optimized on Crankshaft — and it does almost nothing. Behavior is unchanged. The `scheduled` double-schedule guard is promoted to a closure variable, reset at the top of runLoop before the try and set by tick at each scheduling point, so the catch still reschedules only when tick had not already queued the next frame. Context-lost and frame-limit paths now run inside tick but execute no client code and cannot throw, so the catch never fires for them. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
Splits
WebPlatform.startLoopinto two closures so the try/catch keep-alive guard no longer wraps the hot per-frame body directly:tick(currentTime)— all per-frame work: context-lost check, frame limiting,updateFrameTime/updateAnimations, the idle path, and the activedrawFrame/flushFrameEventspath. This is where every client callback (the only code that can throw) and every hotstage.*call runs. No try/catch → fully optimizable.runLoop(currentTime)— a near-empty shell that is essentially justtry { tick(t) } catch (e) { … }. It carries the only try/catch, so it's the only function de-optimized on older engines — and it does almost nothing.Why
The render-loop keep-alive guard (#115) wraps the whole frame body in a try/catch inside the rAF callback. Some optimizing compilers — notably Chrome 38's Crankshaft, our language floor — refuse to optimize any function containing a try/catch, so the entire hot per-frame function was being de-optimized on exactly the embedded targets we care about.
Extracting the body confines the de-opt to the thin shell. The genuinely hot code (
drawFrame,updateAnimations, idle bookkeeping) lives intickand thestage.*methods, all still optimizable. This is the standard "don't put try/catch in hot functions; extract" guidance applied literally.Context: this is the local twin of the upstream discussion on lightning-js/renderer#819, where the try/catch-on-the-hot-path cost was raised in review.
Reviewer notes — behavior is unchanged
scheduleddouble-schedule guard is preserved: promoted to a closure variable, reset at the top ofrunLoopbefore thetry, and set bytickat each point it queues the next frame. The catch still reschedules only whentickhad not already.setTimeout/rAFscheduling, and the no-op-defaulthandleLoopErrorsemantics are all identical.tick(nominally under thetry), but they execute no client code and cannot throw, so the catch never fires for them — and that per-frame work now runs in the optimizable function, which is where you want it.Verification
tsc --noEmit— cleaneslint— cleanPlatformimplementsstartLoop, so nothing else to mirror.🤖 Generated with Claude Code