Skip to content

Conversation

@dberlin
Copy link

@dberlin dberlin commented Dec 21, 2025

The current implementation of block controllers has each block controller search for drawers that are in range (default range = 50). It does so by BFS searching blocks until it hits the range or unloaded blocks.

The maximal number of blocks it will search at default range is ~523,000 (volume of a sphere of radius 50). For each loaded chunks, it's about 25,000 max.

As the number of players, drawer controllers, and loaded chunks increases, this can get quite expensive to be processing.

This patch instead moves to simply keep track of the locations of loaded drawers (catually INetworked entities, to match what the search looked at before) using a spatial grid-index. When INetworked entities load or are created they are entered into the grid, and when they are removed or unloaded they are removed from the grid. This is done by using an event bus and loaded/unloaded events that are watched.

Block controllers then search for drawers in range by querying the grid index instead of doing their own block by block searches. To maintain compatibility with the current search, we use spherical shaped range queries.

With the grid index, even with 100 million drawers/etc, clustered into groups of 0-512 , you easily do 100 queries per second in less than a millisecond. The amount of memory used for the index is neglible even in ridiculous cases.

I also have an octree based version of this patch, which is often conceptually easier to follow but slower in practice and requires more code (Here we just reuse sectionpos which already does most of what we want).

In the end, this patch basically eliminates ~all time being used for drawer searches by controllers.

@dberlin
Copy link
Author

dberlin commented Dec 23, 2025

Oh, i read the docs vs the code wrong. The docs describe it as a sphere (50 blocks in any direction), the code actually did it as a box where the coordinate can be max +-50 in x, y, z (which actually a box, and the diagonals will be longer than 50 blocks). This actually makes the current code search more blocks than i mentioned. I'm happy to modify it to do boxes instead of sphere, easy enough, assuming you decide you want the patch overall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant