2013-06-21 07:47:12 +00:00
From c4ca5d2c5867b36a6e203fbf55f741a644ba87c0 Mon Sep 17 00:00:00 2001
2013-05-08 02:10:34 +00:00
From: snowleo <schneeleo@gmail.com>
Date: Wed, 8 May 2013 12:09:45 +1000
2013-06-21 07:47:12 +00:00
Subject: [PATCH] Cache Translation Storage
2013-05-08 02:10:34 +00:00
This patch reduces the memory footprint of each EntityPlayer by about 300 KB. The original class looks very unfinished and future versions might use the commented code.
diff --git a/src/main/java/net/minecraft/server/LocaleLanguage.java b/src/main/java/net/minecraft/server/LocaleLanguage.java
2013-05-08 08:57:48 +00:00
index d88f864..2a52fe3 100644
2013-05-08 02:10:34 +00:00
--- a/src/main/java/net/minecraft/server/LocaleLanguage.java
+++ b/src/main/java/net/minecraft/server/LocaleLanguage.java
@@ -1,5 +1,10 @@
package net.minecraft.server;
+// Spigot start
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+// Spigot end
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
2013-05-08 08:57:48 +00:00
@@ -12,15 +17,23 @@ import java.util.TreeMap;
2013-05-08 02:10:34 +00:00
public class LocaleLanguage {
+ // Spigot - cache languages to prevent reloading on each player creation
+ private static Cache<String,Properties> languages = CacheBuilder.newBuilder().weakValues().build(
+ new CacheLoader<String, Properties>() {
+ public Properties load(String key) {
+ return loadLanguage(key);
+ }
+ });
private static LocaleLanguage a = new LocaleLanguage("en_US");
- private Properties b = new Properties();
- private TreeMap c;
- private TreeMap d = new TreeMap();
+ private volatile Properties b = new Properties(); // Spigot - volatile
+ private static TreeMap c; // Spigot - static
+ // private TreeMap d = new TreeMap(); // Spigot - Unused map
private String e;
- private boolean f;
+ // private boolean f; // Spigot - removed
2013-05-08 08:57:48 +00:00
+ static { e(); } // Spigot - initializer
2013-05-08 02:10:34 +00:00
public LocaleLanguage(String s) {
- this.e();
2013-05-08 08:57:48 +00:00
+ // this.e(); // Spigot: moved up
2013-05-08 02:10:34 +00:00
this.a(s, false);
}
2013-05-08 08:57:48 +00:00
@@ -28,7 +41,7 @@ public class LocaleLanguage {
2013-05-08 02:10:34 +00:00
return a;
}
- private void e() {
+ private static void e() { // Spigot - static
TreeMap treemap = new TreeMap();
try {
2013-05-08 08:57:48 +00:00
@@ -46,23 +59,25 @@ public class LocaleLanguage {
2013-05-08 02:10:34 +00:00
return;
}
- this.c = treemap;
- this.c.put("en_US", "English (US)");
+ c = treemap; // Spigot - this => static
+ c.put("en_US", "English (US)"); // Spigot - this => static
}
public TreeMap b() {
return this.c;
}
- private void a(Properties properties, String s) throws IOException {
+ private static void a(Properties properties, String s) throws IOException { // Spigot - static
BufferedReader bufferedreader = null;
+ /* Spigot - unused map
if (this.d.containsKey(s)) {
bufferedreader = new BufferedReader(new FileReader((File) this.d.get(s)));
} else {
+ */
bufferedreader = new BufferedReader(new InputStreamReader(LocaleLanguage.class.getResourceAsStream("/lang/" + s + ".lang"), "UTF-8"));
- }
-
+ //} // Spigot: unused map
+ try { // Spigot: close reader
for (String s1 = bufferedreader.readLine(); s1 != null; s1 = bufferedreader.readLine()) {
s1 = s1.trim();
if (!s1.startsWith("#")) {
2013-05-08 08:57:48 +00:00
@@ -73,22 +88,32 @@ public class LocaleLanguage {
2013-05-08 02:10:34 +00:00
}
}
}
+ } finally { bufferedreader.close(); } // Spigot - close reader
}
public synchronized void a(String s, boolean flag) {
if (flag || !s.equals(this.e)) {
+ // Spigot start - Move loading code to new static method
+ this.e = s;
+ this.b = languages.getUnchecked(s);
+ }
+ }
+
+ private static Properties loadLanguage(String s) {
+ // Spigot end
Properties properties = new Properties();
try {
- this.a(properties, "en_US");
+ a(properties, "en_US"); // Spigot - this => static
} catch (IOException ioexception) {
;
}
- this.f = false;
+ // this.f = false; // Spigot - removed variable
if (!"en_US".equals(s)) {
try {
- this.a(properties, s);
+ a(properties, s); // Spigot - this => static
+ /* Spigot - f is unused, so unneeded code
Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements() && !this.f) {
2013-05-08 08:57:48 +00:00
@@ -106,22 +131,25 @@ public class LocaleLanguage {
2013-05-08 02:10:34 +00:00
}
}
}
+ */
} catch (IOException ioexception1) {
ioexception1.printStackTrace();
- return;
+ //return; // Spigot - moved down
}
}
-
+ return properties; // Spigot - return properties
+ /* Spigot - moved up
this.e = s;
this.b = properties;
}
+ */
}
- public synchronized String a(String s) {
+ public String a(String s) { // Spigot - removed synchronized, b is volatile
return this.b.getProperty(s, s);
}
- public synchronized String a(String s, Object... aobject) {
+ public String a(String s, Object... aobject) { // Spigot - removed synchronized, b is volatile
String s1 = this.b.getProperty(s, s);
try {
2013-05-08 08:57:48 +00:00
@@ -131,11 +159,11 @@ public class LocaleLanguage {
2013-05-08 02:10:34 +00:00
}
}
- public synchronized boolean b(String s) {
+ public boolean b(String s) { // Spigot - removed synchronized, b is volatile
return this.b.containsKey(s);
}
- public synchronized String c(String s) {
2013-05-08 08:57:48 +00:00
+ public String c(String s) { // Spigot - removed synchronized, b is volatile
2013-05-08 02:10:34 +00:00
return this.b.getProperty(s + ".name", "");
}
}
--
2013-06-02 05:15:15 +00:00
1.8.1.2
2013-05-08 02:10:34 +00:00