Преглед на файлове

Reducing CPU usage as a result of unloading chunks.

Glitchfinder преди 12 години
родител
ревизия
d5d6f7e860
променени са 2 файла, в които са добавени 44 реда и са изтрити 15 реда
  1. 17 0
      src/main/java/com/gmail/nossr50/datatypes/InactiveChunk.java
  2. 27 15
      src/main/java/com/gmail/nossr50/runnables/ChunkletUnloader.java

+ 17 - 0
src/main/java/com/gmail/nossr50/datatypes/InactiveChunk.java

@@ -0,0 +1,17 @@
+package com.gmail.nossr50.datatypes;
+
+import org.bukkit.Chunk;
+
+public class InactiveChunk {
+    public Chunk chunk;
+    public int inactiveTime = 0;
+
+    public InactiveChunk(Chunk chunk, int inactiveTime) {
+        this.chunk = chunk;
+        this.inactiveTime = inactiveTime;
+    }
+
+    public InactiveChunk(Chunk chunk) {
+        this(chunk, 0);
+    }
+}

+ 27 - 15
src/main/java/com/gmail/nossr50/runnables/ChunkletUnloader.java

@@ -8,23 +8,24 @@ import java.util.Map.Entry;
 import org.bukkit.Chunk;
 import org.bukkit.World;
 
+import com.gmail.nossr50.datatypes.InactiveChunk;
 import com.gmail.nossr50.mcMMO;
 
 public class ChunkletUnloader implements Runnable {
-    private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
+    private static Map<String, InactiveChunk> unloadedChunks = new HashMap<String, InactiveChunk>();
     private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
     public static final int RUN_INTERVAL = 20;
 
     public static void addToList(Chunk chunk) {
-        //Unfortunately we can't use Map.contains() because Chunks are always new objects
-        //This method isn't efficient enough for me
-        for (Chunk otherChunk : unloadedChunks.keySet()) {
-            if (chunk.getX() == otherChunk.getX() && chunk.getZ() == otherChunk.getZ()) {
-                return;
-            }
-        }
+        if (chunk == null || chunk.getWorld() == null)
+            return;
+
+        String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
 
-        unloadedChunks.put(chunk, 0);
+        if (unloadedChunks.containsKey(key))
+            return;
+
+        unloadedChunks.put(key, new InactiveChunk(chunk));
     }
 
     public static void addToList(int cx, int cz, World world) {
@@ -33,24 +34,35 @@ public class ChunkletUnloader implements Runnable {
 
     @Override
     public void run() {
-        for (Iterator<Entry<Chunk, Integer>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
-            Entry<Chunk, Integer> entry = unloadedChunkIterator.next();
-            Chunk chunk = entry.getKey();
+        for (Iterator<Entry<String, InactiveChunk>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
+            Entry<String, InactiveChunk> entry = unloadedChunkIterator.next();
+
+            if (entry.getKey() == null || entry.getValue() == null) {
+                unloadedChunkIterator.remove();
+                continue;
+            }
+
+            if (entry.getValue().chunk == null) {
+                unloadedChunkIterator.remove();
+                continue;
+            }
+
+            Chunk chunk = entry.getValue().chunk;
 
             if (!chunk.isLoaded()) {
-                int inactiveTime = entry.getValue() + RUN_INTERVAL;
+                int inactiveTime = entry.getValue().inactiveTime + RUN_INTERVAL;
 
                 //Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
                 if (inactiveTime >= minimumInactiveTime) {
                     if (mcMMO.placeStore == null)
-                        continue;
+                        return;
 
                     mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
                     unloadedChunkIterator.remove();
                     continue;
                 }
 
-                unloadedChunks.put(entry.getKey(), inactiveTime);
+		entry.getValue().inactiveTime = inactiveTime;
             }
             else {
                 //Just remove the entry if the chunk has been reloaded.