Skip to content

Commit

Permalink
fix: Optimize const-pool, cache wide indices headset when calling 'in…
Browse files Browse the repository at this point in the history
…ternalToCp'
  • Loading branch information
Col-E committed Mar 5, 2022
1 parent 8efcd90 commit f1af659
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/me/coley/cafedude/classfile/ConstPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +21,7 @@
public class ConstPool implements List<ConstPoolEntry> {
private final List<ConstPoolEntry> backing = new ArrayList<>();
private final SortedSet<Integer> wideIndices = new TreeSet<>();
private final Map<Integer, Integer> indexToWides = new HashMap<>();

/**
* Insert an entry after the given index in the pool.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -146,11 +149,11 @@ private void onAdd(ConstPoolEntry constPoolEntry, int location) {
if (!larger.isEmpty()) {
List<Integer> 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);
}

/**
Expand All @@ -172,10 +175,15 @@ private void onRemove(ConstPoolEntry constPoolEntry, int location) {
if (!larger.isEmpty()) {
List<Integer> 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())
Expand Down

0 comments on commit f1af659

Please sign in to comment.