defeatedcrow / HeatAndClimateLib

Mod necessary for defeatedcrow's mod in 1.9.4+
16 stars 6 forks source link

Fix performance issues on block updates. #13

Closed mrapplexz closed 6 years ago

mrapplexz commented 6 years ago

Motivation

When I installed H&C on my server, it began to lag heavily. After profiling the server with WarmRoast, I realized that the lags were caused by climate calculations in the defeatedcrow.hac.core.event.BlockUpdateDC#onUpdate(defeatedcrow.hac.api.recipe.DCBlockUpdateEvent) method.

The Fixes Description

This PR fixes these issues:

  1. It looks like that the clm variable (IClimate clm = ClimateAPI.calculator.getClimate(world, p);) is calculated on each block update, but sometimes not used further. I made it lazy, so the climate calculates only on first access.
  2. Also, the clm2 variable (IClimate clm2 = ClimateAPI.calculator.getClimate(world, p.down());) is calculated for every IGrowable block regardless of whether it really needed. I moved the climate calculation into the right scope, so it will work fine now.
  3. Lastly, the recipe lookup (IClimateSmelting recipe = RecipeAPI.registerSmelting.getRecipe(clm, new ItemStack(block, 1, meta));) uses an instance of IClimate. I refactored some code to make it check the input ItemStack firstly, and, only if it matches, calculate the climate and check it.

    Profiling Results

    Before

    image

    After

    screenshot from 2018-07-29 14-12-45

    Notes

    That pull request needs to be merged too: https://github.com/defeatedcrow/HeatAndClimateMod/pull/37.

defeatedcrow commented 6 years ago

I appreciate your PR. I will incorporate it and if there is no problem it will be reflected in the next update.

mrapplexz commented 6 years ago

Thanks! If you encounter any problems, feel free to contact me.