Skip to content

Commit

Permalink
Merge branch '1.20.1' into 1.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 31, 2025
2 parents a5480ba + f551cf6 commit e5e1957
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setWindow(int regionX, int regionZ, int regionSize) {
&& !windowMoveFuture.isDone()
&& (regionX != 0 || regionZ != 0 || regionSize != 0) // queue window change if we are clearing it (setting size to 0)
) {
XaeroPlus.LOGGER.info("Rejecting window move to: [{} {} {}] from: [{} {} {}]", regionX, regionZ, regionSize, windowRegionX, windowRegionZ, windowRegionSize);
XaeroPlus.LOGGER.debug("Rejecting window move to: [{} {} {}] from: [{} {} {}]", regionX, regionZ, regionSize, windowRegionX, windowRegionZ, windowRegionSize);
return;
}
int prevWindowRegionX = windowRegionX;
Expand All @@ -71,17 +71,17 @@ public void setWindow(int regionX, int regionZ, int regionSize) {
private void moveWindow0(final int windowRegionX, final int windowRegionZ, final int windowRegionSize, final int prevWindowRegionX, final int prevWindowRegionZ, final int prevWindowRegionSize) {
// load new data
Long2LongMap dataBuf = new Long2LongOpenHashMap();
database.getHighlightsInWindowAndOutsidePrevWindow(
dimension,
windowRegionX - windowRegionSize, windowRegionX + windowRegionSize,
windowRegionZ - windowRegionSize, windowRegionZ + windowRegionSize,
prevWindowRegionX - prevWindowRegionSize, prevWindowRegionX + prevWindowRegionSize,
prevWindowRegionZ - prevWindowRegionSize, prevWindowRegionZ + prevWindowRegionSize,
(chunkX, chunkZ, foundTime) -> dataBuf.put(chunkPosToLong(chunkX, chunkZ), foundTime)
);
try {
// minimizes time we have to hold the lock by querying the database outside the lock's scope
// at cost of a bit more memory
database.getHighlightsInWindowAndOutsidePrevWindow(
dimension,
windowRegionX - windowRegionSize, windowRegionX + windowRegionSize,
windowRegionZ - windowRegionSize, windowRegionZ + windowRegionSize,
prevWindowRegionX - prevWindowRegionSize, prevWindowRegionX + prevWindowRegionSize,
prevWindowRegionZ - prevWindowRegionSize, prevWindowRegionZ + prevWindowRegionSize,
(chunkX, chunkZ, foundTime) -> dataBuf.put(chunkPosToLong(chunkX, chunkZ), foundTime)
);
if (!dataBuf.isEmpty() && lock.writeLock().tryLock(1, TimeUnit.SECONDS)) {
for (var entry : Long2LongMaps.fastIterable(dataBuf)) {
chunks.put(entry.getLongKey(), entry.getLongValue());
Expand Down Expand Up @@ -156,18 +156,19 @@ public ListenableFuture<Long2LongMap> getHighlightsInCustomWindow(int windowRegi
int regionZMin = windowRegionZ - windowRegionSize;
int regionXMax = windowRegionX + windowRegionSize;
int regionZMax = windowRegionZ + windowRegionSize;
database.getHighlightsInWindow(
dimension,
regionXMin, regionXMax,
regionZMin, regionZMax,
(chunkX, chunkZ, foundTime) -> map.put(chunkPosToLong(chunkX, chunkZ), foundTime)
);
// append chunks from local cache

try {
database.getHighlightsInWindow(
dimension,
regionXMin, regionXMax,
regionZMin, regionZMax,
(chunkX, chunkZ, foundTime) -> map.put(chunkPosToLong(chunkX, chunkZ), foundTime)
);
int chunkXMin = regionCoordToChunkCoord(regionXMin);
int chunkXMax = regionCoordToChunkCoord(regionXMax);
int chunkZMin = regionCoordToChunkCoord(regionZMin);
int chunkZMax = regionCoordToChunkCoord(regionZMax);
// append chunks from local cache
if (lock.readLock().tryLock(1, TimeUnit.SECONDS)) {
for (var entry : Long2LongMaps.fastIterable(chunks)) {
final long chunkPos = entry.getLongKey();
Expand Down
44 changes: 23 additions & 21 deletions common/src/main/java/xaeroplus/mixin/client/MixinMapWorld.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package xaeroplus.mixin.client;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import xaero.map.MapProcessor;
import xaero.map.world.MapDimension;
import xaero.map.world.MapWorld;
import xaeroplus.util.DelegatingHashTable;

import java.util.Hashtable;

@Mixin(value = MapWorld.class, remap = false)
public class MixinMapWorld {
public abstract class MixinMapWorld {
@Unique MapDimension xaeroPlus$currentDimensionRef = null;

@Shadow
private Hashtable<ResourceKey<Level>, MapDimension> dimensions;
@Shadow
private ResourceKey<Level> currentDimensionId;
@Shadow public abstract MapDimension getDimension(final ResourceKey<Level> dimId);
@Shadow private ResourceKey<Level> currentDimensionId;

@Inject(method = "<init>", at = @At("RETURN"))
public void init(String mainId, String oldUnfixedMainId, MapProcessor mapProcessor, final CallbackInfo ci) {
// Hashtable is slow af as every operation is synchronized
// replace it with our own implementation
// will still be thread-safe
// except with iterators - however, the base Xaero code does use synchronization blocks.
this.dimensions = new DelegatingHashTable<>();
@WrapOperation(method = "switchToFutureUnsynced",
at = @At(
value = "FIELD",
opcode = Opcodes.PUTFIELD,
target = "Lxaero/map/world/MapWorld;currentDimensionId:Lnet/minecraft/resources/ResourceKey;"),
remap = true) // $REMAP
public void setCurrentDimensionRef(final MapWorld instance, final ResourceKey<Level> value, final Operation<Void> original) {
original.call(instance, value);
this.xaeroPlus$currentDimensionRef = getDimension(value);
}

/**
* @author rfresh2
* @reason fast dimension map lookup without synchronization
* @reason skip hot hashtable lookup with cached reference
*/
@Overwrite
public MapDimension getDimension(ResourceKey<Level> dimId) {
public MapDimension getCurrentDimension() {
ResourceKey<Level> dimId = this.currentDimensionId;
MapDimension ref = xaeroPlus$currentDimensionRef;
if (dimId == null) return null;
else return this.dimensions.get(dimId);
if (ref != null) return ref;
return this.getDimension(dimId);
}
}

0 comments on commit e5e1957

Please sign in to comment.