diff --git a/src/main/java/me/coley/cafedude/classfile/ConstPool.java b/src/main/java/me/coley/cafedude/classfile/ConstPool.java index 60cac1d..5b5c2ba 100644 --- a/src/main/java/me/coley/cafedude/classfile/ConstPool.java +++ b/src/main/java/me/coley/cafedude/classfile/ConstPool.java @@ -5,9 +5,11 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; @@ -19,6 +21,7 @@ public class ConstPool implements List { private final List backing = new ArrayList<>(); private final SortedSet wideIndices = new TreeSet<>(); + private final Map indexToWides = new HashMap<>(); /** * Insert an entry after the given index in the pool. @@ -96,7 +99,7 @@ private int internalToCp(int index) { // 3: Double --> 5 // 4: String --> 7 -- // 5: String --> 8 - int wideCount = wideIndices.headSet(index + 1).size(); + int wideCount = indexToWides.computeIfAbsent(index, i -> wideIndices.headSet(i + 1).size()); return 1 + index + wideCount; } @@ -146,11 +149,11 @@ private void onAdd(ConstPoolEntry constPoolEntry, int location) { if (!larger.isEmpty()) { List tmp = new ArrayList<>(larger); larger.clear(); - tmp.forEach(i -> wideIndices.add(i + entrySize)); + tmp.forEach(i -> addWideIndex(i + entrySize)); } // Add wide if (constPoolEntry.isWide()) - wideIndices.add(location); + addWideIndex(location); } /** @@ -172,10 +175,15 @@ private void onRemove(ConstPoolEntry constPoolEntry, int location) { if (!larger.isEmpty()) { List tmp = new ArrayList<>(larger); larger.clear(); - tmp.forEach(i -> wideIndices.add(i - entrySize)); + tmp.forEach(i -> addWideIndex(i - entrySize)); } } + private void addWideIndex(int i) { + wideIndices.add(i); + indexToWides.clear(); + } + @Override public int size() { if (backing.isEmpty())