Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Babashka: a native Clojure interpreter for scri...

Babashka: a native Clojure interpreter for scripting @ GraalVM Workshop 2021

Talk held at the GraalVM Workshop 2021 about babashka: a native Clojure interpreter for scripting.

Michiel Borkent

February 26, 2021
Tweet

More Decks by Michiel Borkent

Other Decks in Programming

Transcript

  1. • Native Clojure scripting tool, single binary, no JVM (GraalVM

    compiled), fast startup • Alternative to byte code compilation by Clojure compiler • Prevents context switch to bash for Clojure devs writing build scripts • Batteries included (arg parsing, JSON, http client/server, ...) • Supports multi-threading • Source compatibility with JVM Clojure + GraalVM = sane upgrade path = low risk adoption $ time bb '(+ 1 2 3)'
 6
 0.00s user 0.00s system 67% cpu 0.013 total
  2. The 5 second rule • Startup time vs performance •

    Sweet spot: short running scripts (< 5 seconds): use babashka (Clojure interpreter) • Long running performance intensive processes: use JVM Clojure compiler • Compile with GraalVM native-image for fast startup
  3. Enhancement: sqlite pod Sqlite pod: bb RPC-like extension Pods are

    started only once: now 80ms total No more shell output parsing, normal function calls
  4. Interpreter: SCI • Split out into its own project: Small

    Clojure Interpreter (sci) • Written in Clojure itself: .cljc -> runs on JVM and ClojureScript. • Leveraged by other native CLIs and ClojureScript projects • Performance: not as good as compiled Clojure or a Truffle interpreter, but good enough for typical bash-like scripts • Yields small images (~11mb) / JS bundles (~120kb gzipped) • Can be used to glue together natively compiled functions using interpreted code
  5. Optimizations Resolving symbols to fns, locals and classes The actual

    function calls to native and interpreted fns Text to s- expressions S-expressions to "evaluator" forms Push as much work as possible to analyzer instead of evaluator
  6. Sci: eval-string (require '[sci.core :as sci]) (sci/eval-string "(+ 1 2

    3)") ;;=> 6 (def ctx (sci/init {:namespaces {'foo {'x 1}}}))
 (sci/eval-string* ctx "foo/x") ;;=> 1 (sci/eval-string* ctx "
 (require '[foo :refer [x]])
 (defn add-x [n] (+ n x))") 
 (sci/eval-string* ctx "(add-x 10)") ;;=> 11
  7. Sci: mixing native and interpreted fns (def ctx (sci/init {:namespaces

    {'clojure.core {'assoc assoc} 'cheshire.core {'generate-string
 generate-string}}})) (sci/eval-string* ctx
 "(cheshire.core/generate-string (assoc {:a 1} :b 2))") ;;=> {"a":1,"b":2}
  8. Clojure + GraalVM • CLJ-1472: issue with GraalVM and locking

    macro • Solved in 1.10.2 • MethodHandle issue: solved in GraalVM 21.0.0 • https://github.com/lread/clj-graal-docs • https://github.com/BrunoBonacci/graalvm-clojure/
  9. Truffle • Espresso compilation of Clojure compiler? • Clojure on

    Truffle? (Thesis from 2015) • AOT of guest language? • Defining new classes at runtime? • Mixing host language AOT-ed fns called from guest language?
  10. Selected talks: • Babashka and GraalVM; taking Clojure to new

    places • Writing Clojure on the command line • Babashka and sci internals • https://github.com/babashka/babashka • https://github.com/borkdude/sci On Github: Michiel Borkent @borkdude