Direct linking is a Clojure compilation mode that removes var indirection by embedding direct calls to compiled function implementations. Enable it through the dynamic *compiler-options* map with :direct-linking true. section 23.8
The option changes the shape of compiled calls:
- With :direct-linking false, compiled code stores a Var and invokes its raw root through IFn. section 23.8
- With :direct-linking true, compiled code calls the generated function class’s static invokeStatic method directly. section 23.8
- Pass the same setting from the command line with -Dclojure.compiler.direct-linking=true. section 23.8
Comparing compiled calls
Compile (defn plus [a b] (+ a b)) followed by (plus 1 2) twice. Disabling direct linking produces a Var lookup and IFn invocation; enabling it produces a direct call to user$plus.invokeStatic with the same arguments. section 23.8
import clojure.lang.RT;
import clojure.lang.IFn;
import clojure.lang.Var;
import clojure.lang.AFunction;
public final class user$eval30 extends AFunction
{
public static final Var const__0;
public static final Object const__1;
public static final Object const__2;
static {
const__0 = RT.var("user", "plus"); #1
const__1 = 1L;
const__2 = 2L;
}
public static Object invokeStatic() {
return ((IFn)user$eval30.const__0.getRawRoot()) #2
.invoke(user$eval30.const__1,
user$eval30.const__2);
}
public Object invoke() {
return invokeStatic();
}
}
Inspect the decompiled indirect call, where a Var resolves the function’s raw root before invocation. 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;
}
}
Inspect the decompiled directly linked call, which invokes the generated function class without Var dereferencing. section 23.8
