`Terrain:ReadVoxels()` should not mutex lock in parallel luau

Currently, as of 5/23/23, the Terrain:ReadVoxels() API sets a mutex lock when any actor threads call it, so that only one thread at a time can get voxels from Workspace.Terrain. This makes it impossible to read & serialize massive amounts of terrain in parallel through the use of actor instances.

If Roblox were to allow ReadVoxels() to be called at the same time from multiple threads instead of mutex-locking (a write-lock only with no read-lock), it would allow developers to serialize huge amounts of terrain.
For example, let’s say I have an 8000x500x8000 piece of terrain that I want to serialize & manipulate:

Traditionally, we would have to read all of these voxels on a single thread. Assuming chunks of 400x400x400, that’s ~500 chunks of terrain to read! On a single thread that takes ~20 seconds to read (a long time)! However, by utilizing parallel threads (actors), we can split this into 4 threads making it ~125 chunks per thread. This would take less than half the time it takes on a single thread (~7 seconds).

Because of the mutex lock that Roblox sets on terrain, we are unable to utilize multiple threads to read different chunks of terrain at the same time, limiting what we can do with terrain.

15 Likes

There are no mutex locks in Terrain:ReadVoxels, so there is nothing to do for your request.

2 Likes

That’s… odd. When I was attempting to read large amounts of terrain across 4 threads, it was running much slower than on a single thread.

I’ll have to re-check my tests, thanks for the info!

1 Like

If you don’t get the expected speedup, please share a place file so we can look at it, maybe there’s just stuff to improve overall, like the way Luau tables are filled in.
To check this report, I tested my own experimental place and reviewed the source code for locking.

2 Likes

I’ll try to re-run my test later today or some time tomorrow. When I do, I will get back to you with the results as well as the place file.

2 Likes

Just an update, I haven’t forgotten about this. If I can’t get to it this week I’ll do it during the weekend.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.