Skip to content

Commit 16deb73

Browse files
Improve InstructionOffsetValue performance (#73)
1 parent 183e831 commit 16deb73

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

base/src/main/java/proguard/evaluation/value/InstructionOffsetValue.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
package proguard.evaluation.value;
1919

20+
import java.util.HashSet;
21+
import java.util.Set;
22+
2023
import proguard.classfile.TypeConstants;
2124

2225
/**
@@ -41,15 +44,16 @@ public class InstructionOffsetValue extends Category1Value
4144
public static final int EXCEPTION_HANDLER = 0x20000000;
4245

4346

44-
private int[] values;
47+
private final int[] values;
48+
private Set<Integer> valuesMap;
4549

4650

4751
/**
4852
* Creates a new InstructionOffsetValue with the given instruction offset.
4953
*/
5054
public InstructionOffsetValue(int value)
5155
{
52-
this.values = new int[] { value };
56+
this.values = new int[]{value};
5357
}
5458

5559

@@ -60,6 +64,18 @@ public InstructionOffsetValue(int value)
6064
public InstructionOffsetValue(int[] values)
6165
{
6266
this.values = values;
67+
// If there are a lot of values, it's faster to use a set
68+
// for the "contains" method, than doing a linear search
69+
// The threshold for this is chosen by experimentation
70+
// to find a balance between memory footprint and performance
71+
if (values.length > 100)
72+
{
73+
valuesMap = new HashSet<>();
74+
for (int index = 0; index < values.length; index++)
75+
{
76+
valuesMap.add(values[index]);
77+
}
78+
}
6379
}
6480

6581

@@ -87,15 +103,18 @@ public int instructionOffset(int index)
87103
*/
88104
public boolean contains(int value)
89105
{
90-
for (int index = 0; index < values.length; index++)
106+
if (valuesMap == null)
91107
{
92-
if (values[index] == value)
108+
for (int index = 0; index < values.length; index++)
93109
{
94-
return true;
110+
if (values[index] == value)
111+
{
112+
return true;
113+
}
95114
}
115+
return false;
96116
}
97-
98-
return false;
117+
return valuesMap.contains(value);
99118
}
100119

101120

0 commit comments

Comments
 (0)