Ruby、PHP 或 Perl 是否可以像 Python 一样为代码创建预编译文件?

发布于 2024-07-20 07:07:40 字数 86 浏览 9 评论 0原文

对于Python,它可以创建一个预编译版本file.pyc,以便程序无需再次解释即可运行。 Ruby、PHP 和 Perl 可以在命令行上执行相同的操作吗?

For Python, it can create a pre-compiled version file.pyc so that the program can be run without interpreted again. Can Ruby, PHP, and Perl do the same on the command line?

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

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

发布评论

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

评论(11

白衬杉格子梦 2024-07-27 07:07:40

Ruby 没有可移植的字节码规范,因此也没有加载预编译字节码档案的标准方法。 然而,几乎所有 Ruby 实现都使用某种字节码或 intcode 格式,其中一些可以转储和重新加载字节码存档。

YARV 总是在执行代码之前编译为字节码,但这通常只在内存中完成。 有多种方法可以将字节码转储到磁盘。 目前,无法读回它然而,在中。 未来这种情况将会改变:YARV 字节码验证器的工作正在进行中,一旦完成,字节码就可以安全地加载到虚拟机中,而不必担心损坏。 此外,JRuby 开发人员表示他们愿意实现 JRuby 内部的 YARV VM 模拟器,一旦 YARV 字节码格式和验证器稳定下来,就可以将 YARV 字节码加载到 JRuby 中。 (请注意,此版本已已过时。)

Rubinius 也总是编译为字节码,并且它有一个 已编译文件的格式.rbc 文件,类似于 JVM .class 文件),并且有讨论字节码存档格式(.rba 文件,类似于 JVM .jar 文件)。 如果将应用程序部署为 YARV 字节码变得流行,那么 Rubinius 可能会实现 YARV 模拟器。 此外,JRuby 开发人员表示他们愿意实现 JRuby 内的 Rubinius 字节码模拟器(如果 Rubinius 字节码成为部署 Ruby 应用程序的流行方式)。 (请注意,此版本已已过时。)

XRuby 是一个纯编译器,它将 Ruby 源代码直接编译为 JVM 字节码(.class 文件)。 您可以像任何其他 Java 应用程序一样部署这些 .class 文件。

JRuby 最初是一个解释器,但它同时具有 JIT 编译器和 AOT 编译器 (jrubyc),可以将 Ruby 源代码编译为 JVM 字节码(.class 文件)。 此外,创建编译器的工作正在进行中它可以将(类型注释的)Ruby 代码编译为 JVM 字节码,该字节码实际上看起来像 Java 类,并且可以无障碍地从 Java 代码中使用。

Ruby.NET 是一个纯编译器,可将 Ruby 源代码编译为 CIL 字节码(PE .dll.exe 文件)。 您可以像部署任何其他 CLI 应用程序一样部署它们。

IronRuby 也可以编译为 CIL 字节码,但通常是在内存中进行的。 但是,您可以传递 命令行切换到它,因此它将 .dll.exe 文件转储到磁盘。 一旦有了这些,就可以正常部署它们。

BlueRuby 自动将 Ruby 源代码预解析为 BRIL ( BlueRuby 中间语言),它基本上是一个序列化的解析树。 (请参阅< em>Blue Ruby - SAP ABAP 中的 Ruby VM(PDF)了解详细信息。)

认为(但我绝对不确定)有一种方法可以获取Cardinal 转储 Parrot 字节码档案。 (实际上,Cardinal 只编译为 PAST,然后 Parrot 接管,因此转储和加载字节码档案将是 Parrot 的工作。)

There is no portable bytecode specification for Ruby, and thus also no standard way to load precompiled bytecode archives. However, almost all Ruby implementations use some kind of bytecode or intcode format, and several of them can dump and reload bytecode archives.

YARV always compiles to bytecode before executing the code, however that is usually only done in memory. There are ways to dump out the bytecode to disk. At the moment, there is no way to read it back in, however. This will change in the future: work is underway on a bytecode verifier for YARV, and once that is done, bytecode can safely be loaded into the VM, without fear of corruption. Also, the JRuby developers have indicated that they are willing to implement a YARV VM emulator inside JRuby, once the YARV bytecode format and verifier are stabilized, so that you could load YARV bytecode into JRuby. (Note that this version is obsolete.)

Rubinius also always compiles to bytecode, and it has a format for compiled files (.rbc files, analogous to JVM .class files) and there is talk about a bytecode archive format (.rba files, analogous to JVM .jar files). There is a chance that Rubinius might implement a YARV emulator, if deploying apps as YARV bytecode ever becomes popular. Also, the JRuby developers have indicated that they are willing to implement a Rubinius bytecode emulator inside JRuby, if Rubinius bytecode becomes a popular way of deploying Ruby apps. (Note that this version is obsolete.)

XRuby is a pure compiler, it compiles Ruby sourcecode straight to JVM bytecode (.class files). You can deploy these .class files just like any other Java application.

JRuby started out as an interpreter, but it has both a JIT compiler and an AOT compiler (jrubyc) that can compile Ruby sourcecode to JVM bytecode (.class files). Also, work is underway to create a new compiler that can compile (type-annotated) Ruby code to JVM bytecode that actually looks like a Java class and can be used from Java code without barriers.

Ruby.NET is a pure compiler that compiles Ruby sourcecode to CIL bytecode (PE .dll or .exe files). You can deploy these just like any other CLI application.

IronRuby also compiles to CIL bytecode, but typically does this in-memory. However, you can pass commandline switches to it, so it dumps the .dll and .exe files out to disk. Once you have those, they can be deployed normally.

BlueRuby automatically pre-parses Ruby sourcecode into BRIL (BlueRuby Intermediate Language), which is basically a serialized parsetree. (See Blue Ruby - A Ruby VM in SAP ABAP(PDF) for details.)

I think (but I am definitely not sure) that there is a way to get Cardinal to dump out Parrot bytecode archives. (Actually, Cardinal only compiles to PAST, and then Parrot takes over, so it would be Parrot's job to dump and load bytecode archives.)

套路撩心 2024-07-27 07:07:40

Perl 5 可以将字节码转储到磁盘,但它有缺陷且令人讨厌。 Perl 6 有一个非常干净的方法来创建字节码可执行文件,Parrot 可以运行。

Perl 的即时编译速度足够快,在大多数情况下这并不重要。 它确实重要的一个地方是 CGI 环境,这就是 mod_perl 的用途。

Perl 5 can dump the bytecodes to disk, but it is buggy and nasty. Perl 6 has a very clean method of creating bytecode executables that Parrot can run.

Perl's just-in-time compilation is fast enough that this doesn't matter in most circumstances. One place where it does matter is in a CGI environment which is what mod_perl is for.

很糊涂小朋友 2024-07-27 07:07:40

对于歇斯底里的葡萄干,Perl 5 在搜索模块时会先查找 .pmc 文件,然后再查找 .pm 文件。 这些文件可能包含字节码,尽管 Perl 默认情况下不会写出字节码(与 Python 不同)。

Module::Compile(或者:这个 PMC 是什么东西?)更深入地了解这个晦涩的功能。 它们并不经常使用,但是......

编写 Module::Compile 利用这一点,将 Perl 代码预编译成……好吧,它仍然是 Perl,但它经过了预处理。

除了其他好处之外,这还可以加快加载时间,并使使用源过滤器时的调试变得更加容易(Perl 代码在被解释器加载之前修改 Perl 源代码)。

For hysterical raisins, Perl 5 looks for .pmc files ahead of .pm files when searching for module. These files could contain bytecode, though Perl doesn't write bytecode out by default (unlike Python).

Module::Compile (or: what's this PMC thingy?) goes into some more depth about this obscure feature. They're not frequently used, but...

The clever folks who wrote Module::Compile take advantage of this, to pre-compile Perl code into... well, it's still Perl, but it's preprocessed.

Among other benefits, this speeds up loading time and makes debugging easier when using source filters (Perl code modifying Perl source code before being loaded by the interpreter).

魔法唧唧 2024-07-27 07:07:40

不适用于 PHP,尽管大多数 PHP 设置都包含一个 字节码缓存 来缓存已编译的字节码,以便下次运行脚本时,将运行编译后的版本。 这大大加快了执行速度。

我不知道如何通过命令行实际获取字节码。

Not for PHP, although most PHP setups incorporate a Bytecode Cache that will cache the compiled bytecode so that next time the script runs, the compiled version is run. This speeds up execution considerably.

There's no way I'm aware of to actually get at the bytecode through the command line.

辞取 2024-07-27 07:07:40

对于 Perl,您可以尝试使用 B::Bytecodeperlcc。 然而,这两者都是高度实验性的。 Perl 6 即将推出(理论上),并将在 Parrot 上发布,并将使用不同的字节码,因此所有这些都将变得毫无意义。

For Perl you can try using B::Bytecode and perlcc. However, both of these are highly experimental. And Perl 6 is coming out soon (theoretically) and will be on Parrot and will use a different bytecode and so all of this will be somewhat moot then.

小嗷兮 2024-07-27 07:07:40

以下是一些命令行魔术词示例

perl -MO=Bytecode,-H,-o"Module.pm"c "Module.pm"

here are some example magic words for the command-line

perl -MO=Bytecode,-H,-o"Module.pm"c "Module.pm"
十秒萌定你 2024-07-27 07:07:40

根据Programming Perl第三版,这是可能的通过一些实验方法来近似这一点。

According to the third edition of Programming Perl, it is possible to approximate this in some experimental ways.

泛滥成性 2024-07-27 07:07:40

如果您在 PHP 脚本上使用 Zend Guard,它实际上会将脚本预编译为字节-code,如果加载了 Zend Optimizer 扩展,则可以由 PHP 引擎运行。

所以,是的,Zend Guard/Optimizer 允许使用预编译的 PHP 脚本。

If you use Zend Guard on your PHP scripts, it essentially precompiles the scripts to byte-code, which can then be run by the PHP engine if the Zend Optimizer extension is loaded.

So, yes, Zend Guard/Optimizer permits pre-compiled PHP scripts to be used.

睫毛溺水了 2024-07-27 07:07:40

对于 PHP,Phalanger 项目 编译为 .Net 程序集。 我不确定这是否是您正在寻找的。

For PHP, the Phalanger Project compiles down to .Net assemblies. I'm not sure if thats what you were looking for though.

甜妞爱困 2024-07-27 07:07:40

有没有人考虑过使用 LLVM 的字节码,而不是另一种自定义字节码?

Has anyone considered using LLVM's bytecode, instead of a yet-another-custom-bytecode?

听不够的曲调 2024-07-27 07:07:40

Ruby 1.8 实际上根本不使用字节码(甚至在内部),因此没有预编译步骤。

Ruby 1.8 doesn't actually use bytecode at all (even internally), so there is no pre-compilation step.

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