Passed down tile for carve to avoid lock hits when grabbing the tile … #414
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When doing the carve step of the cave layer, the tile used to be grabbed from the dimension
final int terrainHeight = dimension.getIntHeightAt(x, y);
This is problematic because within getIntHeightAt, we would get the tile by calling getTile
public int getIntHeightAt(int x, int y, int defaultHeight) { Tile tile = getTile(x >> TILE_SIZE_BITS, y >> TILE_SIZE_BITS); if (tile != null) { return tile.getIntHeight(x & TILE_SIZE_MASK, y & TILE_SIZE_MASK); } else { return defaultHeight; } }
getTile will return the tile properly but it first has to get a lock before doing so.
public Tile getTile(final int x, final int y) { readLock.lock(); try { return tiles.get(new Point(x, y)); } finally { readLock.unlock(); } }
Because getting a lock and releasing it is relativly intensive when being done frequently, a large amount CPU time was spent just getting and releasing the lock.
After the changes which passes the tile down a few steps, the carve step of cave was reduced in cpu time by 57%.