Why is leiningen so slow when it starts? - clojure

I'm using lein repl to execute clojure repl in console.
When I run it, it takes over 15 seconds.
When I run java -cp clojure-1.6.0.jar clojure.main, it takes just a few seconds.
Why is lein repl so slow? Are there anyways to make it faster?
My env:
H/W: MacBookAir
O/S: Mac OS 10.9 Mavericks
CPU: i7
MEM: 8GB

Leiningen starts two JVMs, and hooks them together. It's got to load extra stuff to do that. The prompt you type into is a different process from the Clojure process that evaluates your code. Leiningen also has to parse your project file and make sure that everything is set up as it requires, or go and get what's needed from the web if there's anything missing in your maven configuration directory. In the Leiningen sample project file there are some options that can speed up things a little bit, if you read through it carefully. I think that Leiningen having slow startup is just one of the facts of life right now.
More relevant information:
Improving startup time of Clojure REPL with Leiningen on the Raspberry Pi
Faster

If you run lein repl from within a project directory, it will load your project's source files in addition to starting a repl. Even for a small project, this can add significant time if your source files reference external dependencies.
java -cp clojure-1.6.0.jar clojure.main won't load any project source files or dependencies.

There are multiple ways to improve the startup time off lein.
Documented here:
https://github.com/technomancy/leiningen/wiki/Faster

Your first question has been answered, so regarding the second one I guess what you want is to decrease the booting time cause you usually load some namespaces that are being changed as you code. It's possible to reload code from a modified namespace without exiting the REPL with (use 'your.namespace :reload). This way you might boot just once and reload the updated namespaces
user=> (doc require)
...
:reload forces loading of all the identified libs even if they are already loaded
:reload-all implies :reload and also forces loading of all libs that the identified libs directly or indirectly load via require or use
...
In the other hand if you read the output of lein help repl you will see how setup a REPL server and client that might reduce your booting time

In my case it was the cider-nrepl plug-in that significantly contributed to the load time.
Quick unscientifical research with jvisualvm showed that good deal of time goes into loading & evaluating the files (0.10-snapshot is not AOT-ed) and there was also init logic that scans the classpath.
Using fast trampolines halved the start-up time.
The jvm plays no bigger role than the OS or the file system in my opinion. It's all about the code being loaded.

Related

Interactive workflow with external libraries in Clojure [duplicate]

I have multiple, separate leiningen projects that ostensibly could depend on one-another.
Example:
~/projects/mywebapp (my own project)
~/projects/noir (a clone of the github repo)
~/projects/clojureql (a clone of the github repo)
I want to have them all compiled into the same JVM at the same time. I would like to run the git repos bleeding edge (pulling new commits/making my own commits) and not have to run lein jar or lein deps and certainly not have to restart the VM if I change any of the projects.
Here's a use case:
After running lein swank, from within emacs, I connect to the repl and compile a file from mywebapp (with C-c-k), which requires a file from noir. It finds the version of the file in my projects directory. Later, I open that file, edit it, and compile it (with C-c-k).
Note that I'm not asking for auto-compiling when I do git pull. I just don't want to have to restart the JVM or do lengthy jar compiling processes.
Is this possible in leiningen? How can I set this up?
Does this question from the Lein FAQ help?
Q: I want to hack two projects in parallel, but it's annoying to switch between them.
A: Use a feature called checkout dependencies. If you create a directory called checkouts in your project root and symlink some other
project roots into it, Leiningen will allow you to hack on them in
parallel. That means changes in the dependency will be visible in the
main project without having to go through the whole
install/switch-projects/deps/restart-repl cycle. Note that this is not
a replacement for listing the project in :dependencies; it simply
supplements that for tighter change cycles.
If you're already using swank, you don't need lein checkout dependencies. You can just C-c C-k your project (which will load the jarred versions of noir/whatever), and then browse to your local version of noir and C-c C-k that as well. Swank happily sends all the code to your repl, and the jvm never need know that it came from a different place!
I can only recommend this for smallish changes though, because I think if you compile noir.core, which depends on (say) noir.internal, clojure will load the jarred version of noir.internal even while you compile the local version of noir.core. Even so, it's a handy trick in general.

Clojure and lein

Are there some useful ways of triggering complete rebuilds in lein? I'm finding it hard to understand how / when lein reruns/reloads code. Altering files doesn't always seems to result in the changes being applied, and manually requesting recompile for gen-class doesn't always seem to generate files.
Manually deleting files in the target directory doesn't always work, as even if I recompile those files are not always generated so I'll get ClassNotFound errors. How do I have lein properly rebuild everything on each invocation?
edit: most of my confusion was due to not specifying classes created with gen-class in the project.clj file with an :aot directive as suggested in #juan.facorro's comment, e.g.
(defproject
...
:aot [mytestclass.full.packagename.TestClass1 mytestclass.full.packagename.TestClass2 ]
...
)
This sounds like part of a bigger issue: making the clojure development cycle shorter. Once you have your project running happily in the REPL (and this is the first goal) there are several things thats can really drag down the development speed. Changing protocol deffinitions for instance requires you to hunt down and reload every namespace that has instances of that protocol. Stewart Sierra made a great project, gave a presentation and interview about how to set up your project and process so everything that needs reloading is reloaded as quickly as possible.

how to automatically load new dependencies from project.clj in swank

I am working in emacs, swank-clojure. I start my repl using clojure-jack-in. However when I add a new dependency to my project.clj, the dependency is not available in the repl.
1) Is there a way to load new dependencies automatically in swank, when project.clj changes ?
2) Also what is a clean way to terminate the swank server ? Currently I just kill the buffer.
Thanks,
Murtaza
When Java VM starts it reads all the included jars there is straight-forward way to include jars later. If you really don't want to restart application you could try something like this. But it may get very tricky as if you don't use same class loader as Clojure does then when loading another Clojure library you may end up with 2 instances of Clojure core.
To disconnect from swank server use M-x slime-disconnect. To stop publishing on the server you can issue:
(swank.swank/stop-server)

Analogue for maven-resources-plugin or maven-antrun-plugin for leiningen

I use leiningen to manage my clojure project and I want to copy jar file along with some other files into a certain directory as a final part of a build process. Leiningen treats 'resources' as something which should be included into the jar file, and it is unacceptable for me. If I used maven, I could configure it for such task using maven-resource-plugin or fall back to Ant using maven-antrun-plugin, but leiningen is far more convenient tool for clojure projects.
Strangely, I couldn't manage to find anything about similar functionality in leiningen on the internet. This is curious, because one of major clojure applications is web sites, and web sites usually do not include their resources (js, css, etc) into the jar (or do they? That would be weird since slight css tweak will require rather lenghty recompilation). It comes naturally that we have to prepare site environment (copy static resources along with jar bundle into some directory layout), and this task should be done by the build tool.
Is there a plugin to copy files around the filesystem (or something which could substitute it, like running Ant), or I must write one myself? Right now I'm using shell scripts, but it is very inconvenient since I had to run several commands instead of one, and also it is unportable.
did you checkout lein-resource?
in any case. here is a long list of available plugins for lein, maybe you will fine some of them helpful

How can I set up leiningen to work with multiple projects?

I have multiple, separate leiningen projects that ostensibly could depend on one-another.
Example:
~/projects/mywebapp (my own project)
~/projects/noir (a clone of the github repo)
~/projects/clojureql (a clone of the github repo)
I want to have them all compiled into the same JVM at the same time. I would like to run the git repos bleeding edge (pulling new commits/making my own commits) and not have to run lein jar or lein deps and certainly not have to restart the VM if I change any of the projects.
Here's a use case:
After running lein swank, from within emacs, I connect to the repl and compile a file from mywebapp (with C-c-k), which requires a file from noir. It finds the version of the file in my projects directory. Later, I open that file, edit it, and compile it (with C-c-k).
Note that I'm not asking for auto-compiling when I do git pull. I just don't want to have to restart the JVM or do lengthy jar compiling processes.
Is this possible in leiningen? How can I set this up?
Does this question from the Lein FAQ help?
Q: I want to hack two projects in parallel, but it's annoying to switch between them.
A: Use a feature called checkout dependencies. If you create a directory called checkouts in your project root and symlink some other
project roots into it, Leiningen will allow you to hack on them in
parallel. That means changes in the dependency will be visible in the
main project without having to go through the whole
install/switch-projects/deps/restart-repl cycle. Note that this is not
a replacement for listing the project in :dependencies; it simply
supplements that for tighter change cycles.
If you're already using swank, you don't need lein checkout dependencies. You can just C-c C-k your project (which will load the jarred versions of noir/whatever), and then browse to your local version of noir and C-c C-k that as well. Swank happily sends all the code to your repl, and the jvm never need know that it came from a different place!
I can only recommend this for smallish changes though, because I think if you compile noir.core, which depends on (say) noir.internal, clojure will load the jarred version of noir.internal even while you compile the local version of noir.core. Even so, it's a handy trick in general.