generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a graph cache in tile grapher to prevent repeated computation
this works because the growing tile grapher essentially stretches out the coordinate space of a normal graph, so the same coordinate is evaluated for multiple in-world coordinates. by preventing repeated computations, speed is increased by a major degree.
- Loading branch information
Showing
6 changed files
with
95 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/net/pixaurora/janerator/graphing/grapher/CachingGrapher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.pixaurora.janerator.graphing.grapher; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
import net.minecraft.world.level.ChunkPos; | ||
import net.pixaurora.janerator.graphing.GraphedChunk; | ||
|
||
public abstract class CachingGrapher implements ChunkGrapher { | ||
private LoadingCache<ChunkPos, GraphedChunk> cache; | ||
|
||
public CachingGrapher() { | ||
this.cache = CacheBuilder.newBuilder() | ||
.expireAfterAccess(60, TimeUnit.SECONDS) | ||
.maximumSize(1024) | ||
.build(CacheLoader.from(chunk -> new GraphedChunk(this, chunk))); | ||
} | ||
|
||
@Override | ||
public GraphedChunk getChunkGraph(ChunkPos chunk) { | ||
try { | ||
return this.cache.get(chunk); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
47 changes: 8 additions & 39 deletions
47
src/main/java/net/pixaurora/janerator/graphing/grapher/ChunkGrapher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,26 @@ | ||
package net.pixaurora.janerator.graphing.grapher; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.mojang.serialization.Codec; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.ChunkPos; | ||
import net.pixaurora.janerator.graphing.Coordinate; | ||
import net.pixaurora.janerator.graphing.GraphedChunk; | ||
import net.pixaurora.janerator.graphing.GraphingUtils; | ||
|
||
public abstract class ChunkGrapher { | ||
public interface ChunkGrapher { | ||
public static Codec<ChunkGrapher> CODEC = GrapherType.CODEC.dispatch("type", ChunkGrapher::type, GrapherType::getAppliedCodec); | ||
|
||
private LoadingCache<ChunkPos, GraphedChunk> cache; | ||
|
||
public ChunkGrapher() { | ||
this.cache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(60, TimeUnit.SECONDS) | ||
.maximumSize(1024) | ||
.build(CacheLoader.from(this::graphChunk)); | ||
} | ||
public GrapherType type(); | ||
|
||
public abstract boolean isPointShaded(int x, int z); | ||
public GraphedChunk getChunkGraph(ChunkPos pos); | ||
|
||
public abstract GrapherType type(); | ||
|
||
public boolean isPointShaded(Coordinate coord) { | ||
return this.isPointShaded(coord.x(), coord.z()); | ||
} | ||
|
||
public boolean isPointShaded(BlockPos pos) { | ||
return this.isPointShaded(pos.getX(), pos.getZ()); | ||
} | ||
|
||
private GraphedChunk graphChunk(ChunkPos pos) { | ||
return new GraphedChunk(this, pos); | ||
} | ||
public boolean isPointShaded(Coordinate pos); | ||
|
||
public GraphedChunk getChunkGraph(ChunkPos pos) { | ||
try { | ||
return this.cache.get(pos); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
public default boolean isPointShaded(int x, int z) { | ||
return this.isPointShaded(new Coordinate(x, z)); | ||
} | ||
|
||
public CompletableFuture<GraphedChunk> scheduleChunkGraphing(ChunkPos pos) { | ||
return CompletableFuture.supplyAsync(() -> this.getChunkGraph(pos), GraphingUtils.threadPool); | ||
public default boolean isPointShaded(BlockPos pos) { | ||
return this.isPointShaded(new Coordinate(pos)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters