Skip to content

Instantly share code, notes, and snippets.

@asimjalis
Last active March 14, 2019 12:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save asimjalis/423d139cd34c4872a2b6ade9e7f6f447 to your computer and use it in GitHub Desktop.
Save asimjalis/423d139cd34c4872a2b6ade9e7f6f447 to your computer and use it in GitHub Desktop.
How To Speed Up Clojure Hello World 100x Using GraalVM

How To Speed Up Clojure Hello World 100x Using GraalVM

Performance

Version Command Time (seconds)
Java time java -jar target/fastclj-1.0-standalone.jar 1.354
GraalVM time ./fastclj-1.0-standalone 0.014

Details

# Create project.
lein new fastclj; cd fastclj

# Create project.clj.
cat<<'END'>./project.clj
(defproject fastclj "1.0"
  :description "Fast hello world using GraalVM"
  :dependencies [[org.clojure/clojure "1.9.0"]]
  :main fastclj.core
  :aot  :all)
END

# Create core.clj.
cat<<'END'>./src/fastclj/core.clj
(ns fastclj.core (:gen-class))
(defn -main [& args] (println "Hello world!"))
END

# Compile jar.
lein compile && lein uberjar

# Test jar perf.
time java -jar target/fastclj-1.0-standalone.jar

# Download GraalVM and untar.
wget https://github.com/oracle/graal/releases/download/vm-1.0.0-rc12/graalvm-ce-1.0.0-rc12-macos-amd64.tar.gz
tar zxvf graalvm-ce-1.0.0-rc12-macos-amd64.tar.gz

# Path to GraalVM binaries.
GRAAL_BIN="$PWD/graalvm-ce-1.0.0-rc12/Contents/Home/bin"

# Create native image from jar. 
$GRAAL_BIN/native-image \
  -H:+ReportUnsupportedElementsAtRuntime \
  -J-Xmx3G -J-Xms3G --no-server \
  -jar target/fastclj-1.0-standalone.jar

# Inspect relative sizes.
ls -l ./fastclj-1.0-standalone target/*.jar

# Test perf of GraalVM-produced executable.
time ./fastclj-1.0-standalone 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment