Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/16.x/forge' into 18.x/forge
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Nov 1, 2023
2 parents ff5b446 + 6979187 commit 55ee98e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ public boolean isSectionVisible(int x, int y, int z) {
}

public void updateChunks() {
this.sectionCache.cleanup();

var blockingFutures = this.submitRebuildTasks(ChunkUpdateType.IMPORTANT_REBUILD);
blockingFutures.addAll(this.submitRebuildTasks(ChunkUpdateType.IMPORTANT_SORT));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ClonedChunkSection {

private PalettedContainer<RegistryEntry<Biome>> biomeData;

private long lastUsedTimestamp;

ClonedChunkSection(ClonedChunkSectionCache backingCache) {
this.backingCache = backingCache;
this.blockEntities = new Short2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -212,6 +214,14 @@ public ClonedChunkSectionCache getBackingCache() {
return this.backingCache;
}

public long getLastUsedTimestamp() {
return this.lastUsedTimestamp;
}

public void setLastUsedTimestamp(long timestamp) {
this.lastUsedTimestamp = timestamp;
}

/**
* @param x The local x-coordinate
* @param y The local y-coordinate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
package me.jellysquid.mods.sodium.client.world.cloned;

import it.unimi.dsi.fastutil.longs.Long2ReferenceMap;
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ReferenceLinkedOpenHashMap;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.world.World;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

public class ClonedChunkSectionCache {
private static final int MAX_CACHE_SIZE = 512; /* number of entries */
private static final long MAX_CACHE_DURATION = TimeUnit.SECONDS.toNanos(5); /* number of nanoseconds */

private final World world;

private final ConcurrentLinkedQueue<ClonedChunkSection> inactivePool = new ConcurrentLinkedQueue<>();
private final Long2ReferenceMap<ClonedChunkSection> byPosition = new Long2ReferenceOpenHashMap<>();
private final Long2ReferenceLinkedOpenHashMap<ClonedChunkSection> byPosition = new Long2ReferenceLinkedOpenHashMap<>();
private long time; // updated once per frame to be the elapsed time since application start

public ClonedChunkSectionCache(World world) {
this.world = world;
this.time = getMonotonicTimeSource();
}

public synchronized void cleanup() {
this.time = getMonotonicTimeSource();
this.byPosition.values()
.removeIf(entry -> this.time > (entry.getLastUsedTimestamp() + MAX_CACHE_DURATION));
}

public synchronized ClonedChunkSection acquire(int x, int y, int z) {
long key = ChunkSectionPos.asLong(x, y, z);
ClonedChunkSection section = this.byPosition.get(key);

if (section != null) {
this.inactivePool.remove(section);
} else {
if (section == null) {
while (this.byPosition.size() >= MAX_CACHE_SIZE) {
this.byPosition.removeFirst();
}

section = this.createSection(x, y, z);
}

section.acquireReference();

return section;
}

private ClonedChunkSection createSection(int x, int y, int z) {
ClonedChunkSection section;

if (!this.inactivePool.isEmpty()) {
section = this.inactivePool.remove();

this.byPosition.remove(section.getPosition().asLong());
} else {
section = this.allocate();
}
ClonedChunkSection section = this.allocate();

ChunkSectionPos pos = ChunkSectionPos.from(x, y, z);
section.init(this.world, pos);

this.byPosition.put(pos.asLong(), section);
this.byPosition.putAndMoveToLast(pos.asLong(), section);

return section;
}
Expand All @@ -56,16 +57,15 @@ public synchronized void invalidate(int x, int y, int z) {
}

public void release(ClonedChunkSection section) {
if (section.releaseReference()) {
this.tryReclaim(section);
}

}

private ClonedChunkSection allocate() {
return new ClonedChunkSection(this);
}

private void tryReclaim(ClonedChunkSection section) {
this.inactivePool.add(section);
private static long getMonotonicTimeSource() {
// Should be monotonic in JDK 17 on sane platforms...
return System.nanoTime();
}
}

0 comments on commit 55ee98e

Please sign in to comment.