From f0831e048bd9f23e6d51c18f10e5dec2383dc17f Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Fri, 17 Sep 2021 15:19:34 +0200 Subject: [PATCH] narrowed lookup range for big exception tables --- .../mozilla/classfile/ClassFileWriter.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/org/mozilla/classfile/ClassFileWriter.java b/src/org/mozilla/classfile/ClassFileWriter.java index 07dd4d84f2..90fd245fb1 100644 --- a/src/org/mozilla/classfile/ClassFileWriter.java +++ b/src/org/mozilla/classfile/ClassFileWriter.java @@ -1659,6 +1659,37 @@ private void executeBlock(SuperBlock work) { TypeInfo.print(locals, localsTop, stack, stackTop, itsConstantPool); } + int etStart = 0; + int etEnd = itsExceptionTableTop; + if (itsExceptionTableTop > 1) { + // determine the relevant search range in the exception table. + // this will reduce the search time if we have many exception + // blocks. There may be false positives in the range, but in + // most cases, this code does a good job, which leads in to + // fewer checks in the double-for-loop. + etStart = Integer.MAX_VALUE; + etEnd = 0; + for (int i = 0; i < itsExceptionTableTop; i++) { + ExceptionTableEntry ete = itsExceptionTable[i]; + // we have found an entry, that overlaps with our work block + if (work.getEnd() >= getLabelPC(ete.itsStartLabel) + && work.getStart() < getLabelPC(ete.itsEndLabel)) { + etStart = Math.min(etStart, i); + etEnd = Math.max(etEnd, i + 1); + } + } + if (DEBUGSTACK) { + if (etStart == 0 && etEnd == itsExceptionTableTop) { + System.out.println("lookup size " + itsExceptionTableTop + ": could not be reduced"); + } else if (etStart < 0) { + System.out.println("lookup size " + itsExceptionTableTop + ": reduced completely"); + } else { + System.out.println("lookup size " + itsExceptionTableTop + ": reduced to " + (etEnd-etStart)); + } + } + } + + for (int bci = work.getStart(); bci < work.getEnd(); bci += next) { bc = itsCodeBuffer[bci] & 0xFF; next = execute(bci); @@ -1719,7 +1750,7 @@ private void executeBlock(SuperBlock work) { } } - for (int i = 0; i < itsExceptionTableTop; i++) { + for (int i = etStart; i < etEnd; i++) { ExceptionTableEntry ete = itsExceptionTable[i]; int startPC = getLabelPC(ete.itsStartLabel); int endPC = getLabelPC(ete.itsEndLabel);