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

If cache is enabled, FileLoader should not make duplicate requests. #10644

Closed
4 tasks done
ngokevin opened this issue Jan 25, 2017 · 5 comments
Closed
4 tasks done

If cache is enabled, FileLoader should not make duplicate requests. #10644

ngokevin opened this issue Jan 25, 2017 · 5 comments

Comments

@ngokevin
Copy link
Contributor

ngokevin commented Jan 25, 2017

Description of the problem

Given the code below, only one request should be fired.

THREE.Cache.enabled = true
var fileLoader = new THREE.FileLoader();
fileLoader.load('foo.jpg');  // A
fileLoader.load('foo.jpg');  // B
  1. A checks the cache and sees no pending requests.
  2. A makes the request and stores the pending request in the cache as a Promise. The Promise will resolve the file.
  3. B checks the cache and sees a pending request. B waits for the Promise to resolve the file.
  4. The request finishes, and the Promise resolves.
  5. A and B, both waiting on the Promise, triggers their callbacks at the same time with the same file response.

I'd like to volunteer to work on this. In A-Frame, we often have been having to maintain a cache of promises to prevent duplicate requests. If it sounds reasonable, below are several possible implementations:

  1. Change THREE.Cache to store promises that resolve files, rather than directly storing files. I think this is the optimal solution because requests will always be asynchronous. This is sort of a breaking change, but three.js applications don't often enable the cache, or don't care about the internals of the cache.

  2. Create a separate cache object (RequestCache) for pending requests. The current Cache stays intact, but there is more logic to maintain both the pending and complete caches.

  3. Keep the pending request cache internal to the FileLoader. It'll just be a variable var requestCache = {}.

We'd also need a Promise polyfill. They're less than 1KB gzipped if that sounds fine.

Three.js version
  • Dev
  • r84
Browser
  • All of them
OS
  • All of them
Hardware Requirements (graphics card, VR Device, ...)

N/A

@takahirox
Copy link
Collaborator

Maybe related PR #10149

@takahirox
Copy link
Collaborator

takahirox commented Mar 3, 2017

@mrdoob
I wanna let it go ahead.

Currently we have two ideas to prevent duplicate requests.

Which do you prefer?
Or is there any concerns?

BTW Three.js doesn't use Promise now.
You don't allow to use Promise yet?

@vladgaidukov
Copy link

@takahirox
#10149 conflicts resolved

@takahirox
Copy link
Collaborator

@mrdoob

Would you share your concerns about those approaches with us (again)?

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 17, 2017

I know it's a bit late but i vote for a Promise based solution and @ngokevin's first implementation approach 😊.

ngokevin added a commit to ngokevin/three.js that referenced this issue Oct 18, 2017
ngokevin added a commit to ngokevin/three.js that referenced this issue Oct 18, 2017
@mrdoob mrdoob closed this as completed in 23690f5 Oct 20, 2017
mrdoob added a commit that referenced this issue Oct 20, 2017
have FileLoader merge multiple concurrent requests for the same url (fixes #10644)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants