Hello,
We have a need to cast a LOT of rays and sweeps (e.g. SweepMultiByChannel). Often, upon discovering the results of one cast or sweep, we must do more casts and sweeps, or other work, which suggests that a round-trip thru Unreal’s built-in async ray casting system isn’t appropriate for this use case. The UE::Tasks::Launch system offers a fantastic way to perform our work on the background worker threads, with inter-task dependencies and everything. Chaos seems to handle ray casts from not-the-main-thread safely. It takes many frames (a handful of seconds, even) for all the rays to be cast, and for the results to be collated. We’re OK with this delay, because it’s running in the background and (mostly, hence the forthcoming question…) not affecting the frame rate, and the game design tolerates this. Since we’re casting so many rays, we’re making a lot of read locks on the scene throughout the frame. Of course, various physics updates have to make write-locks. We don’t want our readers to delay those write locks.
I’m curious about this code here:
```
/** Controls the scene lock type. See above. */
#if WITH_EDITOR
#ifndef CHAOS_SCENE_LOCK_TYPE
#define CHAOS_SCENE_LOCK_TYPE CHAOS_SCENE_LOCK_RWFIFO_CRITICALSECTION
#endif
#else
#ifndef CHAOS_SCENE_LOCK_TYPE
#define CHAOS_SCENE_LOCK_TYPE CHAOS_SCENE_LOCK_FRWLOCK
#endif
#endif
```
In the editor, this implies the use of TRwFifoLock, all is well - a write lock will suffer at most the cost of a very long sweep (a handful of msec, we can probably optimize that with a larger number of shorter sweeps).
In cooked builds, this implies the use of FWindowsRWLock which apparently allows new read lock requests to succeed, even after a write lock has been requested. This is bad news for us, because there will *always* be new write locks being made, especially on computers with many many cores. This appears to cause hitching, because those same write locks take a long time (tens of msec) to be satisfied. Indeed, https://learn.microsoft.com/en\-us/windows/win32/Sync/slim\-reader\-writer\-\-srw\-\-locks includes the text “SRW locks are neither fair nor FIFO”.
Two questions:
1. Why is the lock type different between editor and non-editor builds?
2. If we change the code above to use CHAOS_SCENE_LOCK_RWFIFO_CRITICALSECTION in all configurations, how will we suffer?
3. Got any other advice about casting a lot of rays from worker threads while preserving main thread performance?
Thanks!
-Sergey