简体   繁体   English

用于编译Python / Boo / Ruby语法的工具,用于C / C ++ / LLVM / Javascript(使用JS ArrayBuffer提高速度)

[英]Tools for compiling Python / Boo / Ruby like syntax to C / C++ / LLVM / Javascript (using JS ArrayBuffer for speed)

I'm trying to automatically compile / convert code written with Pythonic semantics into native and fast Javascript code. 我正在尝试将使用Pythonic语义编写的代码自动编译/转换为本机和快速Javascript代码。

What tools can do this, with nice debugging support possible like with Java etc? 有什么工具可以做到这一点,可以像Java等一样提供良好的调试支持?

Has anyone done this? 有没有人这样做过?

Why? 为什么?

I'm trying to write some visualisation code with a complex main loop, a timeline, some physics simulation, and some complex interaction. 我正在尝试用复杂的主循环,时间轴,一些物理模拟和一些复杂的交互来编写一些可视化代码。 IE: it IS an actual CPU bound problem. IE:它是一个实际的CPU绑定问题。

Writing with Javascript and testing in it's browser environment is harder to debug than say, Java, .NET or Python running in a decent IDE. 使用Javascript编写并在其浏览器环境中进行测试比在一个体面的IDE中运行的Java,.NET或Python更难调试。 But for doing actual large scale web development with complex client side code, it's necessary to at least compile to Javascript, if not directly write in it. 但是对于使用复杂的客户端代码进行实际的大规模Web开发,至少需要编译为Javascript,如果不直接写入它。

Background: Recent advances 背景:最新进展

Emscripten allows compiling C/C++ to Javascript, that can run with increasing efficiency in the browser due to ArrayBuffer's typed array support and new browser JS engines, as ASM.js and LLJS take advantage of Mozilla's recent speed improvements (that other venders will likely soon follow). Emscripten允许将C / C ++编译为Javascript,由于ArrayBuffer的类型化阵列支持和新的浏览器JS引擎,可以在浏览器中提高效率,因为ASM.jsLLJS利用了Mozilla最近的速度改进(其他供应商可能很快跟随)。

Altjs.org has a laundry list of Javascript alternaltives, but doesn't yet focus on the recent speed improvements or nice semantics specifically but it is becoming common place for people to code for browsers with better tools. Altjs.org有一个Javascript holternaltives的清单,但是还没有专注于最近的速度改进或特别好的语义,但它已成为人们用更好的工具为浏览器编码的常见地方。 Emscripten in particular has loads of amazing demos . 特别是Emscripten有很多令人惊叹的演示

Possible options already considered: 已考虑的可能选择:

  • Shedskin - Currently I have tried getting Shedskin working but I have limited C++/C skills (Emscripten only exposes a C API for the Boehm inspired garbage collector it uses, and Shedskin needs a C++ garbage collection class for it's objects, which doesn't exist yet). Shedskin - 目前我已经尝试过让Shedskin工作,但我的C ++ / C技能有限(Emscripten只为它使用的Boehm灵感垃圾收集器公开了一个C API,Shedskin需要一个C ++垃圾收集类来处理它的对象,它不存在然而)。
  • Unladen Swallow / RPython, to LLVM - have not been able to setup correctly on Ubuntu yet UnLden Swallow / RPython,到LLVM - 尚未能在Ubuntu上正确设置
  • Boo to Java then to LLVM (not been able to setup on my Ubuntu system yet) Boo到Java再到LLVM(还没能在我的Ubuntu系统上安装)

Additional constraints: 附加限制:

  • I need to use this on my Ubuntu system. 我需要在我的Ubuntu系统上使用它。
  • The compiled Javascript should probably be less than 1 MB 编译的Javascript应该小于1 MB
  • Debugging in the native language which is also cross compiled, should still be possible, allowing taking advantage of existing debug tools. 仍然可以使用本地语言(也是交叉编译)进行调试,从而可以利用现有的调试工具。

"This process of constructing instruction tables should be very fascinating. There need be no real danger of it ever becoming a drudge, for any processes that are quite mechanical may be turned over to the machine itself." “构建指令表的这个过程应该非常有趣。它不需要成为苦差事的真正危险,因为任何非常机械化的过程都可能被转移到机器本身。” -- Alan M. Turing, 1946 - 艾伦·图灵,1946年

