Since a few days, I got pingbacks from codebix.com. Their site contains a lot of links to a lot of blogs but no real added value as far as I can tell … who are those guys? What’s their game?
Remember when I wondered how slow it is to copy data around in memory?
This time, I needed to load a 2MB file from disk and examine it for certain patterns.
The original from jazzy spell checker worked like this: It did a binary search in the file for a code. The code would be at the start of each line and be separated from the correct word by an asterisk:
AT*wide AT*widow AT*width AT*wight AT*wit
The search algorithm would seek somewhere in the file, skip the current line, read the next one, compare the code against the one we look for. If our code was smaller, we would seek backward; if it was larger, we would seek forward. Standard binary search.
Only it didn’t work. The test took 4 seconds to load the file without finding anything. Debugging recursive algorithms isn’t as nice as looking at them …
So I considered a different approach: I would load the whole file, character by character, and remember the lines with the same code in an index. A good time to have a look at NIO, especially the grep example.
Question: If it takes 4 seconds to seek ten times in a RandomAccessFile and read about 2000 bytes from it, how long would it take to read it character by character, examine each line, build an index and then load whole chunks (say, 1KB each) from the file when a word needs to be looked up? Plus the additional work of removing the code pattern from the chunk to produce a list of words …
Answer: 0.3 seconds. That’s more than ten times faster. And I could probably optimize the code some more.
Conclusion: When it comes to performance, measurement beats superstition.
And the new code is easy to understand, and to test, too! ^_^
MDD is nice as long as you don’t have exceptions. If you generate 1,000 classes and then one of them needs something special, you’re in trouble.
I think support for applying patches to the result of the code generation step would be a nice feature.
Exceptions should have two purposes: 1. Clean up after an error and 2. help you solve the issue. Sadly, many Java developers often forget about #2.
So you end up with an exception thrown in
SignatureFileVerifier (no source). Or even in a native method. The error message is:
Invalid signature file digest for Manifest main attributes
Right. Which tells us exactly … nothing. The stack trace isn’t better:
java.lang.SecurityException: Invalid signature file digest for Manifest main attributes at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:221) at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:176) at java.util.jar.JarVerifier.processEntry(JarVerifier.java:233) at java.util.jar.JarVerifier.update(JarVerifier.java:188) at java.util.jar.JarFile.initializeVerifier(JarFile.java:325) at java.util.jar.JarFile.getInputStream(JarFile.java:390) at sun.misc.URLClassPath$JarLoader$1.getInputStream(URLClassPath.java:620) at sun.misc.Resource.cachedInputStream(Resource.java:59) at sun.misc.Resource.getByteBuffer(Resource.java:84) at java.net.URLClassLoader.defineClass(URLClassLoader.java:249) at java.net.URLClassLoader.access$100(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at org.eclipse.jface.action.LegacyActionTools.initLocalizedModifiers(LegacyActionTools.java:699) at org.eclipse.jface.action.LegacyActionTools.findLocalizedModifier(LegacyActionTools.java:356) at org.eclipse.jface.action.LegacyActionTools.convertLocalizedAccelerator(LegacyActionTools.java:167) at org.eclipse.jface.action.Action.setText(Action.java:665) at de.pdark.epen.editor.actions.ForwardAction.(ForwardAction.java:29) at de.pdark.epen.editor.actions.ForwardActionTest.testCreate(ForwardActionTest.java:21)
LegacyActionTools needs a class. Which one? Since I don’t have the source, how can I set a breakpoint?
Simple: Set the breakpoint in the constructor of the exception! Even native code has to pass through here, eventually.
James Sutherland: “Java performance optimization is part analysis, and part superstition and witch craft.”
In his blog post “What is faster? JVM Performance,” he compares various ways to solve problems in Java and how they perform. For example, how the various map types perform.
What surprised me as well was to poor performance of HashMap. 23% slower than Hashtable…
Another surprise that block synchronized code is twice as expensive as synchronized methods. Tools like FindBugs discourage using synchronized methods.
Or the impact of volatile on field access.
Merry Christmas to you all 🙂