4 ms·
Building Oil with the OPy Bytecode Compiler
- montecarl 9y agoThis post has more details on what OPy is: http://www.oilshell.org/blog/2017/04/09.html http://www.oilshell.org/blog/2017/04/09.html
- abecedarius 9y agoI see the author didn't know about another Python-subset-in-Python, https://github.com/darius/tailbiter https://github.com/darius/tailbiter -- I should've promoted it a little more, and maybe it'd have been helpful.
- chubot 9y agoActually I did find it a couple months ago! Someone asked me about it yesterday: https://www.reddit.com/r/ProgrammingLanguages/comments/81wkgv/building_oil_with_the_opy_bytecode_compiler/dv6930s/ https://www.reddit.com/r/ProgrammingLanguages/comments/81wkg... I didn't know about it when I did the initial work for OPy in 2017 though. I find the "expression-based" style quite interesting and I suspect it will help me understand the "compiler2" code better. I plan to look at it in more detail as I'm optimizing Oil. This post links to a few posts that mention "byterun". These two pieces of work are probably what pushed me over the edge to apply to Recurse Center! I'm going from May-August this year. I'd love to connect with anyone interested in this kind of thing (e-mail in my profile) ----- And if you have any advice on how to compile Python to more optimized code, I'm interested. I assume this will involve creating some new VM instructions (i.e. it can't just be done with the bytecode compiler) This is a very concrete task -- the OSH parser is around 5,000 lines of code that would be very annoying to port to another language. Essentially, it's 3 interleaved recursive descent parsers and a Pratt parser. I already have benchmarks that show it's 40-50x too slow: http://www.oilshell.org/release/0.5.alpha2/benchmarks.wwz/osh-parser/ http://www.oilshell.org/release/0.5.alpha2/benchmarks.wwz/os... Leaving aside the rest of the shell (which is not big either), I think it's an interesting question if you can recover that factor of 40-50 without rewriting the code. It's written in a pretty "static" style without much dynamism. You don't need any special language features in a recursive descent parser. I already did something like this. I wrote a whole bunch of Python regular expressions for the lexer, then compiled it to C code via re2c: When are Lexer Modes Useful? http://www.oilshell.org/blog/2017/12/17.html http://www.oilshell.org/blog/2017/12/17.html re2c code: http://www.oilshell.org/blog/2017/12/files/osh-lex.re2c.h.html http://www.oilshell.org/blog/2017/12/files/osh-lex.re2c.h.ht... (And to anticipate a question from passers by: it does not make sense to use a parser generator here -- I wrote about this extensively on the blog, e.g. http://www.oilshell.org/blog/tags.html?tag=parsing#parsing http://www.oilshell.org/blog/tags.html?tag=parsing#parsing)
- abecedarius 9y agoNeat! I'll probably be around for the alumni week in May at Recurse Center, and maybe we could get to chat then. For optimization I guess it comes down to making productive restrictions on the Python dialect to rule out some of the extreme dynamism. This sounds like a really cool project, one that's too big for me to have much idea what'd help without investing more time. What you're doing in stripping down CPython reminds me a little of how Luke Gorrie's started adapting LuaJIT to his own purposes: https://github.com/raptorjit/raptorjit https://github.com/raptorjit/raptorjit
- chubot 9y agoI'm going to the summer batch from May 21st to August 8th, and hopefully by then I will have made more progress. Definitely let me know if you are around then! Yeah I agree that you want to remove dynamism. For something like BINARY_ADD, the operands will only be strings or numbers in a parser. You can cheat and just assume there is no operator overloading. Although I don't know how much that will actually speed things up! I should make a profile at the C level. I have profiled at the Python level and sped things up 6-7x already. The other thing I think will help is using something like spans/slices instead of strings. Parsing creates all these tiny string objects. And also as I mentioned changing the representations of the nodes, which is a fairly naive Python representation now. Python objects are huge! ----- I didn't know about raptorjit, but I had heard of Snabb Switch awhile ago on Hacker News. It looks interesting! It reminds me of the Dart language being inspired by v8. The problem with v8 is that you can change one line in JS and your performance will just fall off a cliff. It's hard to detect unless you write benchmarks, which most people don't. So Lars Bak started Dart to remedy that problem, designing the language around stable JIT performance!
- abecedarius 9y agoThe size of Python objects is kind of shocking, yeah. I wonder how hard it'd be to at least get rid of the refcount. I like your idea of using slices. So, a new PyObject subtype that pretends to be a string but holds a couple of indices and a pointer to the 'real' string? I guess you could leave the latter implicit, hopefully. There's a good chance I'll still be around in late May. See you then!
- jnbiche 9y agoSo he writes Oil Shell in Python because he wants to get it done quicker than he would writing in C. And yet now he's found that the performance in Python is inadequate for a shell to compete with Bash (pretty predictable) and so he's basically writing a new Python interpreter (both bytecode compiler and VM), which (as he'll find out) is a huge task, regardless of how simplistic he makes the interpreter. That said, I think Oil Shell the shell language looks pretty cool, so I wish he had done it in a language like C, Rust, or even OCaml (which he apparently considered).
- chubot 9y ago(author here) Well, a key point is that it doesn't have to be an entire Python interpreter. It just has to be the subset that Oil uses. (This applies to both both Python syntax and semantics.) I also forked the 8K LOC bytecode compiler -- I didn't write it from scratch. That's the point of "Cobbling together a Python interpreter". So for a cartoonish view, compare these implementation strategies. 1. Clone bash in C, which (charitably) would involve writing around 80K lines of C code from scratch, given that bash is 160K lines. You can probably do better by not writing it over 30 years :-) 2. Write 16K lines of Python, fork a bytecode compiler in Python (8K lines), and then either fork CPython's VM or write your own. The post implies that #2 is probably easier than #1. I haven't done the last part of #2, so I could be wrong. I think it's an interesting experiment to find out whether you can write a bespoke compiler/VM for a specific program in a high level language. ----- It's a little bit like TeX and Pascal. I discovered awhile ago that that TeX is written in an abstract subset of Pascal. Apparently the version you use on most Linux distros is compiled to machine code via a Pascal-to-C translator. I'm not sure if that translator was written specifically for TeX or not, but if anyone knows, please chime in! But I have seen enough of Knuth's code to know that he does not rely on implementation details. That is roughly the case with Oil and Python. As I mention in the post, Oil is written in Python+ASDL, not Python. And I would even call it Python + ASDL + some abstract regex subset. That subset could be called the intersection of Python regexes + re2c regexes, e.g. at the end of this comment: https://news.ycombinator.com/item?id=16525378 https://news.ycombinator.com/item?id=16525378 Basically when writing Oil, I try to be clear about the algorithms, and not think in terms of implementation details of a particular platform. This is possible for a shell because it's mostly string handling, and the only libraries it uses are a handful of libc calls (which are 40 years old, standardized, and well understood.)