You want a high level dynamic language that compiles down to efficient low level JavaScript? 您想要一种高级动态语言,可以编译为高效的低级JavaScript吗? There is no such thing. 哪有这回事。 If dynamic languages were fast we would not need asm.js in the first place. 如果动态语言很快,我们首先不需要asm.js。

If you want to write code that compiles to efficient JavaScript your will have to learn a lower level language. 如果您想编写编译为高效JavaScript的代码,您将需要学习较低级别的语言。 The reason why Emscripten is fast is because it compiles from a low level language (C/C++) that allows for greater compiler optimization than regular JavaScript. Emscripten快速的原因是因为它从低级语言(C / C ++)编译,允许比常规JavaScript更好的编译器优化。 That is also the reason why asm.js and LLVM can be faster. 这也是asm.js和LLVM可以更快的原因。 They get their speed from not have dynamic types, garbage collection (this specifically is what makes it possible to use ArrayBuffer for memory) and other high-level features. 他们从没有动态类型,垃圾收集(这特别是使得可以使用ArrayBuffer用于内存)和其他高级功能得到他们的速度。

Bottom line is. 底线是。 There exists no tools for compiling a language with Pythonic semantics into native and fast Javascript code. 没有工具可以将Pythonic语义编译成本机和快速Javascript代码。 And depending on what you mean by semantics it is unlikely that such a thing will appear since Python is a slow language in itself. 根据你对语义的意思,不太可能出现这样的事情,因为Python本身就是一种缓慢的语言。

The best option right now for generating fast JavaScript is Emscripten. 现在生成快速JavaScript的最佳选择是Emscripten。 You could also consider LLJS or writing fast JavaScript by hand (Chrome has debugging tools for this). 你也可以考虑LLJS或手工编写快速JavaScript(Chrome有调试工具)。

Also, considering the title of your question you are very concerned about syntax. 另外,考虑到你的问题的标题,你非常关心语法。 You should not be. 你不应该。 When choosing the right language for the job the syntax is one of the least important factors. 在为工作选择正确的语言时,语法是最不重要的因素之一。

Since you mentioned shedskin yourself, I would imaging that you can share some of your experience (and explain what exactly in your opinion shedskin is missing, except that its input is a restricted python grammar). 既然你自己提到了脱壳 ,我会想象一下你可以分享一些你的经验(并解释你的看法究竟是什么样的,除了它的输入是一个受限制的python语法之外)。 I could also assume that Cython/Pyrex are not acceptable (due too grammar restrictions). 我还可以假设Cython / Pyrex是不可接受的(由于语法限制太多)。

If shedskin is too much in alpha stage for you, then you might be looking for something like Numba project, which includes a compiler of dynamic python into LLVM as well as llvm-py which allows to link LLVM exposed bytecode similar as ctypes allows to link shared-libraries and build LLVM IR compilers. 如果在你的alpha阶段对于脱壳太多了,那么你可能正在寻找像Numba项目这样的东西,其中包括一个动态python到LLVM的编译器以及llvm-py,它允许链接LLVM暴露的字节码,类似于ctypes允许链接共享库和构建LLVM IR编译器。

Here is a cut from the blog where it is shown how one can use Numba as JIT for numpy (incl. performance comparison with equivalent Cython code): 这是博客中的一个剪辑,其中显示了如何使用Numba作为JIT进行numpy(包括与等效Cython代码的性能比较):

import numpy as np
from numba import double
from numba.decorators import jit

@jit(arg_types=[double[:,:], double[:,:]])
def pairwise_numba(X, D):
    M = X.shape[0]
    N = X.shape[1]
    for i in range(M):
        for j in range(M):
            d = 0.0
            for k in range(N):
                tmp = X[i, k] - X[j, k]
                d += tmp * tmp
            D[i, j] = np.sqrt(d)

Emscripten should allow you to expose and call your python -> llvm -> JS code as described here: https://github.com/kripken/emscripten/wiki/Interacting-with-code Emscripten应该允许你公开和调用你的python - > llvm - > JS代码,如下所述: https//github.com/kripken/emscripten/wiki/Interacting-with-code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM