John Lawrence Aspden
John Lawrence Aspden

Reputation: 17470

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

In trying to optimize C and LISP, looking at the assembler 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?

Upvotes: 16

Views: 2074

Answers (3)

gtrak
gtrak

Reputation: 5676

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.

Upvotes: 8

haylem
haylem

Reputation: 22663

Some hand-made solutions:

Upvotes: 4

Abhinav Sarkar
Abhinav Sarkar

Reputation: 23792

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

Upvotes: 14

Related Questions