Skip to content

Commit 19dc19a

Browse files
Artem LabazinArtem Labazin
Artem Labazin
authored and
Artem Labazin
committed
Quick fix release for number caches
1 parent 7630ead commit 19dc19a

File tree

28 files changed

+76
-135
lines changed

28 files changed

+76
-135
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Turn on checkstyle JavaDocs module.
2323
- Add updates to the protocol, like new `ControlMessage`.
2424

25+
## [1.6.6](https://github.com/appulse-projects/encon-java/releases/tag/1.6.6) - 2018-10-02
26+
27+
### Changed
28+
29+
- Quick fix for [GitHub issue](https://github.com/appulse-projects/encon-java/issues/11), refactored `ErlangInteger` cache;
30+
- Correctly setup `checkstyle`/`epmd`/`findbugs` validation phases.
31+
2532
## [1.6.5](https://github.com/appulse-projects/encon-java/releases/tag/1.6.5) - 2018-09-28
2633

2734
### Changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $> mvn clean compile
4646
[INFO] ------------------------------------------------------------------------
4747
[INFO] Reactor Summary:
4848
[INFO]
49-
[INFO] encon 1.6.5 ........................................ SUCCESS [ 1.210 s]
49+
[INFO] encon 1.6.6 ........................................ SUCCESS [ 1.210 s]
5050
[INFO] encon-common ....................................... SUCCESS [ 25.693 s]
5151
[INFO] encon-terms ........................................ SUCCESS [ 27.517 s]
5252
[INFO] encon-config ....................................... SUCCESS [ 18.707 s]
@@ -64,7 +64,7 @@ $> mvn clean compile
6464
[INFO] handler-advanced ................................... SUCCESS [ 11.289 s]
6565
[INFO] load-config ........................................ SUCCESS [ 3.725 s]
6666
[INFO] load-config-spring ................................. SUCCESS [ 6.420 s]
67-
[INFO] benchmark 1.6.5 .................................... SUCCESS [ 5.594 s]
67+
[INFO] benchmark 1.6.6 .................................... SUCCESS [ 5.594 s]
6868
[INFO] ------------------------------------------------------------------------
6969
[INFO] BUILD SUCCESS
7070
[INFO] ------------------------------------------------------------------------

benchmark/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>benchmark</artifactId>

encon-common/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-common</artifactId>

encon-config/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First of all, add config's dependency:
1414
<dependency>
1515
<groupId>io.appulse.encon</groupId>
1616
<artifactId>encon-config</artifactId>
17-
<version>1.6.5</version>
17+
<version>1.6.6</version>
1818
</dependency>
1919
...
2020
</dependencies>
@@ -23,7 +23,7 @@ First of all, add config's dependency:
2323
**Gradle**:
2424

2525
```groovy
26-
compile 'io.appulse.encon:encon-config:1.6.5'
26+
compile 'io.appulse.encon:encon-config:1.6.6'
2727
```
2828

2929
### File based configuration

encon-config/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-config</artifactId>

encon-databind/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ First of all, add databind's dependency:
1414
<dependency>
1515
<groupId>io.appulse.encon</groupId>
1616
<artifactId>encon</artifactId>
17-
<version>1.6.5</version>
17+
<version>1.6.6</version>
1818
</dependency>
1919
<dependency>
2020
<groupId>io.appulse.encon</groupId>
2121
<artifactId>encon-databind</artifactId>
22-
<version>1.6.5</version>
22+
<version>1.6.6</version>
2323
</dependency>
2424
...
2525
</dependencies>
@@ -28,8 +28,8 @@ First of all, add databind's dependency:
2828
**Gradle**:
2929

3030
```groovy
31-
compile 'io.appulse.encon:encon:1.6.5'
32-
compile 'io.appulse.encon:encon-databind:1.6.5'
31+
compile 'io.appulse.encon:encon:1.6.6'
32+
compile 'io.appulse.encon:encon-databind:1.6.6'
3333
```
3434

3535
Let's imagine, you have POJO like this:

encon-databind/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-databind</artifactId>

encon-handler/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First of all, add dependency:
1414
<dependency>
1515
<groupId>io.appulse.encon</groupId>
1616
<artifactId>encon-handler</artifactId>
17-
<version>1.6.5</version>
17+
<version>1.6.6</version>
1818
</dependency>
1919
...
2020
</dependencies>
@@ -23,7 +23,7 @@ First of all, add dependency:
2323
**Gradle**:
2424

2525
```groovy
26-
compile 'io.appulse.encon:encon-handler:1.6.5'
26+
compile 'io.appulse.encon:encon-handler:1.6.6'
2727
```
2828

2929
### Basics

encon-handler/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-handler</artifactId>

encon-spring/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-spring</artifactId>

encon-terms/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.6.5</version>
28+
<version>1.6.6</version>
2929
</parent>
3030

3131
<artifactId>encon-terms</artifactId>

encon-terms/src/main/java/io/appulse/encon/terms/ErlangTerm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static <T extends ErlangTerm> T newInstance (@NonNull ByteBuf buffer) {
9595
case INTEGER:
9696
case SMALL_BIG:
9797
case LARGE_BIG:
98-
return (T) ErlangInteger.cached(type, buffer);
98+
return (T) new ErlangInteger(type, buffer);
9999
case FLOAT:
100100
case NEW_FLOAT:
101101
return (T) new ErlangFloat(type, buffer);

encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangInteger.java

+26-99
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@
3131
import io.appulse.encon.terms.TermType;
3232
import io.appulse.encon.terms.exception.ErlangTermDecodeException;
3333
import io.appulse.encon.terms.exception.IllegalErlangTermTypeException;
34-
import io.appulse.utils.cache.LruCache;
3534

3635
import io.netty.buffer.ByteBuf;
37-
import io.netty.util.ByteProcessor;
3836
import lombok.EqualsAndHashCode;
39-
import lombok.Getter;
4037
import lombok.NonNull;
4138
import lombok.ToString;
4239
import lombok.experimental.FieldDefaults;
@@ -64,8 +61,6 @@ public class ErlangInteger extends ErlangTerm {
6461

6562
private static final int MAX_SMALL_BIG_BYTES_LENGTH = 255;
6663

67-
private static final LruCache<Integer, ErlangInteger> CACHE = new LruCache<>(1000);
68-
6964
/**
7065
* Creates cached {@link ErlangInteger} value.
7166
*
@@ -74,15 +69,7 @@ public class ErlangInteger extends ErlangTerm {
7469
* @return new or cached {@link ErlangInteger} object
7570
*/
7671
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)];
8673
}
8774

