Skip to content

Commit

Permalink
Use binary search for getSuperBlockFromOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Sep 15, 2021
1 parent c71eff6 commit 5ebfa5d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/org/mozilla/classfile/ClassFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import org.mozilla.javascript.Kit;
import org.mozilla.javascript.ObjArray;
import org.mozilla.javascript.UintMap;

Expand Down Expand Up @@ -1443,13 +1445,22 @@ void generate() {
}

private SuperBlock getSuperBlockFromOffset(int offset) {
for (int i = 0; i < superBlocks.length; i++) {
SuperBlock sb = superBlocks[i];
if (sb == null) {
break;
} else if (offset >= sb.getStart() && offset < sb.getEnd()) {
return sb;
}
int startIdx = Arrays.binarySearch(itsSuperBlockStarts, 0,
itsSuperBlockStartsTop, offset);

if (startIdx < 0) {
// if offset was not found, insertion point is returned (See
// Arrays.binarySearch)
// we convert it back to the matching superblock.
startIdx = -startIdx - 2;
}
if (startIdx < itsSuperBlockStartsTop) {
SuperBlock sb = superBlocks[startIdx];
// check, if it is really the matching one
if (offset < sb.getStart() || offset >= sb.getEnd())
Kit.codeBug();
return sb;

}
throw new IllegalArgumentException("bad offset: " + offset);
}
Expand Down

0 comments on commit 5ebfa5d

Please sign in to comment.