开发者

How can I examine the JVM bytecode for a clojure function?

In trying to optimize C and LISP, looking at the assemb开发者_如何学JAVAler code output by the compiler can be a great help.

Clojure presumably compiles to a JVM bytecode that would be equally helpful.

How do I see it?


Clojure dynamically compiles all the Clojure code to bytecode at the runtime. I am not sure how you can see this dynamically compiled bytecode. However, you can do Ahead Of Time (AOT) compilation of your Clojure code yourself and that will generate .class files. Then you can use javap to see the bytecode.

Use the compile function in Clojure/core to compile your namespace:

compile function

Usage: (compile lib)

Compiles the namespace named by the symbol lib into a set of classfiles. The source for the lib must be in a proper classpath-relative directory. The output files will go into the directory specified by compile-path, and that directory too must be in the classpath.

Then use javap:

javap -l -c -s -private MyClass


I made a small library and lein-plugin for this, check it out:

https://groups.google.com/d/msg/clojure/_BBXpt_GPIw/WWq2VbWkEKEJ

https://github.com/gtrak/no.disassemble

It has the added advantage of not requiring AOT.


Some hand-made solutions:

  • Java Bytecode Editor
  • the JDK's Java Class File Disassembler (javap)
  • if using Eclipse, you can use the Bytecode Outline plug-in or Bytecode Visualizer.
  • Google for more :)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