Skip to content

Commit d7f32d8

Browse files
committed
8341415: Optimize RawBytecodeHelper::next
Reviewed-by: liach
1 parent 6af1358 commit d7f32d8

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/java.base/share/classes/jdk/internal/classfile/impl/RawBytecodeHelper.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ public static int align(int n) {
299299
private int nextBci;
300300
private int bci;
301301
private int opcode;
302-
private boolean isWide;
303302

304303
public static CodeRange of(byte[] array) {
305304
return new CodeRange(array, array.length);
@@ -341,14 +340,14 @@ public void reset(int nextBci) {
341340
* be valid and can be accessed unchecked.
342341
*/
343342
public int opcode() {
344-
return opcode;
343+
return opcode & 0xFF;
345344
}
346345

347346
/**
348347
* Returns whether the current functional opcode is in wide.
349348
*/
350349
public boolean isWide() {
351-
return isWide;
350+
return (opcode & (WIDE << 8)) != 0;
352351
}
353352

354353
/**
@@ -411,7 +410,7 @@ public int destW() {
411410

412411
// *load, *store, iinc
413412
public int getIndex() {
414-
return isWide ? getU2Unchecked(bci + 2) : getIndexU1();
413+
return isWide() ? getU2Unchecked(bci + 2) : getIndexU1();
415414
}
416415

417416
// ldc
@@ -443,12 +442,11 @@ public boolean next() {
443442
int len = LENGTHS[code & 0xFF]; // & 0xFF eliminates bound check
444443
this.bci = bci;
445444
opcode = code;
446-
isWide = false;
447445
if (len <= 0) {
448446
len = checkSpecialInstruction(bci, end, code); // sets opcode
449447
}
450448

451-
if (len <= 0 || (nextBci += len) > end) {
449+
if ((nextBci += len) > end) {
452450
opcode = ILLEGAL;
453451
}
454452

@@ -457,37 +455,34 @@ public boolean next() {
457455

458456
// Put rarely used code in another method to reduce code size
459457
private int checkSpecialInstruction(int bci, int end, int code) {
458+
int len = -1;
460459
if (code == WIDE) {
461-
if (bci + 1 >= end) {
462-
return -1;
460+
if (bci + 1 < end) {
461+
opcode = (WIDE << 8) | (code = getIndexU1());
462+
// Validated in UtilTest.testOpcodeLengthTable
463+
len = LENGTHS[code] * 2;
463464
}
464-
opcode = code = getIndexU1();
465-
isWide = true;
466-
// Validated in UtilTest.testOpcodeLengthTable
467-
return LENGTHS[code] * 2;
468-
}
469-
if (code == TABLESWITCH) {
465+
} else if (code == TABLESWITCH) {
470466
int alignedBci = align(bci + 1);
471-
if (alignedBci + 3 * 4 >= end) {
472-
return -1;
467+
if (alignedBci + 3 * 4 < end) {
468+
int lo = getIntUnchecked(alignedBci + 1 * 4);
469+
int hi = getIntUnchecked(alignedBci + 2 * 4);
470+
long l = alignedBci - bci + (3L + (long) hi - lo + 1L) * 4L;
471+
len = l > 0 && ((int) l == l) ? (int) l : -1;
473472
}
474-
int lo = getIntUnchecked(alignedBci + 1 * 4);
475-
int hi = getIntUnchecked(alignedBci + 2 * 4);
476-
long l = alignedBci - bci + (3L + (long) hi - lo + 1L) * 4L;
477-
return l > 0 && ((int) l == l) ? (int) l : -1;
478-
}
479-
if (code == LOOKUPSWITCH) {
473+
} else if (code == LOOKUPSWITCH) {
480474
int alignedBci = align(bci + 1);
481-
if (alignedBci + 2 * 4 >= end) {
482-
return -1;
483-
}
484-
int npairs = getIntUnchecked(alignedBci + 4);
485-
if (npairs < 0) {
486-
return -1;
475+
if (alignedBci + 2 * 4 < end) {
476+
int npairs = getIntUnchecked(alignedBci + 4);
477+
if (npairs >= 0) {
478+
long l = alignedBci - bci + (2L + 2L * npairs) * 4L;
479+
len = l > 0 && ((int) l == l) ? (int) l : -1;
480+
}
487481
}
488-
long l = alignedBci - bci + (2L + 2L * npairs) * 4L;
489-
return l > 0 && ((int) l == l) ? (int) l : -1;
490482
}
491-
return -1;
483+
if (len <= 0) {
484+
opcode = ILLEGAL;
485+
}
486+
return len;
492487
}
493488
}

0 commit comments

Comments
 (0)