改善 Scala 脚本启动时间——客户端模式?

发布于 2024-10-03 10:13:33 字数 330 浏览 2 评论 0 原文

我希望简短的 Scala 脚本能够像 Python 脚本一样快速运行,特别是在脚本启动时间方面。

任何人都可以推荐一些不涉及 GCJ 编译的方法吗?

我能想到的一种方法是使用 JVM 的客户端模式运行脚本,但我似乎无法让它工作。一个(已知良好的)shebang 的例子就太好了。

更新:我知道其他问题,但认为到目前为止还没有找到任何可行的答案,因为我正在寻找适用于标准安装的解决方案,而无需额外的要求。这就是我试图通过“例如,不涉及使用 GCJ 进行编译”来达到的目的。

看起来 -client 模式就是为了这个明确的目的而设计的,但由于某种原因从 scala 脚本激活起来很尴尬。

I'd like to get short Scala scripts running as fast as python scripts do, particularly in terms of script startup time.

Can anyone recommend some ways of doing this, that doesn't involve compiling with GCJ, for instance?

One way I can think of is to run the script using the JVM's client mode, but I can't seem to get this working. An example (known-good) shebang for this would be great.

UPDATE: I'm aware of the other questions, but don't think any workable answers have been found so far, as I'm looking for solutions that work on STANDARD installs, without additional requirements. This is what I was trying to get at with "doesn't involve compiling with GCJ, for instance".

It seems that -client mode is designed for this express purpose, but it's just awkward to activate from scala scripts for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七秒鱼° 2024-10-10 10:13:33

作为很多 其他 问题之前已经出现过,如果只能知道如何查找它们,请使用 钉枪

提高脚本性能的其他方法是在系统启动时启动fsc,以便脚本可用,并使用-savecompiled,以避免重复编译脚本。

编辑

您提到-client模式,但我认为这确实不是一个好的选择。这会给你一个更慢的 Scala 编译器,并且对改善编译器本身的启动时间几乎没有什么作用,如果不是 Java 的话。最好将 fsc 作为守护进程,作为 -server 运行,和/或使用 -savecompiled 保存已编译的脚本。

现在,我不知道您在使用 -client 时遇到什么问题,但我了解到它不适用于 64 位 JVM。这可能是你的情况吗?

PS:查找类似问题,我注意到JRuby有内置的Nailgun支持!

As many other questions have gone before, if one could only know how to look for them, use Nailgun.

Other ways to improve on script performance is to start fsc at system boot, so it will be available for scripts, and use -savecompiled, to avoid repeated compilation of scripts.

EDIT

You mention -client mode, but I think that's really not a good choice. The will give you a slower Scala compiler, and do little to improve the startup time of the compiler itself, if not Java. Much better to have fsc as daemon, running as -server, and/or save compiled scripts with -savecompiled.

Now, I don't know what problems you are having with -client, but I have read that it doesn't work with 64 bits JVM. Might that be your case?

PS: Looking up similar questions, I noticed JRuby has builtin Nailgun support!

晌融 2024-10-10 10:13:33

我还没有尝试过这个,但是 scala-native 承诺近乎即时启动,因为它编译成本机二进制文件。因此,一种解决方案是将其作为多个二进制下载提供。

http://www.scala-native.org/en/latest/

I haven't tried this yet, but scala-native promises near-instant startup because it compiles into a native binary. So one solution is to provide it as a number of binary downloads.

http://www.scala-native.org/en/latest/

你是年少的欢喜 2024-10-10 10:13:33

我只是尝试以这种方式通过 Scala 将“-client”参数传递给 JVM:

#!/bin/sh
exec scala -J-client "$0" "$@"
!#
args.foreach(println)

它似乎有效。 Daniel C. Sobral 写道,他读到它不适用于 64 位 JVM。我不知道,也许这已经过时了。无论如何,它似乎稍微缩短了启动时间。

运行:

:~$ time /tmp/testScalasScript arg1
arg1

real    0m2,170s
user    0m2,228s
sys     0m0,217s

这是几次测试中最快的运行。如果没有它,最多需要 0.5 秒的时间。但这确实是一个快速测试,应该更系统地进行才能得出有意义的结果。

难道没有办法让Scala在第一次运行脚本时就编译并保存编译结果以便更快地重用吗?但目前我还不确定。

更新:
我刚刚看到在“java -help”上选项“-client”没有记录(不再?)。无论如何,没有抛出错误(这是在使用不存在的选项时完成的)。所以我不确定“-client”选项是否真的有影响。

I just tried to pass the '-client' parameter through Scala to the JVM this way:

#!/bin/sh
exec scala -J-client "$0" "$@"
!#
args.foreach(println)

It seems to work. Daniel C. Sobral wrote that he read that it doesn't work with 64 bits JVM. I don't know, maybe this is outdated. Anyway it seem to drop the startup time a little bit.

Running:

:~$ time /tmp/testScalasScript arg1
arg1

real    0m2,170s
user    0m2,228s
sys     0m0,217s

This was the fastest run of just a couple of tests. Without it it takes up to 0.5s longer. But this was really a quick test and should be done more systematically to come to meaningful results.

Wasn't there a way to make Scala compile and save the compilation result at the first run of the script for faster reuse? But I don't no for sure at this point of time.

UPDATE:
I just saw that on 'java -help' the option '-client' is not documented (anymore?). Anyway there is no error thrown (which is done on usage of unexisting options). So I am not sure if the '-client' option really has consequences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文