Hi All,
It has always irritated me that they create so many temporary objects. The
patch reduces the number of StringBuilders that are created (originally 2 for
each stack trace element)
The patch reuses the StringBuilder and only sends text to the underlying stream
for every line.
It could be optimised to run faster by sending updates of more than one line
but that would change the memory footprint so this seemed a good compromise.
I include the patch, a micro-benchmark and the results of the micro-benchmark
which show an improvement of 80% throughput (ie it is almost twice as fast)
Regards
Mike
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package compare;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
*
* @author mike
*/
public class TestThrow {
public static void main(String[] args) throws InterruptedException {
try {
generateThrow1(5);
} catch (Throwable t) {
int count = 100;
StringWriter sw = new StringWriter(20000);
PrintWriter pw = new PrintWriter(sw);
System.out.println(sw);
for (int j = 0; j < 100; j++) {
long total = 0;
for (int i = 0; i < count; i++) {
sw.getBuffer().setLength(0);
long start = System.nanoTime();
t.printStackTrace(pw);
long end = System.nanoTime();
total += end - start;
}
System.out.println((total / count));
}
}
}
private static void generateThrow1(int depth) {
generateThrow2(depth);
}
private static void generateThrow2(int depth) {
generateThrow3(depth);
}
private static void generateThrow3(int depth) {
generateThrow4(depth);
}
private static void generateThrow4(int depth) {
generateThrow5(depth);
}
private static void generateThrow5(int depth) {
if (depth == 0) {
throw new RuntimeException("some text ..." + depth);
} else {
try {
generateThrow1(depth - 1);
} catch (RuntimeException x) {
throw new RuntimeException("some text ..." + depth, x);
}
}
}
}