6 ms·
This is glorious. From https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst : > We have the fo
by btown 12y ago
This is glorious. From https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst :
> We have the following major components in the PyPy repo:
>> An "emscripten" build platform definition, which teaches pypy's rpython toolchain how to compile things with emscripten: ./deps/pypy/rpython/translator/platform/emscripten_platform/.
>> An rpython JIT backend that emits asmjs at runtime: ./deps/pypy/rpython/jit/backend/asmjs/.
>> A "js" builtin module for the resulting interperter, to allow interaction with the host javascript environment: ./deps/pypy/pypy/module/js/.
>Along with these wrappers to help working with the resulting interpreter:
>> A wrapper to load up the compiled VM and expose it via a nice javascript API: ./lib/pypy.js.
>> A script for bundling python modules into an indexed format that can be easily loaded into the browser: ./tools/module_bundler.py.
In https://github.com/rfk/pypy/blob/master/rpython/jit/backend/asmjs/assembler.py https://github.com/rfk/pypy/blob/master/rpython/jit/backend/... alone, there are 2887 lines of custom Python code to emit asm.js for function and block JITing.
And at 3.26 seconds to initialize from cache on a 4-year-old machine, 5MB gzipped (not too much worse than your average high-quality gif) and only 1.5x slower than CPython... this is actually approaching viable if you needed to run Python business logic in the browser. Which I never thought I would see.
- mutagen 12y agoGlorious indeed. At least three levels of abstraction, two JITs, and a host of supporting code. Can we add some more layers like an emulator or get recursive by getting something like Pyasm running inside it?
- madeofpalk 12y agoHave you seen Gary Bernhardt's THE BIRTH & DEATH OF JAVASCRIPT? https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript https://www.destroyallsoftware.com/talks/the-birth-and-death...
- d2xdy2 12y agoI saw this a few months ago, and it never gets any less insane (in a good way) to watch. Just following Atwood's Law, I suppose.
- nudpiedo 12y agoI work on a machine that has two hypervisors, one third in plan, and more abstraction layers than a lasagna software. It is still very very efficient.
- StavrosK 12y agoIt may be fast, but I doubt it's very efficient...
- bobajeff 12y agoThis crashes my mobile (webview based) web browser.
- wfunction 12y agoHow can you have multiple hypervisors...? Do they both run in ring -1?
- mschuster91 12y agoHeh, that could be a good trick against hypervisor malware. Just run inside your own hypervisor...
- fragmede 12y agohttp://en.wikipedia.org/wiki/Virtualization#Nested_virtualization http://en.wikipedia.org/wiki/Virtualization#Nested_virtualiz...
- EpicEng 12y agoFast(ish) != efficient. Your setup is categorically inefficient. That may be fine for your use case, but let's not lie to ourselves.
- EpicEng 12y agoBut... Hardware is cheap, right? Hardware gets faster all the time, right? Sure, the end user rarely sees any noticeable improvement because we just use the speed increase as an excuse to write ever more inefficient code, but at least you get to use your favorite language in the browser.
- craftkiller 12y agohttp://en.wikipedia.org/wiki/Wirth%27s_law http://en.wikipedia.org/wiki/Wirth%27s_law
- StavrosK 12y agoSince PyPy is written in (R)Python, you can just run PyPy inside the resulting interpreter, for a 1000x slowdown.
- callahad 12y agoPotentially just one JIT, right? asm.js is AOT compiled on Firefox.
- fijal 12y agoNo, because it's "AOT" compiled when it's downloaded or executed (since you have one JIT, you can't AOT compile what it produces). So you can think about it as "just" one JIT, but a very inefficient one
- pcwalton 12y ago> since you have one JIT, you can't AOT compile what it produces I'm not sure what this means, but if you mean that Firefox doesn't cache compiled asm.js code, that's not true. It does [1]. [1]: https://blog.mozilla.org/luke/2014/01/14/asm-js-aot-compilation-and-startup-performance/ https://blog.mozilla.org/luke/2014/01/14/asm-js-aot-compilat...
- fijal 12y agoSo look. PyPy compiles bits and pieces of code generating assembler. This assembler then gets to be turned into Javascript (asm.js) which needs to be parsed and compiled. All of this happens at runtime and while you can technically call the second piece of it AOT, it's essentially a double JIT or at least you pay the cost of double jitting. The equivalent in C would be generating C at runtime and sending it through gcc/clang.
- deleted 12y ago[deleted]
- pjc50 12y agoIt's compilers all the way down.
- throwaway41597 12y agoSince PyPy compiles RPython to C, couldn't one write a web app in RPython, compile it to C, then to JS (with asm.js) and enjoy a quick page load and smaller file size? By the way, is RPython a nice language to program in?
- throwaway41597 12y agocorrection: the C to JS step would be done with Emscripten (asm.js is a subset of JS not a compiler) Thank you for the answers
- icebraining 12y agoThe differences are described here: https://rpython.readthedocs.org/en/latest/rpython.html https://rpython.readthedocs.org/en/latest/rpython.html
- throwaway41597 12y agoThanks but to be honest, I'm not proficient in Python, so it's hard to guess if those restrictions would hurt, although they seem fairly small. I was more asking for personnal opinions and projects using it. I wasn't able to quickly find any, it even seems pretty much nobody writes RPython code or maybe people call it Python code so googling is hard.
- detaro 12y agoI don't know of any real projects outside of interpreters that use it. It has downsides (long compile times come to mind) and I'm not sure how well you can utilize the strengths (like the JIT) for non-interpreters. EDIT: Writing it is more restricted than normal Python, so you have to think about what you do (especially type hints, to make sure it gets what type your variables are) and the long feedback cycles due to compile time are annoying, but still very comfortable. And for building interpreters, the more or less "free" JIT is magic :)
- aidos 12y agoI've never worked with RPython. Looking through that list it doesn't look dreadful. There are a couple of things that jump out though - you can't use kwargs in function definitions which is probably used quite a lot in dynamic settings. Eg in Python one might do (warning; crazy, contrived example): def create_thing(**kwargs): thing = dict(id=gen_id()) thing['other_data'] = kwargs return thing thing = create_thing(name='name', foo='bar') You could avoid it, and generally in Python you're better off being explicit about everything. But it's definitely a feature that gets used a fair amount, especially in libraries where you don't explicitly know what the api call takes concerning user data. An easy (more explicit) workaround would be: def create_thing(other_data=None): thing = dict(id=gen_id()) thing['other_data'] = other_data or {} return thing create_thing(dict(name='name', foo='bar')) It's just a little more work for the calling code.
- aidanhs 12y agoA little frustrating that github's down, as I'd be interested to see their build process - with some trickery I was able to get empython [0] down to 2.5MB gzipped from 7MB, though I'm aware pypy as a binary is quite a bit larger anyway. I also wish github was up so I could look at the benchmark code - startup looks much faster with empython, but I'd like to know how much slower interpreting would be. [0] http://aidanhs.github.io/empython/ http://aidanhs.github.io/empython/