In my app I use React with vanilla ThreeJS (yes, no react-three-fiber, please don’t advise it). There I have a function that creates EffectComposer:
const bufferSize = new Vector2();
export const createComposer = (renderer: WebGLRenderer) => {
let renderTarget = undefined;
const size = renderer.getDrawingBufferSize(bufferSize);
renderTarget = new WebGLRenderTarget(size.width, size.height, {
samples: 8,
});
return {composer: new EffectComposer(renderer, renderTarget), target: renderTarget};
};
I use the created composer in another component and handle resize there if needed (for that reason I return target here).
Everything works, but what bothers me is if I create composer on every re-render it yields better frame rate than if I memorize it:
const { composer, target } = useMemo(() => createComposer(renderer), []); // Slow!
const { composer } = createComposer(renderer); // Fast. Also no need to handle resize since it is recreated.
When composer is re-created on every re-render I call dispose() for it.
So, my question is why it behaves like this? How can I optimize the memory allocation and performance at the same time?
What does re-render mean? I suppose you don’t have an animation loop and render on demand, right?
It would be strange if the disposal and re-creation would yield a better performance. That should definitely be investigated. Are you able to demonstrate the issue with a live example?
You’re probably best off creating a codesandbox from the repo as a live demonstration of the issue, it would be much easier to access and debug than having to download and install your entire project…
There seems to be an issue with the reproduction test case. The default code runs and outputs the average FPS on the screen. But when I switch to the other setting, meaning:
Could you please suggest if the code I provided works now? We just shouldn’t have commented this part: const composerAndTarget = , but I duplicated it there, so now you can comment the full line.
Strange, I can’t update the codesandbox anymore. It requires a sign in now which wasn’t necessary last time. Did something change in the sandbox configuration?
Not really, I didn’t update the configs. Just one line of code.
However, it seems you need to be logged in and I need to share it with you personally using the your username or email to be able to edit, otherwise it is read-only. Is there any other way I can share it?
Since I don’t want to log in, I have just downloaded your code and tried to debug it locally. I can reproduce the drop in performance but I’m unable to find a reason for this in the three.js code base. There must be a react related root cause. However, I’m not familiar enough with react to further investigate the issue.
If you believe the issue comes from the three.js side, it would be best to share a reproduction test case with plain three.js (meaning without react or any other dependencies).