Hacker News new | comments | show | ask | jobs | submit login
Compiling a subset of Python syntax to x86-64 assembly for fun and zero profit (benhoyt.com)
137 points by benhoyt 9 months ago | hide | past | web | favorite | 15 comments

There was a whole course (notes for which are here [1]) dedicated to this topic, by prof. Jeremy Siek.

[1] ecee.colorado.edu/ecen4553/fall09/notes.pdf

Wow, that looks really good -- thanks for sharing the link. Yeah, it's definitely similar to what I'm doing but in a more formal and non-toyish way.

Looks cool. But why asm? Generating C or C++ code and letting the GCC optimizer do the heavy lifting for you could probably get you a massive perf boost.

Cython already does this. Including using type hints.

Because the people who talk down python for not being c/c++ "would win".

Why? Both C/C++ and Python are just tools and they have different strong/weak sides. I can't see much harm in combining the strong sides of 2 worlds.

That said, isn't PyPy (https://pypy.org/) already doing the same thing?

I wasnt being serious - in line with the headline "for fun and no profit".

Optimizing python with calls out to optimized c/c++ code has always been a thing. I think a large part of the interest here is novelty.

Interesting topic. We did something similar in the past using the bytecode instead of the AST tree to accelerate genetic algorithm evolution:

http://multigrad.blogspot.com/2014/07/low-level-frenzy.html http://multigrad.blogspot.com/2014/06/fun-with-python-byteco...

I'm curious, what was the original use case for pyast64? Feels like we could apply it to genetic programming fairly straightforwardly.

Sorry, I didn't have a use case -- I did it purely for fun. However, I got the idea when I was trying to optimize some Python bytecode.

This was a very fun read. Makes me want to get back into assembly, figure out 64 bit assembly and make this more than a toy... almost.

> pyast64 with peephole 0.24 29.7

> C version (gcc -O0) 0.22 31.8

Modern x86_64 processors are really good at ruing "bad" asm pretty efficiently.

Absolutely. The amount of tricks used to achieve that is amazing, including things register renaming, branch prediction, virtual registers, instruction reordering etc.

That is not really what demonstrated here though, as gcc was run with optimizations turned off, since otherwise the program would have been optimized to printing a constant.

Using the same technique, would it be possible to generate WebAssembly ?

Informative article as always from you. Thanks!

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact