Thanks @CrazyDev!

This commit is contained in:
RePixelatedMC
2023-11-24 20:44:56 +01:00
parent 6edd8bf119
commit 47990cde46
2 changed files with 50 additions and 55 deletions
@@ -293,15 +293,15 @@ public interface EngineMantle extends IObjectPlacer {
} }
default long getToUnload(){ default long getToUnload(){
return Mantle.fakeToUnload.get(); return getMantle().getFakeToUnload().get();
} }
default long getDynamicThreads(){ default long getDynamicThreads(){
return Mantle.dynamicThreads.get(); return getMantle().getDynamicThreads().get();
} }
default double getTectonicLimit(){ default double getTectonicLimit(){
return getMantle().tectonicLimit.get(); return Mantle.tectonicLimit.get();
} }
default double getTectonicDuration(){ default double getTectonicDuration(){
return getMantle().adjustedIdleDuration.get(); return getMantle().getAdjustedIdleDuration().get();
} }
} }
@@ -64,7 +64,7 @@ public class Mantle {
private final int worldHeight; private final int worldHeight;
private final Map<Long, Long> lastUse; private final Map<Long, Long> lastUse;
@Getter @Getter
public static Map<Long, TectonicPlate> loadedRegions; private final Map<Long, TectonicPlate> loadedRegions;
private final HyperLock hyperLock; private final HyperLock hyperLock;
private final KSet<Long> unload; private final KSet<Long> unload;
private final AtomicBoolean closed; private final AtomicBoolean closed;
@@ -390,23 +390,24 @@ public class Mantle {
return numberOfEntries * bytesPerEntry; return numberOfEntries * bytesPerEntry;
} }
@Getter
private final AtomicInteger fakeToUnload = new AtomicInteger(0);
private final AtomicInteger oldFakeToUnload = new AtomicInteger((0));
@Getter
private final AtomicDouble adjustedIdleDuration = new AtomicDouble(0);
public static final AtomicInteger tectonicLimit = new AtomicInteger(30);
@Getter
private final AtomicInteger dynamicThreads = new AtomicInteger(1);
@Getter
private final AtomicInteger forceAggressiveThreshold = new AtomicInteger(30);
private int g = 0;
/** /**
* Save & unload regions that have not been used for more than the * Save & unload regions that have not been used for more than the
* specified amount of milliseconds * specified amount of milliseconds
* *
* @param baseIdleDuration the duration * @param baseIdleDuration the duration
*/ */
@Getter
public static AtomicInteger fakeToUnload = new AtomicInteger(0);
public static AtomicInteger oldFakeToUnload = new AtomicInteger((0));
public static AtomicDouble adjustedIdleDuration = new AtomicDouble(0);
public static AtomicInteger tectonicLimit = new AtomicInteger(30);
public static AtomicInteger dynamicThreads = new AtomicInteger(1);
public static AtomicInteger forceAggressiveThreshold = new AtomicInteger(30);
public int g = 0;
public synchronized void trim(long baseIdleDuration) { public synchronized void trim(long baseIdleDuration) {
if (closed.get()) { if (closed.get()) {
throw new RuntimeException("The Mantle is closed"); throw new RuntimeException("The Mantle is closed");
@@ -433,7 +434,7 @@ public class Mantle {
dynamicThreads.addAndGet(-1); dynamicThreads.addAndGet(-1);
} }
} }
oldFakeToUnload = fakeToUnload; oldFakeToUnload.set(fakeToUnload.get());
} }
if (!IrisSettings.get().getPerformance().dynamicPerformanceMode){ if (!IrisSettings.get().getPerformance().dynamicPerformanceMode){
if(IrisSettings.get().getPerformance().getTectonicUnloadThreads() <= -1){ if(IrisSettings.get().getPerformance().getTectonicUnloadThreads() <= -1){
@@ -459,16 +460,14 @@ public class Mantle {
io.set(true); io.set(true);
try { try {
Set<Long> toUnload; final Set<Long> toUnload = new HashSet<>();
Iris.debug("Trimming Tectonic Plates older than " + Form.duration(adjustedIdleDuration.get(), 0)); Iris.debug("Trimming Tectonic Plates older than " + Form.duration(adjustedIdleDuration.get(), 0));
toUnload = new HashSet<>();
for (Long i : lastUse.keySet()) { for (Long i : lastUse.keySet()) {
double finalAdjustedIdleDuration = adjustedIdleDuration.get(); double finalAdjustedIdleDuration = adjustedIdleDuration.get();
Set<Long> finalToUnload1 = toUnload;
hyperLock.withLong(i, () -> { hyperLock.withLong(i, () -> {
if (M.ms() - lastUse.get(i) >= finalAdjustedIdleDuration) { if (M.ms() - lastUse.get(i) >= finalAdjustedIdleDuration) {
finalToUnload1.add(i); toUnload.add(i);
fakeToUnload.addAndGet(1); fakeToUnload.addAndGet(1);
Iris.debug("Tectonic Region added to unload"); Iris.debug("Tectonic Region added to unload");
} }
@@ -478,55 +477,51 @@ public class Mantle {
if (IrisSettings.get().getPerformance().AggressiveTectonicUnload && loadedRegions.size() > tectonicLimit.get() && tectonicLimit.get() > forceAggressiveThreshold.get()) { if (IrisSettings.get().getPerformance().AggressiveTectonicUnload && loadedRegions.size() > tectonicLimit.get() && tectonicLimit.get() > forceAggressiveThreshold.get()) {
AtomicInteger dummyLoadedRegions = new AtomicInteger(loadedRegions.size()); AtomicInteger dummyLoadedRegions = new AtomicInteger(loadedRegions.size());
for (; dummyLoadedRegions.get() > tectonicLimit.get();) { while (dummyLoadedRegions.get() > tectonicLimit.get()) {
try { try {
long fiveSecondsAgo = M.ms() - 5000; long fiveSecondsAgo = M.ms() - 5000;
toUnload = new HashSet<>(); toUnload.clear();
Long oldestOverFiveSeconds = lastUse.entrySet().stream() lastUse.entrySet().stream()
.filter(e -> e.getValue() < fiveSecondsAgo) .filter(e -> e.getValue() < fiveSecondsAgo)
.max(Comparator.comparingLong(Map.Entry::getValue)) .max(Comparator.comparingLong(Map.Entry::getValue))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
.orElse(null); .ifPresent(oldestOverFiveSeconds -> hyperLock.withLong(oldestOverFiveSeconds, () -> {
if (M.ms() - lastUse.get(oldestOverFiveSeconds) >= adjustedIdleDuration.get()) {
toUnload.add(oldestOverFiveSeconds);
fakeToUnload.getAndAdd(1);
Iris.debug("Oldest Tectonic Region over 5 seconds idle added to unload");
dummyLoadedRegions.getAndDecrement();
}
}));
if (oldestOverFiveSeconds != null) {
Set<Long> finalToUnload = toUnload;
hyperLock.withLong(oldestOverFiveSeconds, () -> {
if (M.ms() - lastUse.get(oldestOverFiveSeconds) >= adjustedIdleDuration.get()) {
finalToUnload.add(oldestOverFiveSeconds);
fakeToUnload.getAndAdd(1);
Iris.debug("Oldest Tectonic Region over 5 seconds idle added to unload");
dummyLoadedRegions.getAndDecrement();
}
});
}
} catch (Exception e) { } catch (Exception e) {
} }
} }
} }
// BurstExecutor burstExecutor = new BurstExecutor(Executors.newFixedThreadPool(1), toUnload.size()); // BurstExecutor burstExecutor = new BurstExecutor(Executors.newFixedThreadPool(1), toUnload.size());
for (Long i : toUnload) { for (Long i : toUnload) {
// burstExecutor.queue(() -> { // burstExecutor.queue(() -> {
hyperLock.withLong(i, () -> { hyperLock.withLong(i, () -> {
TectonicPlate m = loadedRegions.get(i); TectonicPlate m = loadedRegions.get(i);
if (m != null) { if (m != null) {
try { try {
m.write(fileForRegion(dataFolder, i)); m.write(fileForRegion(dataFolder, i));
loadedRegions.remove(i); loadedRegions.remove(i);
lastUse.remove(i); lastUse.remove(i);
fakeToUnload.getAndAdd(-1); fakeToUnload.getAndAdd(-1);
Iris.debug("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(i) + " " + Cache.keyZ(i)); Iris.debug("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(i) + " " + Cache.keyZ(i));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}
} }
}); }
// }); });
// });
} }
// burstExecutor.complete(); // burstExecutor.complete();
} finally { } finally {
io.set(false); io.set(false);