Reduce conditionals in AbstractReferenceCountedByteBuf

Motivation:
AbstractReferenceCountedByteBuf as independent conditional statements to check the bounds of the retain IllegalReferenceCountException condition. One of the exceptions also uses the incorrect increment. The same fix was done for AbstractReferenceCounted as 01523e78358830d4c22ca7c866df4691c8331bd8.

Modifications:
- Combined independent conditional checks into 1 where possible
- Correct IllegalReferenceCountException with incorrect increment
- Remove the subtract to check for overflow and re-use the addition and check for overflow to remove 1 arithmetic operation (see http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2)

Result:
AbstractReferenceCountedByteBuf has less independent branch statements and more correct IllegalReferenceCountException. Compilation size of AbstractReferenceCountedByteBuf.retain() is reduced.
This commit is contained in:
Norman Maurer 2016-08-02 16:40:31 +02:00
parent a496a48fae
commit 1e006be66b

View File

@ -59,11 +59,8 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
public ByteBuf retain() {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalReferenceCountException(0, 1);
}
if (refCnt == Integer.MAX_VALUE) {
throw new IllegalReferenceCountException(Integer.MAX_VALUE, 1);
if (refCnt == 0 || refCnt == Integer.MAX_VALUE) {
throw new IllegalReferenceCountException(refCnt, 1);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
break;
@ -79,14 +76,12 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
}
for (;;) {
final int nextCnt;
int refCnt = this.refCnt;
if (refCnt == 0) {
throw new IllegalReferenceCountException(0, increment);
}
if (refCnt > Integer.MAX_VALUE - increment) {
if (refCnt == 0 || (nextCnt = refCnt + increment) < 0) {
throw new IllegalReferenceCountException(refCnt, increment);
}
if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) {
if (refCntUpdater.compareAndSet(this, refCnt, nextCnt)) {
break;
}
}