perf: stop re-subscribing the replay render loop every frame#906
Open
Anexus5919 wants to merge 1 commit into
Open
perf: stop re-subscribing the replay render loop every frame#906Anexus5919 wants to merge 1 commit into
Anexus5919 wants to merge 1 commit into
Conversation
|
@Anexus5919 is attempting to deploy a commit to the somiljain2024-4175's projects Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Author
|
@Somil450 @diksha78dev Kindly have a review on this pr. Thanks! |
This was referenced Jun 22, 2026
The render-loop useEffect listed currentFrameIdx in its dependency array, and the loop advances currentFrameIdx as it plays. So on every frame step the effect tore down (cancelAnimationFrame) and re-subscribed (requestAnimationFrame), recreating the renderLoop closure each time. Read the frame index from a ref (kept current by a small effect) inside the loop and drop currentFrameIdx from the dependency array, so the loop is set up once. Behaviour is unchanged; the loop already advanced playback via frameFloatRef, and the remaining deps are memoized.
e82c6b1 to
5523032
Compare
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.
📌 Related Issue
Fixes #869
📝 Description
The
requestAnimationFramerender-loopuseEffectinReplay3DModel.tsxhadcurrentFrameIdxin its dependency array, and the loop advancescurrentFrameIdx(setCurrentFrameIdx) on every playback step. So on every frame the effect tore down (cancelAnimationFrame) and re-subscribed (requestAnimationFrame), rebuilding therenderLoopclosure. The other dependencies are memoized/stable, socurrentFrameIdxwas the only one changing during playback.🔹 What has been changed?
src/components/Replay3DModel.tsx: the loop now reads the frame index from a ref (currentFrameIdxRef, kept current by a small dedicated effect) instead of the closure-capturedcurrentFrameIdx, andcurrentFrameIdxis removed from the render-loop effect's dependency array. The loop is now set up once (re-subscribing only whenframes/isPlaying/modelLoaded/etc. actually change).🔹 Why are these changes needed?
frameFloatRef, socurrentFrameIdxdid not need to be a reactive dependency; reading it from an always-current ref preserves behaviour (including the paused/scrub render) while setting the loop up once.🛠️ Type of Change
🧪 Testing
✅ Tests Performed
npx tsc --noEmit:Replay3DModel.tsxis clean (the only pre-existing tsc errors are inexerciseEngine.ts, unrelated).npx eslint src/components/Replay3DModel.tsx: 0 problems — importantly, noreact-hooks/exhaustive-depswarning, confirming the effect no longer referencescurrentFrameIdx(all uses go through the ref).frameFloatRef(unchanged); when paused/scrubbing it renderscurrentFrameIdxRef.current, which the small sync effect keeps current — so the displayed frame is the same, without the per-frame re-subscribe.🔹 Note on automated tests
No runtime unit test is included: the loop is a WebGL
requestAnimationFrameloop inside a large Three.js component, which is not feasible to unit test without a full WebGL + DOM harness. Verification is bytsc,eslint(exhaustive-deps), and a behaviour review.🌐 Browsers Tested
Not applicable (render-loop lifecycle change).
📷 Screenshots / Demo (if applicable)
Not applicable.
📋 Checklist
💬 Additional Notes
Branched from latest
upstream/main(5464425). Small, self-contained change (6 insertions, 4 deletions).