From 43de87be0ddf6864be9ccf8febe8f9c00a46ab0e Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 4 Mar 2023 21:15:50 -0600 Subject: [PATCH] Limit QuadTree concurrently loading LodRenderSections --- .../fullDatafile/FullDataFileHandler.java | 2 +- .../seibel/lod/core/render/LodQuadTree.java | 31 +++++++++++++++++-- .../lod/core/render/LodRenderSection.java | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java index 99a0b553a..e4198c72f 100644 --- a/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java +++ b/core/src/main/java/com/seibel/lod/core/file/fullDatafile/FullDataFileHandler.java @@ -32,7 +32,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider { // Note: Single main thread only for now. May make it multi-thread later, depending on the usage. private static final Logger LOGGER = DhLoggerBuilder.getLogger(); - final ExecutorService fileReaderThread = ThreadUtil.makeThreadPool(4, "FileReaderThread"); + final ExecutorService fileReaderThread = ThreadUtil.makeThreadPool(4, FullDataFileHandler.class.getSimpleName()+"Thread"); final ConcurrentHashMap files = new ConcurrentHashMap<>(); final IDhLevel level; final File saveDir; diff --git a/core/src/main/java/com/seibel/lod/core/render/LodQuadTree.java b/core/src/main/java/com/seibel/lod/core/render/LodQuadTree.java index 6d5e9720f..c121095c9 100644 --- a/core/src/main/java/com/seibel/lod/core/render/LodQuadTree.java +++ b/core/src/main/java/com/seibel/lod/core/render/LodQuadTree.java @@ -14,6 +14,8 @@ import com.seibel.lod.core.util.MathUtil; import com.seibel.lod.core.util.gridList.MovableGridRingList; import org.apache.logging.log4j.Logger; +import java.util.concurrent.CompletableFuture; + /** * This quadTree structure is our core data structure and holds * all rendering data.

@@ -54,7 +56,15 @@ public class LodQuadTree implements AutoCloseable public final int blockRenderDistance; private final ILodRenderSourceProvider renderSourceProvider; - private final IDhClientLevel level; //FIXME: Proper hierarchy to remove this reference! + /** How many {@link LodRenderSection}'s are currently loading */ + private int numberOfRenderSectionsLoading = 0; + /** + * Indicates how many {@link LodRenderSection}'s can load concurrently.
+ * Prevents large number of {@link ILodRenderSourceProvider} tasks from building up when initially loading. + */ + private static final int MAX_NUMBER_OF_LOADING_RENDER_SECTIONS = 2; + + private final IDhClientLevel level; //FIXME: Proper hierarchy to remove this reference! @@ -596,7 +606,24 @@ public class LodQuadTree implements AutoCloseable } else if (section.childCount == 0) { - section.loadRenderSourceAndEnableRendering(); + // limit how many render sections can be loading at a time + if (!section.isRenderingEnabled() && this.numberOfRenderSectionsLoading < MAX_NUMBER_OF_LOADING_RENDER_SECTIONS) + { + section.loadRenderSourceAndEnableRendering(); + + this.numberOfRenderSectionsLoading++; + + CompletableFuture future = section.getRenderSourceLoadingFuture(); + if (future != null) + { + future.whenComplete((renderSource, ex) -> this.numberOfRenderSectionsLoading-- ); + } + else + { + // the future will be null if the section was already loaded + this.numberOfRenderSectionsLoading--; + } + } } diff --git a/core/src/main/java/com/seibel/lod/core/render/LodRenderSection.java b/core/src/main/java/com/seibel/lod/core/render/LodRenderSection.java index 8fd5f9e7d..fb1e3cf46 100644 --- a/core/src/main/java/com/seibel/lod/core/render/LodRenderSection.java +++ b/core/src/main/java/com/seibel/lod/core/render/LodRenderSection.java @@ -148,6 +148,7 @@ public class LodRenderSection public boolean isOutdated() { return this.renderSource != null && !this.renderSource.isValid(); } public ColumnRenderSource getRenderSource() { return this.renderSource; } + public CompletableFuture getRenderSourceLoadingFuture() { return this.loadFuture; } //==============//