Questions about regions search performance

I wrote a Regions-protect plugin that stores two vertices(aabb) in the database, cache them in memory when the server starts, and check them at MoveEntityEvent:

public void entityMoveEvent(MoveEntityEvent e){
   pos= player.pos;
   for(Region region : regions){
            if(region.min.getX() <= pos.x && region.max.getX() >= pos.x
               && region.min.getY() <= pos.y && region.max.getY() >=pos. y
               && region.min.getZ() <= pos.z && region.max.getZ() >= pos.z){


which has a big performance cost. So I analyzed the RedProtect code and found that it performs database retrieval directly on the main thread. Is this right?

Im not sure about redprotect. However because you are only comparing int values. You are able to do this sort of asynced. Obviously to cancel the event you need to be on the same tick as the event that was fired.

You may have heard or even used Java 8 steam libs, which is a more modern way to manipulate Collections (so list and sets). Stream is typically synced processing, however there is a second version called parallel stream.

This is a asynced version of stream, so its a lot quicker then regular stream and it forces the thread where the stream was created to wait until all the data is processed, therefore you could compare all the regions on separate threads with the event being cancelled when one is found. So it would be a lot faster.

Here is a good companion of regular to parallel

Thank you. In my tests, I found that reading from the database consumes a thousand times more than cached memory.
I will read the articles you recommend carefully.
If i use the Asynced version , Can the event be cancelled?

You can still cancel the event because the async method it uses is “fork join” whereby the thread that activated the async call, waits until the result is found. So for example, if you were using streams “AnyMatch”, after calling that, it will wait until one thread says there was a match, then it will carry on as normal.

really appreciate!

1 Like