Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jvm

clojure

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?

like image 932
John Lawrence Aspden Avatar asked Sep 29 '10 08:09

John Lawrence Aspden


People also ask

Does clojure compile to Java bytecode?

Clojure compiles all code you load on-the-fly into JVM bytecode, but sometimes it is advantageous to compile ahead-of-time (AOT). Some reasons to use AOT compilation are: To deliver your application without source.

Does clojure use JVM?

Clojure is a dynamically-typed, functional programming language that runs on the JVM (Java 5 or greater) and provides interoperability with Java. A major goal of the language is to make it easier to implement applications that access data from multiple threads (concurrency).

Can Java read byte code?

Using javap The Java command-line comes with the javap tool that displays information about the fields, constructors, and methods of a class file. Based on the options used, it can disassemble a class and show the instructions that comprise the Java bytecode.

Is clojure compiled?

Clojure is a compiled language, so one might wonder when you have to run the compiler. You don't. Anything you enter into the REPL or load using load-file is automatically compiled to JVM bytecode on the fly. Compiling ahead-of-time is also possible, but not required.


1 Answers

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
like image 135
Abhinav Sarkar Avatar answered Oct 27 '22 01:10

Abhinav Sarkar