Pārlūkot izejas kodu

BETA: Reducing memory usage of !NpcStringId and !SystemMessageId static data.

Reviewed by: UnAfraid
Nos 11 gadi atpakaļ
vecāks
revīzija
84bd05f95b

+ 9 - 22
L2J_Server_BETA/java/com/l2jserver/gameserver/network/NpcStringId.java

@@ -21,9 +21,10 @@ package com.l2jserver.gameserver.network;
 import java.io.File;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
-import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -21606,10 +21607,9 @@ public final class NpcStringId
 	public static final NpcStringId IT_TELEPORTS_THE_GUARD_MEMBERS_OF_THE_ELMORE_IMPERIAL_CASTLE_TO_THE_INSIDE_OF_THE_CASTLE;
 	
 	/**
-	 * Array containing all NpcStringId<br>
-	 * Important: Always initialize with a length of the highest NpcStringId + 1!!!
+	 * Map containing all NpcStringId<br>
 	 */
-	private static NpcStringId[] VALUES;
+	private static Map<Integer, NpcStringId> VALUES = new HashMap<>();
 	
 	static
 	{
@@ -25213,9 +25213,8 @@ public final class NpcStringId
 	private static final void buildFastLookupTable()
 	{
 		final Field[] fields = NpcStringId.class.getDeclaredFields();
-		final ArrayList<NpcStringId> nsIds = new ArrayList<>(fields.length);
 		
-		int maxId = 0, mod;
+		int mod;
 		NpcStringId nsId;
 		for (final Field field : fields)
 		{
@@ -25227,8 +25226,8 @@ public final class NpcStringId
 					nsId = (NpcStringId) field.get(null);
 					nsId.setName(field.getName());
 					nsId.setParamCount(parseMessageParameters(field.getName()));
-					maxId = Math.max(maxId, nsId.getId());
-					nsIds.add(nsId);
+					
+					VALUES.put(nsId.getId(), nsId);
 				}
 				catch (final Exception e)
 				{
@@ -25236,13 +25235,6 @@ public final class NpcStringId
 				}
 			}
 		}
-		
-		VALUES = new NpcStringId[maxId + 1];
-		for (int i = nsIds.size(); i-- > 0;)
-		{
-			nsId = nsIds.get(i);
-			VALUES[nsId.getId()] = nsId;
-		}
 	}
 	
 	private static final int parseMessageParameters(final String name)
@@ -25273,12 +25265,7 @@ public final class NpcStringId
 	
 	private static final NpcStringId getNpcStringIdInternal(final int id)
 	{
-		if ((id < 0) || (id >= VALUES.length))
-		{
-			return null;
-		}
-		
-		return VALUES[id];
+		return VALUES.get(id);
 	}
 	
 	public static final NpcStringId getNpcStringId(final String name)
@@ -25295,7 +25282,7 @@ public final class NpcStringId
 	
 	public static final void reloadLocalisations()
 	{
-		for (final NpcStringId nsId : VALUES)
+		for (final NpcStringId nsId : VALUES.values())
 		{
 			if (nsId != null)
 			{

+ 8 - 22
L2J_Server_BETA/java/com/l2jserver/gameserver/network/SystemMessageId.java

@@ -21,9 +21,10 @@ package com.l2jserver.gameserver.network;
 import java.io.File;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
-import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -15026,10 +15027,9 @@ public final class SystemMessageId
 	public static final SystemMessageId THOMAS_D_TURKEY_DISAPPEARED;
 	
 	/**
-	 * Array containing all SystemMessageIds<br>
-	 * Important: Always initialize with a length of the highest SystemMessageId + 1!!!
+	 * Map containing all SystemMessageIds<br>
 	 */
-	private static SystemMessageId[] VALUES;
+	private static Map<Integer, SystemMessageId> VALUES = new HashMap<>();
 	
 	static
 	{
@@ -17525,9 +17525,8 @@ public final class SystemMessageId
 	private static final void buildFastLookupTable()
 	{
 		final Field[] fields = SystemMessageId.class.getDeclaredFields();
-		final ArrayList<SystemMessageId> smIds = new ArrayList<>(fields.length);
 		
-		int maxId = 0, mod;
+		int mod;
 		SystemMessageId smId;
 		for (final Field field : fields)
 		{
@@ -17539,8 +17538,7 @@ public final class SystemMessageId
 					smId = (SystemMessageId) field.get(null);
 					smId.setName(field.getName());
 					smId.setParamCount(parseMessageParameters(field.getName()));
-					maxId = Math.max(maxId, smId.getId());
-					smIds.add(smId);
+					VALUES.put(smId.getId(), smId);
 				}
 				catch (final Exception e)
 				{
@@ -17548,13 +17546,6 @@ public final class SystemMessageId
 				}
 			}
 		}
-		
-		VALUES = new SystemMessageId[maxId + 1];
-		for (int i = smIds.size(); i-- > 0;)
-		{
-			smId = smIds.get(i);
-			VALUES[smId.getId()] = smId;
-		}
 	}
 	
 	private static final int parseMessageParameters(final String name)
@@ -17585,12 +17576,7 @@ public final class SystemMessageId
 	
 	private static final SystemMessageId getSystemMessageIdInternal(final int id)
 	{
-		if ((id < 0) || (id >= VALUES.length))
-		{
-			return null;
-		}
-		
-		return VALUES[id];
+		return VALUES.get(id);
 	}
 	
 	public static final SystemMessageId getSystemMessageId(final String name)
@@ -17607,7 +17593,7 @@ public final class SystemMessageId
 	
 	public static final void reloadLocalisations()
 	{
-		for (final SystemMessageId smId : VALUES)
+		for (final SystemMessageId smId : VALUES.values())
 		{
 			if (smId != null)
 			{