pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.76k stars 155 forks source link

InstanceMesh Performance Issue #421

Closed jamiemarkwhite closed 1 year ago

jamiemarkwhite commented 1 year ago

Hi, I have been comparing performance between an old custom implementation of cannon + web workers against use-cannon with 1500 instance mesh spheres and the performance was bad with use-cannon and fine with the custom version. After a lot of digging I think I may have found the performance issue...

In physics-provider.tsx (line 181) the function iterates through 'Object.values(refs)' which seem to include multiple entries of the instance mesh. So it appears the function is iterating through as many instance-mesh references as there are instances of the mesh.

I tested my theory by removing duplicate InstanceMesh references from Object.values(refs) and gained a significant performance boost.

So, you might want to take a look at the cause of this duplication in the refs array, if indeed it IS actually a problem and not intentional for the physics to behave correctly.

Appologies, but I haven't been able to look any deeper into this.

Cheers.

https://github.com/pmndrs/use-cannon/blob/85697ec350740ab21d6df22fca17b80c2cd5ecf3/packages/react-three-cannon/src/physics-provider.tsx#L181