Improve 3d model loading to avoid laggy browser#742
Open
victorjzq wants to merge 1 commit intotscircuit:mainfrom
Open
Improve 3d model loading to avoid laggy browser#742victorjzq wants to merge 1 commit intotscircuit:mainfrom
victorjzq wants to merge 1 commit intotscircuit:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Comment on lines
+69
to
+74
| const promise = loadModel(cleanUrl).then((result) => { | ||
| cache.set(cleanUrl, { ...cache.get(cleanUrl)!, result }) | ||
| return result | ||
| }) | ||
| cache.set(cleanUrl, { promise, result: null }) | ||
| return promise |
Contributor
There was a problem hiding this comment.
The first caller receives the original un-cloned THREE.Object3D while subsequent callers receive clones. This defeats the purpose of cloning mentioned in the description ("Three.js objects are stateful, so sharing a single instance across the scene would cause transform conflicts"). The first caller can mutate the cached object, corrupting it for all future callers.
Fix by returning a clone:
const promise = loadModel(cleanUrl).then((result) => {
cache.set(cleanUrl, { ...cache.get(cleanUrl)!, result })
return result?.clone() ?? null
})
Suggested change
| const promise = loadModel(cleanUrl).then((result) => { | |
| cache.set(cleanUrl, { ...cache.get(cleanUrl)!, result }) | |
| return result | |
| }) | |
| cache.set(cleanUrl, { promise, result: null }) | |
| return promise | |
| const promise = loadModel(cleanUrl).then((result) => { | |
| cache.set(cleanUrl, { ...cache.get(cleanUrl)!, result }) | |
| return result?.clone() ?? null | |
| }) | |
| cache.set(cleanUrl, { promise, result: null }) | |
| return promise | |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
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.
/claim #93
The browser was getting laggy because every component render triggered fresh network fetches and full model parses for the same URLs — no deduplication at all. I added a global
window.TSCIRCUIT_3D_MODEL_CACHEthat stores both the in-flight promise and the resolved result, so concurrent requests for the same model share one fetch and subsequent renders just clone the cached object. Using.clone()was the key thing to get right here — Three.js objects are stateful, so sharing a single instance across the scene would cause transform conflicts.Fixes #93