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

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

问题描述

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

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).

我发现 Clojure 应用程序需要几秒钟才能启动,而用 Java 编写的类似应用程序在同一台机器上的启动时间不到几秒钟(例如显示使用信息的时间).

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).

我怀疑这是因为 Clojure 正在即时编译 clojure.core 库中的一些代码,因为 clojure.jar 文件中有源代码(.clj 文件).

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.

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

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(至少是 Oracle 的 HotSpot)使减少启动时间变得非常棘手.它不会将所有程序的类和方法加载到内存中,它只加载它现在需要的资源.显示使用信息或类似内容所需的代码并不多,因此实际加载的函数很少,Java 程序可以快速启动.此外,HotSpot甚至不编译这几个函数——它对重复执行的代码使用 JIT 编译(和其他优化).没有理由花时间编译只会执行一次的函数,例如几乎所有的启动方式,HotSpot 没有.

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.

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

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天全站免登陆