Skip to content

Commit

Permalink
Reduce conditionals in AbstractReferenceCountedByteBuf
Browse files Browse the repository at this point in the history
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 01523e7.

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.
  • Loading branch information
normanmaurer authored and liuzhengyang committed Sep 10, 2017
1 parent 388c895 commit 8781b59
Showing 1 changed file with 5 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ protected final void setRefCnt(int refCnt) {
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;
Expand All @@ -79,14 +76,12 @@ public ByteBuf retain(int increment) {
}

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;
}
}
Expand Down

0 comments on commit 8781b59

Please sign in to comment.