17
17
*/
18
18
package proguard .evaluation .value ;
19
19
20
+ import java .util .HashSet ;
21
+ import java .util .Set ;
22
+
20
23
import proguard .classfile .TypeConstants ;
21
24
22
25
/**
@@ -41,15 +44,16 @@ public class InstructionOffsetValue extends Category1Value
41
44
public static final int EXCEPTION_HANDLER = 0x20000000 ;
42
45
43
46
44
- private int [] values ;
47
+ private final int [] values ;
48
+ private Set <Integer > valuesMap ;
45
49
46
50
47
51
/**
48
52
* Creates a new InstructionOffsetValue with the given instruction offset.
49
53
*/
50
54
public InstructionOffsetValue (int value )
51
55
{
52
- this .values = new int [] { value };
56
+ this .values = new int []{ value };
53
57
}
54
58
55
59
@@ -60,6 +64,18 @@ public InstructionOffsetValue(int value)
60
64
public InstructionOffsetValue (int [] values )
61
65
{
62
66
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
+ }
63
79
}
64
80
65
81
@@ -87,15 +103,18 @@ public int instructionOffset(int index)
87
103
*/
88
104
public boolean contains (int value )
89
105
{
90
- for ( int index = 0 ; index < values . length ; index ++ )
106
+ if ( valuesMap == null )
91
107
{
92
- if ( values [ index ] == value )
108
+ for ( int index = 0 ; index < values . length ; index ++ )
93
109
{
94
- return true ;
110
+ if (values [index ] == value )
111
+ {
112
+ return true ;
113
+ }
95
114
}
115
+ return false ;
96
116
}
97
-
98
- return false ;
117
+ return valuesMap .contains (value );
99
118
}
100
119
101
120
0 commit comments