Skip to content

Commit

Permalink
Avoid repeated field usage, eliminate bound checks
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 5, 2024
1 parent 59a9533 commit a8b9a82
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,30 +233,32 @@ public int getIndexU2() {
* we have a valid opcode.
*/
public boolean next() {
if (nextBci >= endBci()) {
var bci = nextBci;
var end = endBci();
if (bci >= end) {
return false;
}

bci = nextBci;
int code = getU1Unchecked(bci);
int len = LENGTHS[code];
int len = LENGTHS[code & 0xFF]; // & 0xFF eliminates bound check
this.bci = bci;
opcode = code;
isWide = false;
if (len <= 0) {
len = checkSpecialInstruction(code);
len = checkSpecialInstruction(bci, end, code); // sets opcode
}

if (len <= 0 || (nextBci += len) > endBci()) {
if (len <= 0 || (nextBci += len) > end) {
opcode = ILLEGAL;
}

return true;
}

// Put rarely used code in another method to reduce code size
private int checkSpecialInstruction(int code) {
private int checkSpecialInstruction(int bci, int end, int code) {
if (code == WIDE) {
if (bci + 1 >= endBci()) {
if (bci + 1 >= end) {
return -1;
}
opcode = code = getIndexU1();
Expand All @@ -266,7 +268,7 @@ private int checkSpecialInstruction(int code) {
}
if (code == TABLESWITCH) {
int alignedBci = align(bci + 1);
if (alignedBci + 3 * 4 >= endBci()) {
if (alignedBci + 3 * 4 >= end) {
return -1;
}
int lo = getIntUnchecked(alignedBci + 1 * 4);
Expand All @@ -276,7 +278,7 @@ private int checkSpecialInstruction(int code) {
}
if (code == LOOKUPSWITCH) {
int alignedBci = align(bci + 1);
if (alignedBci + 2 * 4 >= endBci()) {
if (alignedBci + 2 * 4 >= end) {
return -1;
}
int npairs = getIntUnchecked(alignedBci + 4);
Expand Down

0 comments on commit a8b9a82

Please sign in to comment.