This commit is contained in:
RePixelatedMC
2023-12-07 10:41:17 +01:00
parent b5c1e7e2da
commit 6494cded62
@@ -471,20 +471,21 @@ public class Mantle {
try { try {
Iris.debug("Trimming Tectonic Plates older than " + Form.duration(adjustedIdleDuration.get(), 0)); Iris.debug("Trimming Tectonic Plates older than " + Form.duration(adjustedIdleDuration.get(), 0));
if (lastUse != null) { if (lastUse != null) {
for (Long i : lastUse.keySet()) { synchronized (lastUse) { // Synchronize access to lastUse ( GPT Code )
Long lastUseTime = lastUse.get(i); for (Long i : new ArrayList<>(lastUse.keySet())) { // Use ArrayList to avoid ConcurrentModificationException
if (lastUseTime != null) { double finalAdjustedIdleDuration = adjustedIdleDuration.get();
double finalAdjustedIdleDuration = adjustedIdleDuration.get(); hyperLock.withLong(i, () -> {
hyperLock.withLong(i, () -> { Long lastUseTime = lastUse.get(i); // Move the get inside the lock
if (M.ms() - lastUseTime >= finalAdjustedIdleDuration) { if (lastUseTime != null && M.ms() - lastUseTime >= finalAdjustedIdleDuration) {
toUnload.add(i); toUnload.add(i);
Iris.debug("Tectonic Region added to unload"); Iris.debug("Tectonic Region added to unload");
} }
}); });
} }
} }
} }
if (IrisSettings.get().getPerformance().AggressiveTectonicUnload if (IrisSettings.get().getPerformance().AggressiveTectonicUnload
&& loadedRegions.size() > forceAggressiveThreshold.get()) { && loadedRegions.size() > forceAggressiveThreshold.get()) {
@@ -529,27 +530,40 @@ public class Mantle {
protected long loop() { protected long loop() {
try { try {
Iris.info(C.DARK_BLUE + "TECTONIC UNLOAD HAS RUN"); Iris.info(C.DARK_BLUE + "TECTONIC UNLOAD HAS RUN");
BurstExecutor burstExecutor = new BurstExecutor(Executors.newFixedThreadPool(dynamicThreads.get()), toUnload.size()); int threadCount = 1;
for (Long i : toUnload.toArray(Long[]::new)) { ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
burstExecutor.queue(() -> { List<Long> toUnloadList = new ArrayList<>(toUnload);
hyperLock.withLong(i, () -> {
TectonicPlate m = loadedRegions.get(i); int chunkSize = (int) Math.ceil(toUnloadList.size() / (double) threadCount);
if (m != null) {
try { for (int i = 0; i < threadCount; i++) {
m.write(fileForRegion(dataFolder, i)); int start = i * chunkSize;
loadedRegions.remove(i); int end = Math.min(start + chunkSize, toUnloadList.size());
lastUse.remove(i); List<Long> sublist = toUnloadList.subList(start, end);
toUnload.remove(i);
Iris.info("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(i) + " " + Cache.keyZ(i)); executorService.submit(() -> {
} catch (IOException e) { for (Long id : sublist) {
e.printStackTrace(); hyperLock.withLong(id, () -> {
TectonicPlate m = loadedRegions.get(id);
if (m != null) {
try {
m.write(fileForRegion(dataFolder, id));
loadedRegions.remove(id);
lastUse.remove(id);
toUnload.remove(id);
Iris.info("Unloaded Tectonic Plate " + C.DARK_GREEN + Cache.keyX(id) + " " + Cache.keyZ(id));
} catch (IOException e) {
e.printStackTrace();
}
} }
} });
}); }
}); });
} }
burstExecutor.complete();
} catch (Exception e){ executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (Exception e) {
Iris.reportError(e); Iris.reportError(e);
return -1; return -1;
} }
@@ -559,6 +573,7 @@ public class Mantle {
ioTectonicUnload.set(true); ioTectonicUnload.set(true);
} }
/** /**
* This retreives a future of the Tectonic Plate at the given coordinates. * This retreives a future of the Tectonic Plate at the given coordinates.
* All methods accessing tectonic plates should go through this method * All methods accessing tectonic plates should go through this method