views:

216

answers:

2

Optimizations based on escape analysis is a planned feature for Proguard. In the meantime, are there any existing tools like proguard that already do optimizations which require escape analysis?

+2  A: 

Yes, I think the Soot framework performs escape analysis.

Joa Ebert
How do you configure soot to do whole program optimizations (such as escape analysis), on android applications? It appears the framework simply assumes you have a main function and builds the call tree from there only, but android applications don't have main functions. Proguard lets you keep multiple classes, so that each method in the class becomes a new "root" in the call tree analysis for whole program optimization. I can't find a similar option for soot.
Jeremy Bell
Going to mark this as the answer. I opened a new question specifically about soot and whole program optimization without a main function here: http://stackoverflow.com/questions/3093648/how-to-use-soot-to-do-whole-program-optimizations-on-android-applications
Jeremy Bell
+1  A: 

What do you expect from escape analysis on compiler level? Java classes are more like object files in C - they are linked in the JVM, hence the escape analysis can be performed only on single-method level, which is of limited usability and will hamper the debugging (e.g. you will have lines of code through which you can not step).

In Java's design, the compiler is quite dumb - it checks for correctness (like Lint), but doesn't try to optimize. The smart pieces are put in the JVM - it uses multiple optimization techniques to yield well performing code on the current platform, under the current conditions. Since the JVM knows all the code that is currently loaded it can assume a lot more than the compiler and perform speculative optimizations which are reverted the moment the assumptions are invalidated. HotSpot JVM can replace code with more optimized version on the fly while the function is running (e.g. in the middle of a loop as the code gets 'hotter').

When not in debugger, variables with non-overlapping lifetimes are collapsed, invariants are hoisted out of loops, loops are unrolled, etc. All this happens in the JIT-ted code and is done dependent on how much time is spent in this function (it does not make much sense to spend time optimizing code that never runs). If we perform some of these optimizations upfront, the JIT will have less freedom and the overall result might be a net negative.

Another optimization is stack allocation of objects that do not escape the current method - this is done in certain cases, though I read a paper somewhere that the time to perform rigorous escape analysis vs the time gained by optimizations suggests that it's not worth it, so the current strategy is more heuristic.

Overall, the more information the JVM has about your original code, the better it can optimize it. And the optimizations the JVM does are constantly improving, hence I would think about compiled code optimizations only when speaking about very restricted and basic JVMs like mobile phones. In these cases you want to run your application through obfuscator anyway (to shorten class names, etc.)

ddimitrov
I am only interested in the dalvik VM, and I am locked into targeting the 1.5/1.6 platform for at least another 6 months, and 2.1 for at least a year. Even if I could target 2.2, the JIT is specifically tuned to have very fast startup times and spend as little time as possible in JIT, so expensive optimizations like escape analysis plus scalar replacement are completely out. What I am looking for specifically is a static bytecode optimizer which can do scalar replacement based off of escape analysis for short-lived temporary objects.
Jeremy Bell