From dfeb981361deeeaca3d79d72790f74712837b8f5 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Wed, 14 Jan 2015 21:48:24 -0500 Subject: [PATCH] Some memory optimizations. - Load all OsuFile String objects into a hash table while parsing, to eliminate duplicate strings. - Trim all ArrayList objects in OsuFiles. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/OsuParser.java | 48 +++++++++++++++++++----- src/itdelatrisu/opsu/states/Options.java | 2 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/itdelatrisu/opsu/OsuParser.java b/src/itdelatrisu/opsu/OsuParser.java index 5fe14e0e..9b1f576d 100644 --- a/src/itdelatrisu/opsu/OsuParser.java +++ b/src/itdelatrisu/opsu/OsuParser.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedList; import org.newdawn.slick.Color; @@ -51,6 +52,11 @@ public class OsuParser { */ private static int totalDirectories = -1; + /** + * The string database. + */ + private static HashMap stringdb = new HashMap(); + // This class should not be instantiated. private OsuParser() {} @@ -92,11 +98,15 @@ public class OsuParser { parseFile(file, osuFiles, false); } if (!osuFiles.isEmpty()) { // add entry if non-empty + osuFiles.trimToSize(); Collections.sort(osuFiles); OsuGroupList.get().addSongGroup(osuFiles); } } + // clear string DB + stringdb = new HashMap(); + currentFile = null; currentDirectoryIndex = -1; totalDirectories = -1; @@ -153,7 +163,7 @@ public class OsuParser { osu.countdown = Byte.parseByte(tokens[1]); break; case "SampleSet": - osu.sampleSet = tokens[1]; + osu.sampleSet = getDBString(tokens[1]); break; case "StackLeniency": osu.stackLeniency = Float.parseFloat(tokens[1]); @@ -234,28 +244,28 @@ public class OsuParser { try { switch (tokens[0]) { case "Title": - osu.title = tokens[1]; + osu.title = getDBString(tokens[1]); break; case "TitleUnicode": - osu.titleUnicode = tokens[1]; + osu.titleUnicode = getDBString(tokens[1]); break; case "Artist": - osu.artist = tokens[1]; + osu.artist = getDBString(tokens[1]); break; case "ArtistUnicode": - osu.artistUnicode = tokens[1]; + osu.artistUnicode = getDBString(tokens[1]); break; case "Creator": - osu.creator = tokens[1]; + osu.creator = getDBString(tokens[1]); break; case "Version": - osu.version = tokens[1]; + osu.version = getDBString(tokens[1]); break; case "Source": - osu.source = tokens[1]; + osu.source = getDBString(tokens[1]); break; case "Tags": - osu.tags = tokens[1].toLowerCase(); + osu.tags = getDBString(tokens[1].toLowerCase()); break; case "BeatmapID": osu.beatmapID = Integer.parseInt(tokens[1]); @@ -321,7 +331,7 @@ public class OsuParser { tokens[2] = tokens[2].replaceAll("^\"|\"$", ""); String ext = OsuParser.getExtension(tokens[2]); if (ext.equals("jpg") || ext.equals("png")) - osu.bg = file.getParent() + File.separator + tokens[2]; + osu.bg = getDBString(file.getParent() + File.separator + tokens[2]); break; case "2": // break periods try { @@ -339,6 +349,8 @@ public class OsuParser { break; } } + if (osu.breaks != null) + osu.breaks.trimToSize(); break; case "[TimingPoints]": while ((line = in.readLine()) != null) { @@ -369,6 +381,7 @@ public class OsuParser { line, file.getAbsolutePath()), e); } } + osu.timingPoints.trimToSize(); break; case "[Colours]": LinkedList colors = new LinkedList(); @@ -586,4 +599,19 @@ public class OsuParser { return currentDirectoryIndex * 100 / totalDirectories; } + + /** + * Returns the String object in the database for the given String. + * If none, insert the String into the database and return the original String. + * @param s the string to retrieve + * @return the string object + */ + private static String getDBString(String s) { + String DBString = stringdb.get(s); + if (DBString == null) { + stringdb.put(s, s); + return s; + } else + return DBString; + } } \ No newline at end of file diff --git a/src/itdelatrisu/opsu/states/Options.java b/src/itdelatrisu/opsu/states/Options.java index 7180f1e7..35448370 100644 --- a/src/itdelatrisu/opsu/states/Options.java +++ b/src/itdelatrisu/opsu/states/Options.java @@ -698,7 +698,7 @@ public class Options extends BasicGameState { int subtextWidth = Utils.FONT_DEFAULT.getWidth("Click or drag an option to change it."); float tabX = (width / 50) + (tab.getWidth() / 2f); float tabY = 15 + Utils.FONT_XLARGE.getLineHeight() + (tab.getHeight() / 2f); - float tabOffset = (float) Math.min(tab.getWidth(), + int tabOffset = Math.min(tab.getWidth(), ((width - subtextWidth - tab.getWidth()) / 2) / TAB_MAX); for (int i = 0; i < optionTabs.length; i++) optionTabs[i] = new MenuButton(tab, tabX + (i * tabOffset), tabY);