Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageBitmapLoader: Cache promises to deduplicate requests. #27270

Merged
merged 9 commits into from
Dec 7, 2023

Conversation

Archimagus
Copy link
Contributor

Related issue: #XXXX

Description

Currently I am loading several GLTF files that reference the same texture. Since those calls all come in before the textures have downloaded the cache is failing.
This PR adds the promise to the cache so subsequent calls can see that the fetch is in progress and it will wait for them rather than downloading again.

Archimagus and others added 6 commits October 16, 2023 15:59
sampleRenderTarget is being deleted and set to null by super.dispose();
the check for undefined was incorrect causing an attempt to call dispose on a null object.
Fix code style.
@Mugen87
Copy link
Collaborator

Mugen87 commented Nov 29, 2023

Something is wrong with the PR since the diff is the same as #27106.

So that if multiple calls to load the same image come in,
before the first one is loaded it will still only load it once.
Copy link

github-actions bot commented Nov 29, 2023

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
668.7 kB (166 kB) 668.8 kB (166 kB) +122 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
449.8 kB (108.9 kB) 449.8 kB (108.9 kB) +0 B

@Archimagus
Copy link
Contributor Author

Something is wrong with the PR since the diff is the same as #27106.

Thanks, something got out of sync. Fixed now.

@sxxov
Copy link

sxxov commented Dec 4, 2023

I'm also looking into this for my project!

Just a question, is there a benefit to doing it this way—introducing promises to Cache—instead of using a similar strategy to FileLoader, where it dedupes the request using a global loading variable instead?:

const loading = {};
if ( loading[ url ] !== undefined ) {

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 4, 2023

@sxxov That is a good question. The solution of this PR tries to achieve the same (monitoring pending requests) like what is already implemented in FileLoader with the loading object/dictionary.

I tend to prefer the promise based solution though. The approach in FileLoader comes from a time before fetch() was introduced (#12434). Maybe we can use the same approach from this PR in FileLoader as well?

@Mugen87 Mugen87 added this to the r160 milestone Dec 4, 2023
@sxxov
Copy link

sxxov commented Dec 4, 2023

@Mugen87

@sxxov That is a good question. The solution of this PR tries to achieve the same (monitoring pending requests) like what is already implemented in FileLoader with the loading object/dictionary.

I tend to prefer the promise based solution though. The approach in FileLoader comes from a time before fetch() was introduced (#12434). Maybe we can use the same approach from this PR in FileLoader as well?

Why not migrate this loader to use FileLoader internally, then migrate FileLoader? Whilst this means having adjacent cached keys for caching createImageBitmap, it's something that might be useful when considered, given #27301.

There seems to some workarounds in FileLoader that is absent here too!

@Mugen87
Copy link
Collaborator

Mugen87 commented Dec 4, 2023

Now that FileLoader uses fetch it sounds right to investigate this option.

However, it does not prevent us from merging this PR and apply a refactoring in FileLoader to cache promises first. The usage of FileLoader in ImageBitmapLoader can be introduced at a later point as well.

@Mugen87 Mugen87 changed the title Added additional caching to ImageBitmapLoader ImageBitmapLoader: Cache promises to deduplicate requests. Dec 5, 2023
@Mugen87 Mugen87 merged commit a5a728d into mrdoob:dev Dec 7, 2023
12 checks passed
AdaRoseCannon pushed a commit to AdaRoseCannon/three.js that referenced this pull request Jan 15, 2024
)

* Fix for dispose error
sampleRenderTarget is being deleted and set to null by super.dispose();
the check for undefined was incorrect causing an attempt to call dispose on a null object.

* Removed disposal of sampleRenderTarget entierly

* Fix for setting path not affecting GLTF Sub Assets correctly.

* Fixed Comment

* Update GLTFLoader.js

Fix code style.

* Added additional caching to ImageBitmapLoader
So that if multiple calls to load the same image come in,
before the first one is loaded it will still only load it once.

* Lint

---------

Co-authored-by: Michael Herzog <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants