Reduce plugin lag

Discussion in 'Plugin Development' started by JoshArgent, Mar 10, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    JoshArgent

    Hi,

    I have a plugin on my server that I made myself. It essentialy finds a random, fresh chunk on the server and teleports a new player to that place. The system works very well and is becoming very popular.

    My server has been getting lag spikes where no one can place blocks, chat ect for about 30 seconds. After investigating the server log I can tell it happens whenever someone teleports to a random location.

    How can I reduce the lag caused by it? Right now whenever a player enters the command it just executes the random home method. The method checks that the random location is safe (no water, lava, trees ect) so usually 4 or 5 different random locations are calculated.

    Is it a good idea to do it in a seperate thread, although I heard you can't access Bukkit API's when doing this.

    I believe my code is as efficient as possible.

    What's the best route to go down?

    Thanks in advance,
    Josh.
     
  2. The lag is likely being caused by the generation of new chunks as well as the calculations if the chunk is safe.
    If it's a "Fresh" chunk it'll need to be generated but while it's being generated you're trying to check if the position is safe to use.

    Perhaps load the chunk first, then get a random position in the chunk to place the player?
     
  3. Offline

    JoshArgent

    How can I reduce the amount of lag caused by fresh chunks being generated?
     
  4. When running a professional server I think it's best to generate the world (With any limitations you want to apply) as soon as possible. This'll be a massive file (A few gigabytes) but will mean that the world won't need to generate the chunks as players move about. Teleporting a player to an unrendered chunk will cause not only that chunk but also many chunks around it to be generated, do this a few times and the server will lag out if the hardware isn't up to spec.
     
  5. Offline

    JoshArgent

    Unfortunetly that's not possible on my server because of the way it works. I have special arrangements with my host so that they allow me to have a very large world file (in excess of 20gb). I reguarly delete regions older than a month.

    Would it be quicker to manually generate each chunk and then teleport the player as opposed to just teleporting and then letting it automatically generate the chunks.
     
  6. Offline

    CubieX

    You can use the "Nolagg" - Addon "examine" which is built in to have a detailed look at the time consumption of your plugins and other server proccesses. You can see which plugins event caused what time consumption and you can also see if the chunk generation provider is running and how much time it consumes each tick.

    Generating chunks uses ALOT of CPU time compared to other "normal" plugin actions.
    So pre-generating the World to a desired radius will definately lower the impact your "random teleport plugin" will have in server performance.

    I don't think that your environmental checks will use up much cpu time, unless you iterate through tenth of thousands of blocks for it each time.

    But especcially teleporting to a new, ungenerated chunk will cause a heavy load as he server has to generate a minimum of about 314 chunks (10^2 * PI) around that player beeing teleported in one sweep.
    (assuming your view-distance is set to 10 in server.properties)

    Perharps you could generate those chunks one after another while showing a countdown to the player who is about to be teleported. But I have no deeper expierience in how to do this in a way, that will use less time each tick for generation that the automatic generation routine would.
     
Thread Status:
Not open for further replies.

Share This Page