fix: Add global model cache to prevent duplicate loading and browser lag (#93)#678
Open
1234-ad wants to merge 1 commit intotscircuit:mainfrom
Open
fix: Add global model cache to prevent duplicate loading and browser lag (#93)#6781234-ad wants to merge 1 commit intotscircuit:mainfrom
1234-ad wants to merge 1 commit intotscircuit:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
The Fix: Lines 58, 64, and 69 have trailing spaces that need to be removed. Running Here's the diff: --- a/src/utils/load-model.ts
+++ b/src/utils/load-model.ts
@@ -55,18 +55,18 @@ async function loadModelFromUrl(url: string): Promise<THREE.Object3D | null> {
export async function load3DModel(url: string): Promise<THREE.Object3D | null> {
// Clean the URL (remove cache busting parameters)
const cleanUrl = url.replace(/&cachebust_origin=$/, "")
-
+
const cache = window.TSCIRCUIT_3D_MODEL_CACHE
// Check if model is already in cache
if (cache.has(cleanUrl)) {
const cacheItem = cache.get(cleanUrl)!
-
+
// If we have a cached result, clone it to avoid sharing the same object
if (cacheItem.result) {
return cacheItem.result.clone()
}
-
+
// If we're still loading, wait for the existing promise and clone the result
return cacheItem.promise.then((result) => {
return result ? result.clone() : nullI've also pushed the fix to |
3 tasks
|
Closing in favor of #685 which includes the same changes with the format-check CI fix applied. |
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.
Description
This PR fixes the laggy browser issue caused by duplicate model loading by implementing a global model cache for all 3D model types (OBJ, STL, GLTF, GLB, WRL).
Problem
The current implementation in
load3DModel()creates new loader instances and loads models from scratch every time they're requested, even if the same model URL has been loaded before. This causes:Example project showing the issue: https://tscircuit.com/editor?snippet_id=22c651e7-a6d2-436d-b2b6-ba0b0921e95e
Solution
Implemented a global model cache (
TSCIRCUIT_3D_MODEL_CACHE) that:Changes
Modified Files
src/utils/load-model.ts- Added global caching mechanismKey Implementation Details
Cache Flow:
Benefits
✅ Performance: Models are loaded once and reused
✅ Memory efficient: Single copy of geometry data per unique model
✅ Network efficient: No duplicate HTTP requests
✅ Browser responsiveness: Eliminates lag from redundant loading
✅ Backward compatible: No API changes, drop-in replacement
Testing
Tested with:
Notes
useGlobalObjLoaderhook but applies to the lower-levelload3DModelutilityCloses
Closes #93
Bounty
This PR addresses the $5 bounty issue.