31
31
import io .appulse .encon .terms .TermType ;
32
32
import io .appulse .encon .terms .exception .ErlangTermDecodeException ;
33
33
import io .appulse .encon .terms .exception .IllegalErlangTermTypeException ;
34
- import io .appulse .utils .cache .LruCache ;
35
34
36
35
import io .netty .buffer .ByteBuf ;
37
- import io .netty .util .ByteProcessor ;
38
36
import lombok .EqualsAndHashCode ;
39
- import lombok .Getter ;
40
37
import lombok .NonNull ;
41
38
import lombok .ToString ;
42
39
import lombok .experimental .FieldDefaults ;
@@ -64,8 +61,6 @@ public class ErlangInteger extends ErlangTerm {
64
61
65
62
private static final int MAX_SMALL_BIG_BYTES_LENGTH = 255 ;
66
63
67
- private static final LruCache <Integer , ErlangInteger > CACHE = new LruCache <>(1000 );
68
-
69
64
/**
70
65
* Creates cached {@link ErlangInteger} value.
71
66
*
@@ -74,15 +69,7 @@ public class ErlangInteger extends ErlangTerm {
74
69
* @return new or cached {@link ErlangInteger} object
75
70
*/
76
71
public static ErlangInteger cached (byte value ) {
77
- int hashCode = 31 + value ;
78
-
79
- ErlangInteger result = CACHE .get (hashCode );
80
- if (result != null ) {
81
- return result ;
82
- }
83
- result = new ErlangInteger (value );
84
- CACHE .put (hashCode , result );
85
- return result ;
72
+ return ErlangIntegerCache .CACHE [value + (-ErlangIntegerCache .LOW )];
86
73
}
87
74
88
75
/**
@@ -93,16 +80,10 @@ public static ErlangInteger cached (byte value) {
93
80
* @return new or cached {@link ErlangInteger} object
94
81
*/
95
82
public static ErlangInteger cached (char value ) {
96
- int hashCode = 31 + (byte ) (value >> 8 );
97
- hashCode = 31 * hashCode + (byte ) value ;
98
-
99
- ErlangInteger result = CACHE .get (hashCode );
100
- if (result != null ) {
101
- return result ;
83
+ if (value <= ErlangIntegerCache .HIGH ) {
84
+ return ErlangIntegerCache .CACHE [value + (-ErlangIntegerCache .LOW )];
102
85
}
103
- result = new ErlangInteger (value );
104
- CACHE .put (hashCode , result );
105
- return result ;
86
+ return new ErlangInteger (value );
106
87
}
107
88
108
89
/**
@@ -113,16 +94,10 @@ public static ErlangInteger cached (char value) {
113
94
* @return new or cached {@link ErlangInteger} object
114
95
*/
115
96
public static ErlangInteger cached (short value ) {
116
- int hashCode = 31 + (byte ) (value >> 8 );
117
- hashCode = 31 * hashCode + (byte ) value ;
118
-
119
- ErlangInteger result = CACHE .get (hashCode );
120
- if (result != null ) {
121
- return result ;
97
+ if (value >= ErlangIntegerCache .LOW && value <= ErlangIntegerCache .HIGH ) {
98
+ return ErlangIntegerCache .CACHE [value + (-ErlangIntegerCache .LOW )];
122
99
}
123
- result = new ErlangInteger (value );
124
- CACHE .put (hashCode , result );
125
- return result ;
100
+ return new ErlangInteger (value );
126
101
}
127
102
128
103
/**
@@ -133,18 +108,10 @@ public static ErlangInteger cached (short value) {
133
108
* @return new or cached {@link ErlangInteger} object
134
109
*/
135
110
public static ErlangInteger cached (int value ) {
136
- int hashCode = 31 + (byte ) (value >> 24 );
137
- hashCode = 31 * hashCode + (byte ) (value >> 16 );
138
- hashCode = 31 * hashCode + (byte ) (value >> 8 );
139
- hashCode = 31 * hashCode + (byte ) value ;
140
-
141
- ErlangInteger result = CACHE .get (hashCode );
142
- if (result != null ) {
143
- return result ;
111
+ if (value >= ErlangIntegerCache .LOW && value <= ErlangIntegerCache .HIGH ) {
112
+ return ErlangIntegerCache .CACHE [value + (-ErlangIntegerCache .LOW )];
144
113
}
145
- result = new ErlangInteger (value );
146
- CACHE .put (hashCode , result );
147
- return result ;
114
+ return new ErlangInteger (value );
148
115
}
149
116
150
117
/**
@@ -155,22 +122,10 @@ public static ErlangInteger cached (int value) {
155
122
* @return new or cached {@link ErlangInteger} object
156
123
*/
157
124
public static ErlangInteger cached (long value ) {
158
- int hashCode = 31 + (byte ) (value >> 56 );
159
- hashCode = 31 * hashCode + (byte ) (value >> 48 );
160
- hashCode = 31 * hashCode + (byte ) (value >> 40 );
161
- hashCode = 31 * hashCode + (byte ) (value >> 32 );
162
- hashCode = 31 * hashCode + (byte ) (value >> 24 );
163
- hashCode = 31 * hashCode + (byte ) (value >> 16 );
164
- hashCode = 31 * hashCode + (byte ) (value >> 8 );
165
- hashCode = 31 * hashCode + (byte ) value ;
166
-
167
- ErlangInteger result = CACHE .get (hashCode );
168
- if (result != null ) {
169
- return result ;
125
+ if (value >= ErlangIntegerCache .LOW && value <= ErlangIntegerCache .HIGH ) {
126
+ return ErlangIntegerCache .CACHE [(int ) value + (-ErlangIntegerCache .LOW )];
170
127
}
171
- result = new ErlangInteger (value );
172
- CACHE .put (hashCode , result );
173
- return result ;
128
+ return new ErlangInteger (value );
174
129
}
175
130
176
131
/**
@@ -187,39 +142,6 @@ public static ErlangInteger cached (BigInteger value) {
187
142
return new ErlangInteger (value );
188
143
}
189
144
190
- public static ErlangInteger cached (TermType type , @ NonNull ByteBuf buffer ) {
191
- int index = buffer .readerIndex ();
192
- ByteArrayHashCode byteProcessor = new ByteArrayHashCode ();
193
-
194
- int length ;
195
- switch (type ) {
196
- case SMALL_INTEGER :
197
- length = Byte .BYTES ;
198
- break ;
199
- case INTEGER :
200
- length = Integer .BYTES ;
201
- break ;
202
- case SMALL_BIG :
203
- length = buffer .readByte () + Byte .BYTES ;
204
- break ;
205
- case LARGE_BIG :
206
- default :
207
- length = buffer .readInt () + Byte .BYTES ;
208
- }
209
-
210
- buffer .forEachByte (buffer .readerIndex (), length , byteProcessor );
211
-
212
- return CACHE .compute (byteProcessor .getHashCode (), (key , value ) -> {
213
- if (value == null ) {
214
- buffer .readerIndex (index );
215
- return new ErlangInteger (type , buffer );
216
- } else {
217
- buffer .skipBytes (length );
218
- return value ;
219
- }
220
- });
221
- }
222
-
223
145
BigInteger value ;
224
146
225
147
@ NonFinal
@@ -476,16 +398,21 @@ private void reverse (byte[] data) {
476
398
}
477
399
}
478
400
479
- @ Getter
480
- @ FieldDefaults (level = PRIVATE )
481
- private static class ByteArrayHashCode implements ByteProcessor {
401
+ @ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
402
+ private static class ErlangIntegerCache {
482
403
483
- int hashCode = 1 ;
404
+ private static final int LOW = - 128 ;
484
405
485
- @ Override
486
- public boolean process (byte value ) throws Exception {
487
- hashCode = 31 * hashCode + value ;
488
- return true ;
406
+ private static final int HIGH = 127 ;
407
+
408
+ private static final ErlangInteger [] CACHE ;
409
+
410
+ static {
411
+ CACHE = new ErlangInteger [(HIGH - LOW ) + 1 ];
412
+ int value = LOW ;
413
+ for (int index = 0 ; index < CACHE .length ; index ++) {
414
+ CACHE [index ] = new ErlangInteger (value ++);
415
+ }
489
416
}
490
417
}
491
418
}
0 commit comments