<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged direct-linking</title>
<link>https://ask.clojure.org/index.php/tag/direct-linking</link>
<description></description>
<item>
<title>Can direct-linking be enabled for my own code and not for libraries?</title>
<link>https://ask.clojure.org/index.php/13757/can-direct-linking-enabled-for-own-code-and-not-for-libraries</link>
<description>&lt;p&gt;Blanketly enablind direct-linking can break libraries, and I can't control what vars in them are affected, but I'd still like to improve performance at least in parts of my own code.&lt;/p&gt;
&lt;p&gt;Would the following work?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(binding [clojure.core/*compiler-options* 
  {:direct-linking true}]
  ,,,
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here's the docs: &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/*compiler-options*&quot;&gt;https://clojuredocs.org/clojure.core/*compiler-options*&lt;/a&gt;&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13757/can-direct-linking-enabled-for-own-code-and-not-for-libraries</guid>
<pubDate>Tue, 27 Feb 2024 12:47:39 +0000</pubDate>
</item>
<item>
<title>Finer-grained control over direct-linking</title>
<link>https://ask.clojure.org/index.php/12488/finer-grained-control-over-direct-linking</link>
<description>&lt;p&gt;Currently, the approach to enabling direct linking (DL) is mostly all-of-nothing with pinpoint opt-out (&lt;code&gt;^:redef&lt;/code&gt;). In my experience, it makes using DL somewhat awkward in production cases where you still want to retain the ability to jack into the application for debugging and investigation purposes. Knowing upfront which functions you would need to recompile is cumbersome and mentally taxing. In fact, I end up not enabling DL at all because of it.&lt;/p&gt;
&lt;p&gt;I wonder if having a more flexible way to tune DL would make it more popular. Here are my thoughts pertaining to this.&lt;/p&gt;
&lt;p&gt;Currently, the decision whether to DL is made at the callsite depending on the value of &lt;code&gt;*compiler-options*&lt;/code&gt; at the moment of compiling that callsite. The alternative I envision is when the decision to DL is made at the function definition time, and all the future invocations of that function honor that decision.&lt;/p&gt;
&lt;p&gt;Let me give you an example. Clojure itself compiles &lt;code&gt;clojure.core&lt;/code&gt; namespace with DL enabled. This means that functions within &lt;code&gt;clojure.core&lt;/code&gt; statically link to other &lt;code&gt;clojure.core&lt;/code&gt; functions. However, if the developer has DL disabled, then the calls to &lt;code&gt;clojure.core&lt;/code&gt; functions would go through Var resolution. I personally can't envision a scenario where this precise behavior is desired. Would someone want to redefine a &lt;code&gt;clojure.core&lt;/code&gt; function for the code they run while being content with an old version still being used by some other code? Is this something that the language design wants to encourage?&lt;/p&gt;
&lt;p&gt;My second point is that a blanket DL usually brings quite unsubstantial performance improvements except when used for specific functions called in tight loops that are amenable to JIT cascade optimizations due to inlining. Such performance-critical functions can be reliably identified by the developer with a help of a profiler, and then it makes sense to only enable DL for those functions. As of now, the only way to make a function that is guaranteed to be JIT-inlinable without going buying into DL wholesale is to use the semi-obscure &lt;code&gt;:inline&lt;/code&gt; function which is just a macro, after all.&lt;/p&gt;
&lt;p&gt;On the implementation side, this could be implemented by a combination of a special value of &lt;code&gt;*compiler-options* :direct-linking&lt;/code&gt; (kinda similar to how &lt;code&gt;*unchecked-math*&lt;/code&gt; can just be &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;:warn-on-boxed&lt;/code&gt;) and a metadata on Vars (could reuse the currently noop &lt;code&gt;^:static&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;TL;DR: I'd like a mode of DL when all callsites of a function compiled with DL to be directly linked automatically, and a way to explicitly mark a function to always use DL. Does anybody else find something like this useful? Would love to hear what you think.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12488/finer-grained-control-over-direct-linking</guid>
<pubDate>Sun, 18 Dec 2022 19:44:03 +0000</pubDate>
</item>
<item>
<title>Are protocol methods guaranteed to not be directly linked?</title>
<link>https://ask.clojure.org/index.php/10967/are-protocol-methods-guaranteed-to-not-be-directly-linked</link>
<description>&lt;p&gt;As shown in the following example, the protocol method, &lt;code&gt;pfoo&lt;/code&gt; doesn't appear to be directly linked.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clj -J-Dclojure.compiler.direct-linking=true
Clojure 1.10.1
user=&amp;gt; (defprotocol Foo (pfoo [this]))
Foo
user=&amp;gt; (defn libfn [x] (pfoo x))
#'user/libfn
user=&amp;gt; (extend-protocol Foo String (pfoo [_] :dude))
nil
user=&amp;gt; (libfn &quot;hello&quot;)
:dude
user=&amp;gt; (alter-var-root #'user/pfoo (fn [old] (fn [this] (prn :this) (old this))))
#object[user$eval178$fn__179$fn__180 0x18eec010 &quot;user$eval178$fn__179$fn__180@18eec010&quot;]
user=&amp;gt; (libfn &quot;hello&quot;)
:this
:dude
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The reason I ask is because I'm trying to find a generic way to patch protocol methods to work within sci + graal native-image. The constraints are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;works under direct-linking (important for native-image use)&lt;/li&gt;
&lt;li&gt;use with 3rd party libraries&lt;ul&gt;
&lt;li&gt;avoid source code changes to target protocols (can't add ^:dynamic, ^:redef meta)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have a solution that works &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/phronmophobic/mobiletest/blob/main/src/com/phronemophobic/mobiletest/scify.clj#L6&quot;&gt;here&lt;/a&gt;, but it relies on the fact that protocol vars can be altered using alter-var-root at compile time even under direct linking. Is that a safe assumption?&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10967/are-protocol-methods-guaranteed-to-not-be-directly-linked</guid>
<pubDate>Mon, 23 Aug 2021 23:20:02 +0000</pubDate>
</item>
</channel>
</rss>