liveBook · Manning

Compiler option

As explained in Clojure: The Essential Reference

The compiler-options dynamic variable is a Clojure configuration map that controls selected compiler behaviors.

The dynamic variable *compiler-options* holds a configuration map for selected Clojure compiler behaviors. Bind it around compilation or evaluation, or supply equivalent -Dclojure.compiler.* command-line properties. section 23.8

The supported options control memory behavior, emitted metadata, and Var dispatch:

  • :disable-locals-clearing prevents the compiler from nulling selected local attributes after use, which assists debugging but normally favors production use. section 23.8
  • :elide-meta accepts metadata keywords such as :doc or :file and omits them from emitted Java classes, reducing executable size and startup time. section 23.8
  • :direct-linking enables calls that bypass the indirection introduced by dereferencing Vars. section 23.8

Inspecting local clearing

Bind {:disable-locals-clearing true} while compiling a form, then decompile the generated classes. With the option enabled, captured local s remains assigned instead of being set to null after use. section 23.8

(binding [*compile-files* true
          *compile-path* "."
          *compiler-options* {:disable-locals-clearing true}] ; #1
  (eval '(let [s "potentially-huge"] (^:once fn* [] s))))

Compile a closure with local clearing disabled, then compare its generated Java with the default output. section 23.8

import clojure.lang.AFunction;

//
// Decompiled by Procyon v0.5.36
//

public final class user$eval35 extends AFunction
{
    public static final Object const__1;
    public static final Object const__2;

    public static Object invokeStatic() {
        return user$plus    #1
          .invokeStatic(user$eval35.const__1,
                        user$eval35.const__2);
    }

    public Object invoke() {
        return invokeStatic();
    }

    static {
        const__1 = 1L;
        const__2 = 2L;
    }
}

Compare direct linking with ordinary Var-based invocation in decompiled output. section 23.8

How Clojure: The Essential Reference covers it

How deep it goes
Defines three options and demonstrates generated-class effects, stopping short of broader compiler internals.
Examples
Uses closure compilation, Java decompilation, metadata elision, and direct-versus-indirect function invocation.
What it assumes
Assumes familiarity with dynamic variables and compilation, while introducing option effects through executable forms and decompiled classes.
What you can do after
Operate/configure compilation with bindings or -D properties and inspect resulting Java classes.
How it connects
Connects dynamic variables with metadata, Java classes, local-variable clearing, and Var dereferencing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.