From 9446c272f19f01e6eb1a0da9dc4e1248b4b3e5f0 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Mon, 22 Jan 2018 02:25:05 +0000 Subject: [PATCH] Use interned strings when decoding objects/items/npcs Converts decoded string fields (e.g. name, description, action) to interned strings during decoding to cut down memory usage. --- .../apollo/cache/decoder/ItemDefinitionDecoder.java | 11 +++++++---- .../apollo/cache/decoder/NpcDefinitionDecoder.java | 8 +++++--- .../apollo/cache/decoder/ObjectDefinitionDecoder.java | 8 +++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cache/src/main/java/org/apollo/cache/decoder/ItemDefinitionDecoder.java b/cache/src/main/java/org/apollo/cache/decoder/ItemDefinitionDecoder.java index 5ba66621..6b839cc1 100644 --- a/cache/src/main/java/org/apollo/cache/decoder/ItemDefinitionDecoder.java +++ b/cache/src/main/java/org/apollo/cache/decoder/ItemDefinitionDecoder.java @@ -73,9 +73,11 @@ public final class ItemDefinitionDecoder implements Runnable { } else if (opcode == 1) { buffer.getShort(); } else if (opcode == 2) { - definition.setName(BufferUtil.readString(buffer)); + String name = BufferUtil.readString(buffer); + definition.setName(name.intern()); } else if (opcode == 3) { - definition.setDescription(BufferUtil.readString(buffer)); + String description = BufferUtil.readString(buffer); + definition.setDescription(description.intern()); } else if (opcode >= 4 && opcode <= 8 || opcode == 10) { buffer.getShort(); } else if (opcode == 11) { @@ -99,9 +101,10 @@ public final class ItemDefinitionDecoder implements Runnable { if (str.equalsIgnoreCase("hidden")) { str = null; } - definition.setGroundAction(opcode - 30, str); + definition.setGroundAction(opcode - 30, str.intern()); } else if (opcode >= 35 && opcode < 40) { - definition.setInventoryAction(opcode - 35, BufferUtil.readString(buffer)); + String action = BufferUtil.readString(buffer); + definition.setInventoryAction(opcode - 35, action.intern()); } else if (opcode == 40) { int colourCount = buffer.get() & 0xFF; for (int i = 0; i < colourCount; i++) { diff --git a/cache/src/main/java/org/apollo/cache/decoder/NpcDefinitionDecoder.java b/cache/src/main/java/org/apollo/cache/decoder/NpcDefinitionDecoder.java index aa5dfd20..a67f8118 100644 --- a/cache/src/main/java/org/apollo/cache/decoder/NpcDefinitionDecoder.java +++ b/cache/src/main/java/org/apollo/cache/decoder/NpcDefinitionDecoder.java @@ -80,9 +80,11 @@ public final class NpcDefinitionDecoder implements Runnable { models[index] = buffer.getShort(); } } else if (opcode == 2) { - definition.setName(BufferUtil.readString(buffer)); + String name = BufferUtil.readString(buffer); + definition.setName(name.intern()); } else if (opcode == 3) { - definition.setDescription(BufferUtil.readString(buffer)); + String description = BufferUtil.readString(buffer); + definition.setDescription(description.intern()); } else if (opcode == 12) { definition.setSize(buffer.get()); } else if (opcode == 13) { @@ -98,7 +100,7 @@ public final class NpcDefinitionDecoder implements Runnable { action = null; } - definition.setInteraction(opcode - 30, action); + definition.setInteraction(opcode - 30, action.intern()); } else if (opcode == 40) { int length = buffer.get() & 0xFF; int[] originalColours = new int[length]; diff --git a/cache/src/main/java/org/apollo/cache/decoder/ObjectDefinitionDecoder.java b/cache/src/main/java/org/apollo/cache/decoder/ObjectDefinitionDecoder.java index 3ffc797a..2086535d 100644 --- a/cache/src/main/java/org/apollo/cache/decoder/ObjectDefinitionDecoder.java +++ b/cache/src/main/java/org/apollo/cache/decoder/ObjectDefinitionDecoder.java @@ -78,9 +78,11 @@ public final class ObjectDefinitionDecoder implements Runnable { data.get(); } } else if (opcode == 2) { - definition.setName(BufferUtil.readString(data)); + String name = BufferUtil.readString(data); + definition.setName(name.intern()); } else if (opcode == 3) { - definition.setDescription(BufferUtil.readString(data)); + String description = BufferUtil.readString(data); + definition.setDescription(description.intern()); } else if (opcode == 5) { int amount = data.get() & 0xFF; for (int i = 0; i < amount; i++) { @@ -106,7 +108,7 @@ public final class ObjectDefinitionDecoder implements Runnable { actions = new String[10]; } String action = BufferUtil.readString(data); - actions[opcode - 30] = action; + actions[opcode - 30] = action.intern(); definition.setMenuActions(actions); } else if (opcode == 39) { data.get();