8875
/**
@@ -93,16 +80,10 @@ public static ErlangInteger cached (byte value) {
9380
* @return new or cached {@link ErlangInteger} object
9481
*/
9582
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)];
10285
}
103-
result = new ErlangInteger(value);
104-
CACHE.put(hashCode, result);
105-
return result;
86+
return new ErlangInteger(value);
10687
}
10788

10889
/**
@@ -113,16 +94,10 @@ public static ErlangInteger cached (char value) {
11394
* @return new or cached {@link ErlangInteger} object
11495
*/
11596
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)];
12299
}
123-
result = new ErlangInteger(value);
124-
CACHE.put(hashCode, result);
125-
return result;
100+
return new ErlangInteger(value);
126101
}
127102

128103
/**
@@ -133,18 +108,10 @@ public static ErlangInteger cached (short value) {
133108
* @return new or cached {@link ErlangInteger} object
134109
*/
135110
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)];
144113
}
145-
result = new ErlangInteger(value);
146-
CACHE.put(hashCode, result);
147-
return result;
114+
return new ErlangInteger(value);
148115
}
149116

150117
/**
@@ -155,22 +122,10 @@ public static ErlangInteger cached (int value) {
155122
* @return new or cached {@link ErlangInteger} object
156123
*/
157124
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)];
170127
}
171-
result = new ErlangInteger(value);
172-
CACHE.put(hashCode, result);
173-
return result;
128+
return new ErlangInteger(value);
174129
}
175130

176131
/**
@@ -187,39 +142,6 @@ public static ErlangInteger cached (BigInteger value) {
187142
return new ErlangInteger(value);
188143
}
189144

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-
223145
BigInteger value;
224146

225147
@NonFinal
@@ -476,16 +398,21 @@ private void reverse (byte[] data) {
476398
}
477399
}
478400

479-
@Getter
480-
@FieldDefaults(level = PRIVATE)
481-
private static class ByteArrayHashCode implements ByteProcessor {
401+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
402+
private static class ErlangIntegerCache {
482403

483-
int hashCode = 1;
404+
private static final int LOW = -128;
484405

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+
}
489416
}
490417
}
491418
}

encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangIntegerTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ public void decode () throws Exception {
167167
}
168168
}
169169

170+
@Test
171+
public void cached () {
172+
ErlangInteger num1 = ErlangInteger.cached(1273);
173+
ErlangInteger num2 = ErlangInteger.cached(117);
174+
assertThat(num1).isNotEqualTo(num2);
175+
}
176+
170177
@SneakyThrows
171178
private byte[] bytes (char value) {
172179
try (OtpOutputStream output = new OtpOutputStream()) {

encon/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Adding encon's dependency to your `JVM` app:
2121
<dependency>
2222
<groupId>io.appulse.encon</groupId>
2323
<artifactId>encon</artifactId>
24-
<version>1.6.5</version>
24+
<version>1.6.6</version>
2525
</dependency>
2626
...
2727
</dependencies>
@@ -30,7 +30,7 @@ Adding encon's dependency to your `JVM` app:
3030
**Gradle**:
3131

3232
```groovy
33-
compile 'io.appulse.encon:encon:1.6.5'
33+
compile 'io.appulse.encon:encon:1.6.6'
3434
```
3535

3636
## Start the Node

0 commit comments

Comments
 (0)