PyPy:关于什么是嗡嗡声?

时间:2022-04-23 11:13:19

Note: The title is deliberately provocative (to make you click on it and want to close-vote the question) and I don't want to look preoccupied.

注意:标题是故意挑衅的(让你点击它并想要关闭投票的问题),我不想看起来全神贯注。

I've been reading and hearing more and more about PyPy. It's like a linear graph.

我一直在阅读和听到越来越多关于PyPy的内容。这就像一个线性图。

  • Why is PyPy so special? As far as I know implementations of dynamic languages written in the languages itself aren't such a rare thing, or am I not getting something?

    为什么PyPy如此特别?据我所知,用语言编写的动态语言的实现并不是那么罕见,或者我没有得到什么?

  • Some people even call PyPy "the future" [of python], or see some sort of deep potential in this implementation. What exactly is the meaning of this?

    有些人甚至称PyPy为“未来”[python],或者在此实现中看到某种深层潜力。这究竟是什么意思?

5 个解决方案

#1


46  

Good thing to be aware when talking about the PyPy project is that it aims to actually provide two deliverables: first is JIT compiler generator. Yes, generator, meaning that they are implementing a framework for writing implementations of highly dynamic programming languages, such as Python. The second one is the actual test of this framework, and is the PyPy Python interpreter implementation.

在谈论PyPy项目时要注意的好处是它的目的是实际提供两个可交付成果:首先是JIT编译器生成器。是的,生成器,意味着他们正在实现编写高动态编程语言(如Python)的实现的框架。第二个是该框架的实际测试,是PyPy Python解释器实现。

Now, there are multiple answers why PyPy is so special: the project development is running from 2004, started as a research project rather than from a company, reimplements Python in Python, implements a JIT compiler in Python, and can translate RPython (Python code with some limitations for the framework to be able to translate that code to C) to compiled binary.

现在,有多个答案为什么PyPy如此特别:项目开发从2004年开始,作为研究项目开始而不是从公司开始,在Python中重新实现Python,在Python中实现JIT编译器,并且可以翻译RPython(Python代码)有一些限制,框架能够将代码转换为C)到已编译的二进制文件。

The current version of PyPy is 99% compatible with CPython version 2.5, and can run Django, Twisted and many other Python programs. There used to be a limitation of not being able to run existing CPython C extensions, but that is also being addressed with cpyext module in PyPy. C API compatibility is possible and to some extent already implemented. JIT is also very real, see this pystone comparison.

当前版本的PyPy与CPython 2.5版兼容99%,可以运行Django,Twisted和许多其他Python程序。曾经有一个限制,无法运行现有的CPython C扩展,但这也是使用PyPy中的cpyext模块解决的。 C API兼容性是可能的,并且在某种程度上已经实现。 JIT也非常真实,请看这个pystone比较。

With CPython:

使用CPython:

Python 2.5.5 (r255:77872, Apr 21 2010, 08:44:16) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import pystone
>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 12.28
This machine benchmarks at 81433.2 pystones/second

With PyPy:

使用PyPy:

Python 2.5.2 (75632, Jun 28 2010, 14:03:25)
[PyPy 1.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``A radioactive cat has 18
half-lives.''
>>>> from test import pystone
>>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 1.50009
This machine benchmarks at 666625 pystones/second

So you can get a nearly 10x speedup just by using PyPy on some calculations!

因此,只需在某些计算中使用PyPy即可获得近10倍的加速!

So, as PyPy project is slowly maturing and offering some advantages, it is attracting more interest from people trying to address speed issues in their code. An alternative to PyPy is unladden swallow (a Google project) which aims to speed up CPython implementation by using LLVM's JIT capabilities, but progress on unladden swallow was slowed because the developer needed to deal with bugs in LLVM.

因此,随着PyPy项目正在慢慢成熟并提供一些优势,它吸引了更多人们试图解决其代码中的速度问题的兴趣。 PyPy的替代方案是unladden swallow(谷歌项目),旨在通过使用LLVM的JIT功能加速CPython的实现,但由于开发人员需要处理LLVM中的错误,因此unladden swallow的进展放缓。

So, to sum it up, I guess PyPy is regarded the future of Python because it's separating language specification from VM implementation. Features introduced in, eg. stackless Python, could then be implemented in PyPy with very little extra effort, because it's just altering language specification and you keep the shared code the same. Less code, less bugs, less merging, less effort.

总而言之,我认为PyPy被认为是Python的未来,因为它将语言规范与VM实现分开。引入的功能,例如。无堆栈的Python,可以用很少的额外工作在PyPy中实现,因为它只是改变了语言规范,你保持共享代码相同。更少的代码,更少的错误,更少的合并,更少的努力。

And by writing, for example, a new bash shell implementation in RPython, you could get a JIT compiler for free and speed up many linux shell scripts without actually learning any heavy JIT knowledge.

例如,通过编写RPython中的新bash shell实现,您可以免费获得JIT编译器并加速许多Linux shell脚本,而无需实际学习任何繁重的JIT知识。

#2


22  

Lets see it this way... Suppose you want to implement your own dynamic language, and you want to make it fast. You have two options: the hard way and pypy.

让我们这样看......假设你想要实现自己的动态语言,并且想要快速实现它。你有两个选择:艰难的方式和pypy。

The hard way means writing your interpreter in c, and then implement a jit by hand, also in c, by using a mix of very complicated techniques such a method-jit, threaded-jit, tracing jit, polymorphic inline caches, loop invariant motion, etc, etc... Spent several years tuning it up and if you persevere a lot and you don't give up, you may end up with a fast dynamic language implementation.

困难的方式意味着在c中编写解释器,然后在c中使用混合非常复杂的技术,例如方法-jit,线程jit,跟踪jit,多态内联缓存,循环不变运动来实现jit等等......花了几年的时间来调整它,如果你坚持不懈而且不放弃,你最终可能会得到一个快速动态的语言实现。

Or, you can use the pypy framework. That means, writing your interpreter in python instead of c (actually, it would be rpython, a more limited subset of python that can be compiled to c). Once you wrote your interpreter, pypy will automatically generate a jit for free. And you are basically done.

或者,您可以使用pypy框架。这意味着,在python而不是c中编写解释器(实际上,它将是rpython,可以编译为c的更有限的python子集)。一旦你编写了解释器,pypy就会自动生成一个免费的jit。你基本上完成了。

Sounds good, huh?

听起来不错,对吧?

#3


10  

The cool thing about PyPy (aside from being fast and written in RPython (a subset of the Python language) so basically bootstrapped, is that it can provide an automatically created JIT (just in time compiler) for any program you write in PyPy: this makes it ideal to implement, quickly, your own language and have it be rather fast.

关于PyPy的一个很酷的事情(除了快速并用RPython(Python语言的一个子集)编写,所以基本上是自举的,它可以为你在PyPy中编写的任何程序提供一个自动创建的JIT(只是及时编译):这个使其成为快速实施您自己的语言并使其快速实现的理想选择。

Read more here

在这里阅读更多

#4


5  

Not to mention that they just recently exceeded the speed of CPython on some benchmarks. See their blog, I think. I can't reach it from here:

更不用说他们刚刚在某些基准测试中超过了CPython的速度。我想,请看他们的博客。我无法从这里到达它:

http://morepypy.blogspot.com/

http://morepypy.blogspot.com/

#5


4  

Since most of us agree that it's easier to write Python than C, a Python interpreter that's written in Python (well, technically RPython) should be able to be modified much easier and with less bugs than CPython.

由于我们大多数人都同意编写Python比使用C语言更容易,因此使用Python编写的Python解释器(以及技术上的RPython)应该能够比CPython更容易修改并且错误更少。

#1


46  

Good thing to be aware when talking about the PyPy project is that it aims to actually provide two deliverables: first is JIT compiler generator. Yes, generator, meaning that they are implementing a framework for writing implementations of highly dynamic programming languages, such as Python. The second one is the actual test of this framework, and is the PyPy Python interpreter implementation.

在谈论PyPy项目时要注意的好处是它的目的是实际提供两个可交付成果:首先是JIT编译器生成器。是的,生成器,意味着他们正在实现编写高动态编程语言(如Python)的实现的框架。第二个是该框架的实际测试,是PyPy Python解释器实现。

Now, there are multiple answers why PyPy is so special: the project development is running from 2004, started as a research project rather than from a company, reimplements Python in Python, implements a JIT compiler in Python, and can translate RPython (Python code with some limitations for the framework to be able to translate that code to C) to compiled binary.

现在,有多个答案为什么PyPy如此特别:项目开发从2004年开始,作为研究项目开始而不是从公司开始,在Python中重新实现Python,在Python中实现JIT编译器,并且可以翻译RPython(Python代码)有一些限制,框架能够将代码转换为C)到已编译的二进制文件。

The current version of PyPy is 99% compatible with CPython version 2.5, and can run Django, Twisted and many other Python programs. There used to be a limitation of not being able to run existing CPython C extensions, but that is also being addressed with cpyext module in PyPy. C API compatibility is possible and to some extent already implemented. JIT is also very real, see this pystone comparison.

当前版本的PyPy与CPython 2.5版兼容99%,可以运行Django,Twisted和许多其他Python程序。曾经有一个限制,无法运行现有的CPython C扩展,但这也是使用PyPy中的cpyext模块解决的。 C API兼容性是可能的,并且在某种程度上已经实现。 JIT也非常真实,请看这个pystone比较。

With CPython:

使用CPython:

Python 2.5.5 (r255:77872, Apr 21 2010, 08:44:16) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import pystone
>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 12.28
This machine benchmarks at 81433.2 pystones/second

With PyPy:

使用PyPy:

Python 2.5.2 (75632, Jun 28 2010, 14:03:25)
[PyPy 1.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``A radioactive cat has 18
half-lives.''
>>>> from test import pystone
>>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 1.50009
This machine benchmarks at 666625 pystones/second

So you can get a nearly 10x speedup just by using PyPy on some calculations!

因此,只需在某些计算中使用PyPy即可获得近10倍的加速!

So, as PyPy project is slowly maturing and offering some advantages, it is attracting more interest from people trying to address speed issues in their code. An alternative to PyPy is unladden swallow (a Google project) which aims to speed up CPython implementation by using LLVM's JIT capabilities, but progress on unladden swallow was slowed because the developer needed to deal with bugs in LLVM.

因此,随着PyPy项目正在慢慢成熟并提供一些优势,它吸引了更多人们试图解决其代码中的速度问题的兴趣。 PyPy的替代方案是unladden swallow(谷歌项目),旨在通过使用LLVM的JIT功能加速CPython的实现,但由于开发人员需要处理LLVM中的错误,因此unladden swallow的进展放缓。

So, to sum it up, I guess PyPy is regarded the future of Python because it's separating language specification from VM implementation. Features introduced in, eg. stackless Python, could then be implemented in PyPy with very little extra effort, because it's just altering language specification and you keep the shared code the same. Less code, less bugs, less merging, less effort.

总而言之,我认为PyPy被认为是Python的未来,因为它将语言规范与VM实现分开。引入的功能,例如。无堆栈的Python,可以用很少的额外工作在PyPy中实现,因为它只是改变了语言规范,你保持共享代码相同。更少的代码,更少的错误,更少的合并,更少的努力。

And by writing, for example, a new bash shell implementation in RPython, you could get a JIT compiler for free and speed up many linux shell scripts without actually learning any heavy JIT knowledge.

例如,通过编写RPython中的新bash shell实现,您可以免费获得JIT编译器并加速许多Linux shell脚本,而无需实际学习任何繁重的JIT知识。

#2


22  

Lets see it this way... Suppose you want to implement your own dynamic language, and you want to make it fast. You have two options: the hard way and pypy.

让我们这样看......假设你想要实现自己的动态语言,并且想要快速实现它。你有两个选择:艰难的方式和pypy。

The hard way means writing your interpreter in c, and then implement a jit by hand, also in c, by using a mix of very complicated techniques such a method-jit, threaded-jit, tracing jit, polymorphic inline caches, loop invariant motion, etc, etc... Spent several years tuning it up and if you persevere a lot and you don't give up, you may end up with a fast dynamic language implementation.

困难的方式意味着在c中编写解释器,然后在c中使用混合非常复杂的技术,例如方法-jit,线程jit,跟踪jit,多态内联缓存,循环不变运动来实现jit等等......花了几年的时间来调整它,如果你坚持不懈而且不放弃,你最终可能会得到一个快速动态的语言实现。

Or, you can use the pypy framework. That means, writing your interpreter in python instead of c (actually, it would be rpython, a more limited subset of python that can be compiled to c). Once you wrote your interpreter, pypy will automatically generate a jit for free. And you are basically done.

或者,您可以使用pypy框架。这意味着,在python而不是c中编写解释器(实际上,它将是rpython,可以编译为c的更有限的python子集)。一旦你编写了解释器,pypy就会自动生成一个免费的jit。你基本上完成了。

Sounds good, huh?

听起来不错,对吧?

#3


10  

The cool thing about PyPy (aside from being fast and written in RPython (a subset of the Python language) so basically bootstrapped, is that it can provide an automatically created JIT (just in time compiler) for any program you write in PyPy: this makes it ideal to implement, quickly, your own language and have it be rather fast.

关于PyPy的一个很酷的事情(除了快速并用RPython(Python语言的一个子集)编写,所以基本上是自举的,它可以为你在PyPy中编写的任何程序提供一个自动创建的JIT(只是及时编译):这个使其成为快速实施您自己的语言并使其快速实现的理想选择。

Read more here

在这里阅读更多

#4


5  

Not to mention that they just recently exceeded the speed of CPython on some benchmarks. See their blog, I think. I can't reach it from here:

更不用说他们刚刚在某些基准测试中超过了CPython的速度。我想,请看他们的博客。我无法从这里到达它:

http://morepypy.blogspot.com/

http://morepypy.blogspot.com/

#5


4  

Since most of us agree that it's easier to write Python than C, a Python interpreter that's written in Python (well, technically RPython) should be able to be modified much easier and with less bugs than CPython.

由于我们大多数人都同意编写Python比使用C语言更容易,因此使用Python编写的Python解释器(以及技术上的RPython)应该能够比CPython更容易修改并且错误更少。