Clojure应用程序启动性能 [英] Clojure application startup performance

查看:96
本文介绍了Clojure应用程序启动性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure中编写了一些小的实用程序应用程序,我使用Maven和maven-shade-plugin编译成自包含的可执行JAR文件(uberjars)。这些uberjars包含clojure.jar的解包版本和应用程序依赖的其他库(即:commons-cli)。它们很方便,因为我可以将它们发送给客户,而不需要客户安装Clojure(所有客户已经安装了JRE)。



我发现Clojure应用程序花费几秒钟来启动,而用Java编写的类似应用程序在同一台机器上以亚秒为单位启动(例如,显示使用消息的时间)。



I怀疑这是因为Clojure是on-the-fly编译clojure.core库中的一些代码,因为clojure中有源代码( .clj 文件)。 jar文件。



有没有办法预编译这个源代码?还有什么可以做到加快启动性能吗?我听说客户抱怨启动需要多长时间(他们不知道或关心应用程序是用Clojure或Java或Foobar编写的)。

解决方案

JVM(至少是Oracle的HotSpot)减少启动时间是非常棘手的事情。它不会加载到内存所有程序的类和方法,它只会加载它现在需要的资源。没有这么多代码需要显示使用消息或类似的东西,所以只有很少的功能实际加载和Java程序快速启动。此外,HotSpot 甚至不编译这些几个函数 - 它使用JIT编译(和其他优化)的代码,它是重复执行。没有理由花时间编译只执行一次的函数,例如。几乎所有的启动方法,和HotSpot不。



那么,Clojure呢?我不认为你想重写Clojure的核心来添加类似的功能。不过,你可以在Clojure代码中使用相同的方法。你说你的实用程序使用几个库,这可能会减缓启动。因此,延迟加载库尽可能多。例如,您可以从命名空间定义中排除:use 选项,而在主函数中调用显式 use 。这不会减少总时间,但它会移动dalay 到那一刻,当它不那么明显。你甚至可以在Java中编写程序的一小部分,并且只有在实际需要时才调用Clojure代码。


I have written a few small utility applications in Clojure that I compile into self-contained executable JAR files ("uberjars") using Maven and the maven-shade-plugin. These uberjars contain unpacked versions of clojure.jar and other libraries (i.e.: commons-cli) that the application depends on. They are convenient because I can send them to a customer without requiring that the customer install Clojure (all customers already have the JRE installed).

I have found that the Clojure applications take several seconds to start up, whereas similar applications written in Java start in sub-seconds on the same machines (time to show a usage message, for example).

I suspect that it is because Clojure is on-the-fly compiling some of the code in the clojure.core library as there is source code (.clj files) in the clojure.jar file.

Is there any way to precompile this source code? Can anything else be done to speed up startup performance? I have heard complaints from the customers about how long the startup takes (and they don't know or care that the application is written in Clojure or Java or Foobar).

解决方案

JVM (at least Oracle's HotSpot) makes very tricky thing to reduce startup time. It doesn't load to memory all program's classes and methods, it loads only resources it needs right now. There are not so many code needed to show a usage message or something like that, so only few functions are actually loaded and Java program gets started quickly. Moreover, HotSpot doesn't even compile these few functions - it uses JIT compilation (and other optimization) for the code, which is executed repeatedly. There's no reason to spend time to compile functions that will be executed only once, e.g. almost all startup methods, and HotSpot doesn't.

So, what about Clojure? I don't think you would like to rewrite Clojure's core to add similar functionality. Nevertheless, you can use same approach inside of your Clojure code. You said your utilities use several libraries, that can slow down startup. So, load libraries lazily as much as you can. For example, you can exclude :use option from your namespace definition and call explicit use in your principal functions instead. This won't reduce total time, but it will shift dalay to the moment, when it isn't so appreciable. You can even write small part of your program in Java and call Clojure code only when it is actually needed.

这篇关于Clojure应用程序启动性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