perf: reuse Three.js scratch objects in the replay render loop#916
Open
Anexus5919 wants to merge 1 commit into
Open
perf: reuse Three.js scratch objects in the replay render loop#916Anexus5919 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! |
The per-frame render loop allocated a fresh stack of THREE.Vector3 and THREE.Color objects every frame: updateStressVectors cloned several vectors and built two colors per joint, buildSegmentScaleState round- tripped a Matrix4/Vector3/Quaternion to recover a scale it already knew, and the segment adaptor allocated identity vectors as lerp targets. At 60fps over many joints/segments this churned the GC. Hoist the constants and reusable scratch vectors/colors to module scope, write directly into mesh targets, reuse the stored previous-position vectors, and compute the segment scale without the matrix round-trip. Behaviour is unchanged.
d70fd66 to
93f5467
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 #879
📝 Description
Replay3DModel's render loop allocated a fresh batch of Three.js objects every frame:updateStressVectorscloned several vectors and built twoTHREE.Colors per joint.buildSegmentScaleStateround-tripped aMatrix4/Vector3/Quaternionto recover a scale it already knew.updateSegmentScaleAdaptorallocated identity vectors as lerp targets.Across many joints and segments at 60fps this churned the garbage collector (frame hitches).
🔹 What has been changed?
src/components/Replay3DModel.tsx:Vector3/Colorto module scope.updateStressVectors: write directly intomesh.position, use the constant axis/colour objects, and reuse the stored previous-position vectors via.copy().buildSegmentScaleState: set the scale components directly into a scratch vector (dropping the unusedMatrix4round-trip; only.scalewas ever read).🔹 Why are these changes needed?
🛠️ Type of Change
🧪 Testing
✅ Tests Performed
npx tsc --noEmit:Replay3DModel.tsxclean (the only pre-existing tsc errors are inexerciseEngine.ts, unrelated).npx eslint src/components/Replay3DModel.tsx: 0 problems.directionis copied into a separate scratch before being scaled, so it remains valid for the quaternion call.🔹 Note on automated tests
No runtime unit test:
Replay3DModelis a Three.js/WebGL component that does not run under the vitest/jsdom environment. Verification is bytsc,eslint, and the aliasing review above (allocation reduction is observable in the DevTools memory timeline).🌐 Browsers Tested
Not applicable (allocation profile).
📷 Screenshots / Demo (if applicable)
Not applicable.
📋 Checklist
💬 Additional Notes
Branched from latest
upstream/main(5464425). This touchesReplay3DModel.tsx, as do a few other replay PRs (#906, #907, #878); they change different lines and are independently mergeable, with at most a trivial rebase for whichever lands later.