├── LICENSE ├── README.md ├── build.boot ├── src └── lazy_require │ └── core.clj └── test └── lazy_require └── core_test.clj /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF 5 | THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and 12 | documentation distributed under this Agreement, and 13 | 14 | b) in the case of each subsequent Contributor: 15 | 16 | i) changes to the Program, and 17 | 18 | ii) additions to the Program; 19 | 20 | where such changes and/or additions to the Program originate from and 21 | are distributed by that particular Contributor. A Contribution 22 | 'originates' from a Contributor if it was added to the Program by such 23 | Contributor itself or anyone acting on such Contributor's 24 | behalf. Contributions do not include additions to the Program which: 25 | (i) are separate modules of software distributed in conjunction with 26 | the Program under their own license agreement, and (ii) are not 27 | derivative works of the Program. 28 | 29 | "Contributor" means any person or entity that distributes the Program. 30 | 31 | "Licensed Patents" mean patent claims licensable by a Contributor 32 | which are necessarily infringed by the use or sale of its Contribution 33 | alone or when combined with the Program. 34 | 35 | "Program" means the Contributions distributed in accordance with this 36 | Agreement. 37 | 38 | "Recipient" means anyone who receives the Program under this 39 | Agreement, including all Contributors. 40 | 41 | 2. GRANT OF RIGHTS 42 | 43 | a) Subject to the terms of this Agreement, each Contributor hereby 44 | grants Recipient a non-exclusive, worldwide, royalty-free copyright 45 | license to reproduce, prepare derivative works of, publicly display, 46 | publicly perform, distribute and sublicense the Contribution of such 47 | Contributor, if any, and such derivative works, in source code and 48 | object code form. 49 | 50 | b) Subject to the terms of this Agreement, each Contributor hereby 51 | grants Recipient a non-exclusive, worldwide, royalty-free patent 52 | license under Licensed Patents to make, use, sell, offer to sell, 53 | import and otherwise transfer the Contribution of such Contributor, if 54 | any, in source code and object code form. This patent license shall 55 | apply to the combination of the Contribution and the Program if, at 56 | the time the Contribution is added by the Contributor, such addition 57 | of the Contribution causes such combination to be covered by the 58 | Licensed Patents. The patent license shall not apply to any other 59 | combinations which include the Contribution. No hardware per se is 60 | licensed hereunder. 61 | 62 | c) Recipient understands that although each Contributor grants the 63 | licenses to its Contributions set forth herein, no assurances are 64 | provided by any Contributor that the Program does not infringe the 65 | patent or other intellectual property rights of any other entity. Each 66 | Contributor disclaims any liability to Recipient for claims brought by 67 | any other entity based on infringement of intellectual property rights 68 | or otherwise. As a condition to exercising the rights and licenses 69 | granted hereunder, each Recipient hereby assumes sole responsibility 70 | to secure any other intellectual property rights needed, if any. For 71 | example, if a third party patent license is required to allow 72 | Recipient to distribute the Program, it is Recipient's responsibility 73 | to acquire that license before distributing the Program. 74 | 75 | d) Each Contributor represents that to its knowledge it has sufficient 76 | copyright rights in its Contribution, if any, to grant the copyright 77 | license set forth in this Agreement. 78 | 79 | 3. REQUIREMENTS 80 | 81 | A Contributor may choose to distribute the Program in object code form 82 | under its own license agreement, provided that: 83 | 84 | a) it complies with the terms and conditions of this Agreement; and 85 | 86 | b) its license agreement: 87 | 88 | i) effectively disclaims on behalf of all Contributors all warranties 89 | and conditions, express and implied, including warranties or 90 | conditions of title and non-infringement, and implied warranties or 91 | conditions of merchantability and fitness for a particular purpose; 92 | 93 | ii) effectively excludes on behalf of all Contributors all liability 94 | for damages, including direct, indirect, special, incidental and 95 | consequential damages, such as lost profits; 96 | 97 | iii) states that any provisions which differ from this Agreement are 98 | offered by that Contributor alone and not by any other party; and 99 | 100 | iv) states that source code for the Program is available from such 101 | Contributor, and informs licensees how to obtain it in a reasonable 102 | manner on or through a medium customarily used for software exchange. 103 | 104 | When the Program is made available in source code form: 105 | 106 | a) it must be made available under this Agreement; and 107 | 108 | b) a copy of this Agreement must be included with each copy of the Program. 109 | 110 | Contributors may not remove or alter any copyright notices contained 111 | within the Program. 112 | 113 | Each Contributor must identify itself as the originator of its 114 | Contribution, if any, in a manner that reasonably allows subsequent 115 | Recipients to identify the originator of the Contribution. 116 | 117 | 4. COMMERCIAL DISTRIBUTION 118 | 119 | Commercial distributors of software may accept certain 120 | responsibilities with respect to end users, business partners and the 121 | like. While this license is intended to facilitate the commercial use 122 | of the Program, the Contributor who includes the Program in a 123 | commercial product offering should do so in a manner which does not 124 | create potential liability for other Contributors. Therefore, if a 125 | Contributor includes the Program in a commercial product offering, 126 | such Contributor ("Commercial Contributor") hereby agrees to defend 127 | and indemnify every other Contributor ("Indemnified Contributor") 128 | against any losses, damages and costs (collectively "Losses") arising 129 | from claims, lawsuits and other legal actions brought by a third party 130 | against the Indemnified Contributor to the extent caused by the acts 131 | or omissions of such Commercial Contributor in connection with its 132 | distribution of the Program in a commercial product offering. The 133 | obligations in this section do not apply to any claims or Losses 134 | relating to any actual or alleged intellectual property 135 | infringement. In order to qualify, an Indemnified Contributor must: a) 136 | promptly notify the Commercial Contributor in writing of such claim, 137 | and b) allow the Commercial Contributor tocontrol, and cooperate with 138 | the Commercial Contributor in, the defense and any related settlement 139 | negotiations. The Indemnified Contributor may participate in any such 140 | claim at its own expense. 141 | 142 | For example, a Contributor might include the Program in a commercial 143 | product offering, Product X. That Contributor is then a Commercial 144 | Contributor. If that Commercial Contributor then makes performance 145 | claims, or offers warranties related to Product X, those performance 146 | claims and warranties are such Commercial Contributor's responsibility 147 | alone. Under this section, the Commercial Contributor would have to 148 | defend claims against the other Contributors related to those 149 | performance claims and warranties, and if a court requires any other 150 | Contributor to pay any damages as a result, the Commercial Contributor 151 | must pay those damages. 152 | 153 | 5. NO WARRANTY 154 | 155 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 156 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 157 | KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY 158 | WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 159 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 160 | responsible for determining the appropriateness of using and 161 | distributing the Program and assumes all risks associated with its 162 | exercise of rights under this Agreement , including but not limited to 163 | the risks and costs of program errors, compliance with applicable 164 | laws, damage to or loss of data, programs or equipment, and 165 | unavailability or interruption of operations. 166 | 167 | 6. DISCLAIMER OF LIABILITY 168 | 169 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR 170 | ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 171 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 172 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 173 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 174 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 175 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 176 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 177 | 178 | 7. GENERAL 179 | 180 | If any provision of this Agreement is invalid or unenforceable under 181 | applicable law, it shall not affect the validity or enforceability of 182 | the remainder of the terms of this Agreement, and without further 183 | action by the parties hereto, such provision shall be reformed to the 184 | minimum extent necessary to make such provision valid and enforceable. 185 | 186 | If Recipient institutes patent litigation against any entity 187 | (including a cross-claim or counterclaim in a lawsuit) alleging that 188 | the Program itself (excluding combinations of the Program with other 189 | software or hardware) infringes such Recipient's patent(s), then such 190 | Recipient's rights granted under Section 2(b) shall terminate as of 191 | the date such litigation is filed. 192 | 193 | All Recipient's rights under this Agreement shall terminate if it 194 | fails to comply with any of the material terms or conditions of this 195 | Agreement and does not cure such failure in a reasonable period of 196 | time after becoming aware of such noncompliance. If all Recipient's 197 | rights under this Agreement terminate, Recipient agrees to cease use 198 | and distribution of the Program as soon as reasonably 199 | practicable. However, Recipient's obligations under this Agreement and 200 | any licenses granted by Recipient relating to the Program shall 201 | continue and survive. 202 | 203 | Everyone is permitted to copy and distribute copies of this Agreement, 204 | but in order to avoid inconsistency the Agreement is copyrighted and 205 | may only be modified in the following manner. The Agreement Steward 206 | reserves the right to publish new versions (including revisions) of 207 | this Agreement from time to time. No one other than the Agreement 208 | Steward has the right to modify this Agreement. The Eclipse Foundation 209 | is the initial Agreement Steward. The Eclipse Foundation may assign 210 | the responsibility to serve as the Agreement Steward to a suitable 211 | separate entity. Each new version of the Agreement will be given a 212 | distinguishing version number. The Program (including Contributions) 213 | may always be distributed subject to the version of the Agreement 214 | under which it was received. In addition, after a new version of the 215 | Agreement is published, Contributor may elect to distribute the 216 | Program (including its Contributions) under the new version. Except as 217 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives 218 | no rights or licenses to the intellectual property of any Contributor 219 | under this Agreement, whether expressly, by implication, estoppel or 220 | otherwise. All rights in the Program not expressly granted under this 221 | Agreement are reserved. 222 | 223 | This Agreement is governed by the laws of the State of Washington and 224 | the intellectual property laws of the United States of America. No 225 | party to this Agreement will bring a legal action under this Agreement 226 | more than one year after the cause of action arose. Each party waives 227 | its rights to a jury trial in any resulting litigation. 228 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lazy-require 2 | 3 | lazy-require is a minimalistic library that allows you to postpone loading 4 | namespaces until the code which needs them runs for the first time. This allows 5 | to reduce the perceived startup time of the application. 6 | 7 | ## Why? 8 | 9 | By design, Clojure is eager about loading namespaces. It certainly doesn't load 10 | every namespaces on the classpath — that would be wasteful. Instead, Clojure 11 | walks the dependency tree of namespaces as it loads them, starting with the app 12 | entrypoint (the namespace with `-main`) and eagerly loads all namespaces it 13 | reaches via `require` links. 14 | 15 | Sometimes, however, your application might have parts that would never run, and 16 | those parts depend on namespaces that never had to be loaded. This could happen, 17 | for example, if the program can be started with different configuration that 18 | will determine which code is run. Imagine an encryption program that can either 19 | encrypt or decrypt data based on a launch flag, and those two functions require 20 | disparate sets of namespaces to be loaded. 21 | 22 | Another case is when some functionality is not needed immediately when the 23 | program starts, but may become necessary after some external event — user input 24 | or a network call. In that case, the namespaces still have to be loaded 25 | eventually, but postponing their loading may reduce the initial time the user 26 | has to wait. 27 | 28 | ## How does it work? 29 | 30 | First, you'll need to add `com.clojure-goes-fast/lazy-require` to dependencies: 31 | 32 | [![](https://clojars.org/com.clojure-goes-fast/lazy-require/latest-version.svg)](https://clojars.org/com.clojure-goes-fast/lazy-require) 33 | 34 | The main macro to use is called `with-lazy-require`: 35 | 36 | ```clojure 37 | (require '[lazy-require.core :as lreq]) 38 | 39 | (defn some-function [args] 40 | ... 41 | (lreq/with-lazy-require [clojure.string 42 | [clojure.set :as set]] 43 | (set/union #{1 2} #{3 (clojure.string/join ["a" "b"])})) 44 | ...) 45 | ``` 46 | 47 | First argument to `with-lazy-require` is a vector of `require` forms. They can 48 | be either symbols or vectors like `[name.space :as alias]` — these are the only 49 | two variants that lazy-require supports. The rest of the arguments is the body 50 | that will be executed once the declared namespaces are loaded. Notice how you 51 | can use both full namespace names and aliases in the body. 52 | 53 | Of course, using `with-lazy-require` means you can remove those namespaces from 54 | `:require` in the `ns` definition — otherwise Clojure will load them immediately 55 | and the point of lazy-require is lost. 56 | 57 | If you want to postpone loading some namespaces, but don't want the user to 58 | experience the delay later when the loading actually happens, you can call 59 | `(load-in-background)` at some point during program initialization. This will 60 | start loading all of the namespaces postponed by `with-lazy-require` in a 61 | background thread. Chances are, by the time the user will need the functionality 62 | provided by a postponed namespace, it will already be loaded. 63 | 64 | ## Warnings and gotchas 65 | 66 | Using this library slightly circumvents the regular Clojure loading process. 67 | For example, when doing AOT, Clojure will not infer that it has to compile the 68 | postponed namespaces — you'll have to mention them explicitly. 69 | 70 | Another problem might happen if you mix lazy-require with some other code that 71 | does dynamic namespace loading. lazy-require itself makes sure to serialize the 72 | loading of namespaces so that nothing breaks because of concurrent loading 73 | (e.g., see 74 | [clojure-emacs/cider#2092](https://github.com/clojure-emacs/cider/issues/2092)). 75 | However, if other code also loads namespaces in the background, something might 76 | happen. If you ever experience such issues, please file a ticket. 77 | 78 | ## License 79 | 80 | lazy-require is distributed under the Eclipse Public License. See 81 | [LICENSE](LICENSE). 82 | 83 | Copyright 2018 Alexander Yakushev 84 | -------------------------------------------------------------------------------- /build.boot: -------------------------------------------------------------------------------- 1 | (task-options! 2 | pom {:project 'com.clojure-goes-fast/lazy-require 3 | :version "0.1.1" 4 | :description "Clojure library for deferred namespace loading." 5 | :url "https://github.com/clojure-goes-fast/lazy-require" 6 | :scm {:url "https://github.com/clojure-goes-fast/lazy-require"} 7 | :license {"Eclipse Public License" 8 | "http://www.eclipse.org/legal/epl-v10.html"}} 9 | push {:repo "clojars"}) 10 | 11 | (set-env! :dependencies '[[org.clojure/clojure "1.9.0" :scope "provided"]] 12 | :source-paths #{"src/"} 13 | :test-paths #{"test/"}) 14 | 15 | (ns-unmap 'boot.user 'test) 16 | (deftask test [] 17 | (set-env! :source-paths #(into % (get-env :test-paths)) 18 | :dependencies #(conj % '[adzerk/boot-test "1.2.0"])) 19 | (require 'adzerk.boot-test) 20 | ((resolve 'adzerk.boot-test/test))) 21 | 22 | (deftask build [] 23 | (set-env! :resource-paths (get-env :source-paths)) 24 | (comp (pom) (jar))) 25 | -------------------------------------------------------------------------------- /src/lazy_require/core.clj: -------------------------------------------------------------------------------- 1 | (ns lazy-require.core) 2 | 3 | ;; Private API 4 | 5 | (def ^:private lock (Object.)) 6 | 7 | (def ^:private deferred-nses (atom #{})) 8 | 9 | (defn- postwalk 10 | "Copied from `clojure.walk/walk` to avoid loading the functions we don't need, 11 | since the whole premise of this lib is to shave loading time." 12 | [f form] 13 | (letfn [(walk [form] 14 | (f 15 | (cond 16 | (list? form) (apply list (map walk form)) 17 | (instance? clojure.lang.IMapEntry form) (into [] (map walk) form) 18 | (seq? form) (doall (map walk form)) 19 | (instance? clojure.lang.IRecord form) (into form (map walk) form) 20 | (coll? form) (into (empty form) (map walk) form) 21 | :else form)))] 22 | (walk form))) 23 | 24 | (defn- synchronized-require [require-forms] 25 | (locking lock 26 | (apply require require-forms))) 27 | 28 | ;; Public API 29 | 30 | (defmacro with-lazy-require 31 | "Load namespaces listed in `require-forms` and execute `body` in the context 32 | where those namespaces are already required. `require-forms` is a vector of 33 | require declarations, each can be either a symbol or a vector of the form 34 | `[name.space :as alias]`. Prefixed and nested require declarations, and 35 | `:refer` keyword are not supported. 36 | 37 | Example: 38 | 39 | (with-lazy-require [clojure.string 40 | [clojure.set :as set]] 41 | (set/union #{1 2} #{3 (clojure.string/join [\"a\" \"b\"])}))" 42 | [require-forms & body] 43 | (let [nses (map #(if (symbol? %) % (first %)) require-forms) 44 | aliases (keep #(when (vector? %) (nth % 2)) require-forms) 45 | ns-set (->> (concat nses aliases) 46 | (map str) 47 | set)] 48 | (swap! deferred-nses into nses) 49 | `(do (#'synchronized-require (quote ~require-forms)) 50 | ~@(postwalk (fn [el] 51 | (if (and (symbol? el) (ns-set (namespace el))) 52 | `(resolve (quote ~el)) 53 | el)) 54 | body)))) 55 | 56 | (defn load-in-background 57 | "Start loading all namespaces that were declared in `with-lazy-require` 58 | asynchronously in a background thread." 59 | [] 60 | (doto (Thread. #(synchronized-require @deferred-nses)) 61 | (.setDaemon true) 62 | .start)) 63 | -------------------------------------------------------------------------------- /test/lazy_require/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns lazy-require.core-test 2 | (:require [lazy-require.core :as sut] 3 | [clojure.test :refer :all])) 4 | 5 | (deftest with-lazy-require-test 6 | (is (= #{"ab" 1 2 3} 7 | (sut/with-lazy-require [clojure.string 8 | [clojure.set :as set]] 9 | (set/union #{1 2} #{3 (clojure.string/join ["a" "b"])}))))) 10 | 11 | (deftest load-in-background-test 12 | ;; clojure.core.reducers takes noticable amount of time to load. If we load it 13 | ;; in background, then the later require should be instant. 14 | 15 | ;; First, ensure that it really takes awhile to load. 16 | (let [start (System/currentTimeMillis)] 17 | (require 'clojure.core.reducers) 18 | (is (> (- (System/currentTimeMillis) start) 50) 19 | "Loading c.c.reducers should take some time.")) 20 | 21 | ;; Flush *loaded-libs* so that c.c.reducers is not "loaded" now 22 | (dosync (ref-set @#'clojure.core/*loaded-libs* (sorted-set))) 23 | 24 | (let [start (System/currentTimeMillis)] 25 | ;; Start background loading. The lazy require is done below, but since it 26 | ;; has side effets in macroexpansion time, we already know about it here. 27 | (sut/load-in-background) 28 | (is (< (- (System/currentTimeMillis) start) 5) 29 | "Loading is asynchronous - no time should have passed")) 30 | 31 | (Thread/sleep 1000) ;; To let the background loading complete 32 | 33 | (let [start (System/currentTimeMillis)] 34 | ;; Now, require the namespace and use it. 35 | (sut/with-lazy-require [[clojure.core.reducers :as r]] 36 | (is (= 10 (r/reduce + [1 2 3 4])))) 37 | (is (< (- (System/currentTimeMillis) start) 5) 38 | "c.c.reducers should be already loaded by now - no delay here"))) 39 | --------------------------------------------------------------------------------