什么是 LLVM 以及如何用 LLVM 替换 Python VM 将速度提高 5 倍?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/695370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:40:06  来源:igfitidea点击:

What is LLVM and How is replacing Python VM with LLVM increasing speeds 5x?

pythonmulticorellvmunladen-swallow

提问by Lakshman Prasad

Google is sponsoring an Open Source project to increase the speed of Python by 5x.

Google 正在赞助一个开源项目,以将 Python 的速度提高 5 倍。

Unladen-Swallowseems to have a good project plan

Unladen-Swallow似乎有一个很好的项目计划

Why is concurrency such a hard problem?
Is LLVM going to solve the concurrency problem?
Are there solutions other than Multi-core for Hardware advancement?

为什么并发是一个如此困难的问题?
LLVM 会解决并发问题吗?
除了多核硬件升级之外,还有其他解决方案吗?

回答by Ismael

LLVMis several things together - kind of a virtual machine/optimizing compiler, combined with different frontends that take the input in a particular language and output the result in an intermediate language. This intermediate output can be run with the virtual machine, or can be used to generate a standalone executable.

LLVM是几件事情 - 一种虚拟机/优化编译器,结合不同的前端,这些前端以特定语言输入并以中间语言输出结果。此中间输出可以与虚拟机一起运行,也可以用于生成独立的可执行文件。

The problem with concurrency is that, although it was used for a long time in scientific computing, it has just recently has become common in consumer apps. So while it's widely known how to program a scientific calculation program to achieve great performance, it is completely different thing to write a mail user agent/word processor that can be good at concurrency. Also, most of the current OS's were being designed with a single processor in mind, and they may not be fully prepared for multicore processors.

并发的问题在于,尽管它在科学计算中使用了很长时间,但最近才在消费类应用程序中变得普遍。因此,虽然众所周知如何编写科学计算程序以实现出色的性能,但编写具有良好并发性的邮件用户代理/文字处理器是完全不同的事情。此外,当前的大多数操作系统在设计时都考虑到了单处理器,它们可能没有为多核处理器做好充分准备。

The benefit of LLVM with respect to concurrency is that you have an intermediate output, and if in the future there are advances in concurrency, then by updating your interpreter you instantly gain those benefits in all LLVM-compiled programs. This is not so easy if you had compiled to a standalone executable. So LLVM doesn't solve the concurrency problem per se but it leaves an open door for future enhancements.

LLVM 在并发方面的好处是你有一个中间输出,如果将来在并发方面有进步,那么通过更新你的解释器,你可以立即在所有 LLVM 编译的程序中获得这些好处。如果您编译为独立的可执行文件,这并不容易。所以 LLVM 本身并不能解决并发问题,但它为未来的增强打开了大门。

Sure there are more possible advances for the hardware like quantum computers, genetics computers, etc. But we have to wait for them to become a reality.

当然,量子计算机、遗传学计算机等硬件还有更多可能的进步。但我们必须等待它们成为现实。

回答by DNS

The switch to LLVM itself isn't solving the concurrency problem. That's being solved separately, by getting rid of the Global Interpreter Lock.

切换到 LLVM 本身并不能解决并发问题。这将通过摆脱Global Interpreter Lock单独解决。

I'm not sure how I feel about that; I use threads mainly to deal with blocking I/O, not to take advantage of multicore processors (for that, I would use the multiprocessingmodule to spawn separate processes).

我不确定我对此有何感受;我使用线程主要是为了处理阻塞 I/O,而不是利用多核处理器(为此,我会使用该multiprocessing模块来生成单独的进程)。

So I kind of like the GIL; it makes my life a lot easier not having to think about tricky synchronization issues.

所以我有点喜欢 GIL;它让我的生活变得更轻松,无需考虑棘手的同步问题。

回答by Rhamphoryncus

LLVM takes care of the nitty-gritty of code generation, so it lets them rewrite Psyco in a way that's more general, portable, maintainable. That in turn allows them to rewrite the CPython core, which lets them experiment with alternate GCs and other things needed to improve python's support for concurrency.

LLVM 负责代码生成的细节,因此它允许他们以更通用、可移植和可维护的方式重写 Psyco。这反过来又允许他们重写 CPython 核心,这让他们可以试验备用 GC 和其他需要改进 python 对并发性支持的东西。

In other words, LLVM doesn't solve the concurrency problem, it just frees up your hands so YOU can solve it.

换句话说,LLVM 并没有解决并发问题,它只是解放了您的双手,以便您可以解决它。