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.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.block.CustomBlockUtils; 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.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.lang.ref.SoftReference;
import java.util.Set; import java.util.Set;
/** /**
@ -60,7 +60,8 @@ public class DynamicChunk extends Chunk {
private long lastChangeTime; 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, public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ,
@NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) { @NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) {
@ -386,17 +387,22 @@ public class DynamicChunk extends Chunk {
@NotNull @NotNull
@Override @Override
protected ChunkDataPacket createFreshPacket() { protected ChunkDataPacket createFreshPacket() {
return cachedPacket.getUpdatedCache(() -> { ChunkDataPacket packet = cachedPacket.get();
ChunkDataPacket chunkDataPacket = new ChunkDataPacket(getIdentifier(), getLastChangeTime()); if (packet != null && cachedPacketTime == getLastChangeTime()) {
chunkDataPacket.biomes = biomes; return packet;
chunkDataPacket.chunkX = chunkX; }
chunkDataPacket.chunkZ = chunkZ; packet = new ChunkDataPacket(getIdentifier(), getLastChangeTime());
chunkDataPacket.paletteStorage = blockPalette.clone(); packet.biomes = biomes;
chunkDataPacket.customBlockPaletteStorage = customBlockPalette.clone(); packet.chunkX = chunkX;
chunkDataPacket.blockEntities = blockEntities.clone(); packet.chunkZ = chunkZ;
chunkDataPacket.blocksData = blocksData.clone(); packet.paletteStorage = blockPalette.clone();
return chunkDataPacket; packet.customBlockPaletteStorage = customBlockPalette.clone();
}, getLastChangeTime()); packet.blockEntities = blockEntities.clone();
packet.blocksData = blocksData.clone();
this.cachedPacketTime = getLastChangeTime();
this.cachedPacket = new SoftReference<>(packet);
return packet;
} }
@NotNull @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.item.attribute.ItemAttribute;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.Writeable; import net.minestom.server.utils.binary.Writeable;
import net.minestom.server.utils.cache.CachedObject;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -34,8 +33,8 @@ public class ItemMeta implements Writeable {
private final NBTCompound nbt; private final NBTCompound nbt;
private final ItemMetaBuilder emptyBuilder; private final ItemMetaBuilder emptyBuilder;
private CachedObject<String> cachedSNBT = new CachedObject<>(); private String cachedSNBT;
private CachedObject<ByteBuf> cachedBuffer = new CachedObject<>(); private ByteBuf cachedBuffer;
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) { protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
this.damage = metaBuilder.damage; this.damage = metaBuilder.damage;
@ -129,7 +128,10 @@ public class ItemMeta implements Writeable {
} }
public @NotNull String toSNBT() { public @NotNull String toSNBT() {
return cachedSNBT.getCache(nbt::toSNBT); if (cachedSNBT == null) {
this.cachedSNBT = nbt.toSNBT();
}
return cachedSNBT;
} }
@Override @Override
@ -153,12 +155,12 @@ public class ItemMeta implements Writeable {
@Override @Override
public synchronized void write(@NotNull BinaryWriter writer) { public synchronized void write(@NotNull BinaryWriter writer) {
final var buffer = cachedBuffer.getCache(() -> { if (cachedBuffer == null) {
BinaryWriter w = new BinaryWriter(); BinaryWriter w = new BinaryWriter();
w.writeNBT("", nbt); w.writeNBT("", nbt);
return w.getBuffer(); this.cachedBuffer = w.getBuffer();
}); }
writer.write(buffer); writer.write(cachedBuffer);
buffer.resetReaderIndex(); 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();
}
}