Revert "Added CachedObject to access a single-object cache"

This reverts commit 1c39e06d
This commit is contained in:
TheMode 2021-05-13 08:32:26 +02:00
parent 1c39e06d55
commit 6a712b33a0
3 changed files with 30 additions and 91 deletions

View File

@ -16,7 +16,6 @@ import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.block.CustomBlockUtils;
import net.minestom.server.utils.cache.CachedObject;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
@ -27,6 +26,7 @@ import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.SoftReference;
import java.util.Set;
/**
@ -60,7 +60,8 @@ public class DynamicChunk extends Chunk {
private long lastChangeTime;
private CachedObject<ChunkDataPacket> cachedPacket = new CachedObject<>();
private SoftReference<ChunkDataPacket> cachedPacket = new SoftReference<>(null);
private long cachedPacketTime;
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ,
@NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) {
@ -386,17 +387,22 @@ public class DynamicChunk extends Chunk {
@NotNull
@Override
protected ChunkDataPacket createFreshPacket() {
return cachedPacket.getUpdatedCache(() -> {
ChunkDataPacket chunkDataPacket = new ChunkDataPacket(getIdentifier(), getLastChangeTime());
chunkDataPacket.biomes = biomes;
chunkDataPacket.chunkX = chunkX;
chunkDataPacket.chunkZ = chunkZ;
chunkDataPacket.paletteStorage = blockPalette.clone();
chunkDataPacket.customBlockPaletteStorage = customBlockPalette.clone();
chunkDataPacket.blockEntities = blockEntities.clone();
chunkDataPacket.blocksData = blocksData.clone();
return chunkDataPacket;
}, getLastChangeTime());
ChunkDataPacket packet = cachedPacket.get();
if (packet != null && cachedPacketTime == getLastChangeTime()) {
return packet;
}
packet = new ChunkDataPacket(getIdentifier(), getLastChangeTime());
packet.biomes = biomes;
packet.chunkX = chunkX;
packet.chunkZ = chunkZ;
packet.paletteStorage = blockPalette.clone();
packet.customBlockPaletteStorage = customBlockPalette.clone();
packet.blockEntities = blockEntities.clone();
packet.blocksData = blocksData.clone();
this.cachedPacketTime = getLastChangeTime();
this.cachedPacket = new SoftReference<>(packet);
return packet;
}
@NotNull

View File

@ -6,7 +6,6 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.item.attribute.ItemAttribute;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.Writeable;
import net.minestom.server.utils.cache.CachedObject;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -34,8 +33,8 @@ public class ItemMeta implements Writeable {
private final NBTCompound nbt;
private final ItemMetaBuilder emptyBuilder;
private CachedObject<String> cachedSNBT = new CachedObject<>();
private CachedObject<ByteBuf> cachedBuffer = new CachedObject<>();
private String cachedSNBT;
private ByteBuf cachedBuffer;
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
this.damage = metaBuilder.damage;
@ -129,7 +128,10 @@ public class ItemMeta implements Writeable {
}
public @NotNull String toSNBT() {
return cachedSNBT.getCache(nbt::toSNBT);
if (cachedSNBT == null) {
this.cachedSNBT = nbt.toSNBT();
}
return cachedSNBT;
}
@Override
@ -153,12 +155,12 @@ public class ItemMeta implements Writeable {
@Override
public synchronized void write(@NotNull BinaryWriter writer) {
final var buffer = cachedBuffer.getCache(() -> {
if (cachedBuffer == null) {
BinaryWriter w = new BinaryWriter();
w.writeNBT("", nbt);
return w.getBuffer();
});
writer.write(buffer);
buffer.resetReaderIndex();
this.cachedBuffer = w.getBuffer();
}
writer.write(cachedBuffer);
this.cachedBuffer.resetReaderIndex();
}
}

View File

@ -1,69 +0,0 @@
package net.minestom.server.utils.cache;
import com.google.common.annotations.Beta;
import org.jetbrains.annotations.NotNull;
import java.lang.ref.SoftReference;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
/**
* Represents a single that can be collected by the garbage collector
* depending on memory demand.
*
* @param <T> the object to cache
*/
@Beta
public class CachedObject<T> extends SoftReference<AtomicReference<T>> {
private long cacheTime;
public CachedObject() {
super(new AtomicReference<>());
}
/**
* Retrieves the cache.
*
* @param supplier supplier for the value if absent
* @return the cache
*/
public @NotNull T getCache(@NotNull Supplier<@NotNull T> supplier) {
var referent = get();
assert referent != null;
var value = referent.get();
if (value == null) {
value = supplier.get();
referent.set(value);
}
return value;
}
/**
* Retrieves the cache, and ensure that the value is up-to-date.
*
* @param supplier supplier for the value if absent or outdated
* @param lastUpdate the required internal timestamp, supplier will be called otherwise
* @return the cache
*/
public @NotNull T getUpdatedCache(@NotNull Supplier<@NotNull T> supplier, long lastUpdate) {
var referent = get();
assert referent != null;
var value = referent.get();
if (value == null || cacheTime != lastUpdate) {
value = supplier.get();
this.cacheTime = lastUpdate;
referent.set(value);
}
return value;
}
/**
* @deprecated use {@link #getCache(Supplier)}
*/
@Override
@Deprecated
public AtomicReference<T> get() {
return super.get();
}
}